Phase 6 · Tools, Agents & the APIModule 24~36 min read

Building Agents: The Reasoning Loop

An agent is a model in a loop with tools and a goal. See the think-act-observe cycle, how to prompt for it, and how to keep an autonomous run on the rails.

What you'll learn

An agent is a language model placed in a loop with tools and a goal, deciding its own next step until the job is done. It's the culmination of this course — chaining (Module 11), reasoning (Module 13), and tools (Module 23) combined into a system that acts. It's also where good prompting matters most, because small errors compound over many steps.

By the end of this module you'll be able to:

  • Say what turns a model into an agent
  • Describe the think-act-observe loop and how a goal drives it
  • Write an agent system prompt that keeps it focused
  • Add stopping conditions and guardrails so a run stays reliable

What makes a model an agent

In a fixed chain (Module 11), you decide the steps in advance. In an agent, the model decides the steps at runtime: given a goal and a set of tools, it chooses what to do next based on what it has learned so far, and keeps going until it's finished. That autonomy is powerful — and it's exactly why an agent needs tighter prompting and firmer limits than a one-shot prompt.

The think-act-observe loop

Under the hood, an agent runs the ReAct loop from Module 13, now driven by real tools: thinkabout the next step, act by calling a tool, observe the result, and repeat — accumulating knowledge each cycle until it can answer.

The agent loop
Goal

Think

It reasons about the next step toward the goal.

Act

It calls a tool (Module 23).

Observe

It reads the result and updates its plan.

↻ loop until the goal is met or a stop condition fires

A goal instead of a script. The model thinks, acts with a tool, observes, and loops — choosing each step itself.
agent.py
# An agent is a loop: the model chooses each step until the goal is met.
messages = [{"role": "user", "content": goal}]

for step in range(MAX_STEPS):                 # a hard budget (safety!)
    resp = client.messages.create(model=M, system=AGENT_PROMPT,
                                  messages=messages, tools=TOOLS)

    if resp.tool_call:                        # ACT
        result = run_tool(resp.tool_call)     # your code executes it
        messages.append(resp.message)
        messages.append({"role": "tool", "content": result})  # OBSERVE
        continue                              # THINK again next iteration

    return resp.output_text                   # goal met - final answer

return "Stopped: step budget exhausted"       # graceful stop
An agent is remarkably simple: the tool-use loop plus a goal and a hard step budget. The intelligence is in the prompt and tools.

The agent's system prompt & planning

The system prompt is an agent's constitution. Because it steers many autonomous steps, it must be more thorough than a normal prompt. A strong agent prompt spells out:

  • The role and goal — what the agent is for and what "done" looks like.
  • How to use the tools — when to reach for each, and to prefer tools over guessing.
  • A planning instruction — think first, break the goal into sub-goals, tackle them in order.
  • Rules and limits — what it must never do, and when to stop and ask a human.

Key idea

Ask the agent to plan before acting and to state when the goal is complete. A short plan up front keeps a long run coherent; an explicit "done" signal keeps it from wandering past the finish line.

Stopping conditions & budgets

Autonomy's flip side is that an agent can loop forever — repeating a failing action, or chasing a goal it can't reach. Every agent needs hard limits it cannot exceed:

  • A step cap — a maximum number of loop iterations (the MAX_STEPS above).
  • A budget — a ceiling on tokens or dollars per run.
  • A success check — a concrete definition of "done" so it stops when finished.
  • A give-up path — what to do when it's stuck (stop and report, or escalate to a human).

Keeping agents on the rails

Errors compound across steps, so reliability engineering (Module 17) matters even more here:

RiskMitigation
Looping / getting stuckHard step and cost budgets with a graceful stop
Context bloat over many stepsCompact history and prune old tool output (Module 21)
A wrong early step derails the runPlan first; add a critic/verifier step (Module 13)
Unsafe or irreversible actionsLeast-privilege tools; require confirmation for big actions
Untrusted input hijacks the agentTreat tool/document content as data (Module 28)
An agent multiplies both the power and the risk of a single prompt — engineer the guardrails accordingly.

Watch out

Start simple. Most tasks don't need a fully autonomous agent — a fixed chain (Module 11) is more predictable and easier to debug. Reach for an agent only when the steps genuinely can't be known in advance.

Recap & quick check

Key takeaways

  • An agent is a model in a loop with tools and a goal, choosing its own next step until the job is done.
  • Unlike a fixed chain (you script the steps), an agent decides the steps at runtime — powerful but riskier.
  • It runs think → act (tool) → observe, repeatedly; the code is simple, the intelligence is in the prompt and tools.
  • The agent's system prompt must define role, goal, tool use, a planning instruction, and hard rules.
  • Always add stopping conditions — step caps, budgets, a 'done' check, and a give-up path — and compact context over long runs.

Quick check

1. What distinguishes an agent from a fixed prompt chain?

2. Which loop does an agent run?

3. Why must every agent have a step cap or budget?

4. When should you prefer a fixed chain over an agent?

One agent is powerful; sometimes a team of specialized agents is better still — and sometimes it's needless complexity. Next up: Module 25 — Multi-Agent Systems & Workflows.