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.
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
# 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 stopThe 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
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_STEPSabove). - 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:
| Risk | Mitigation |
|---|---|
| Looping / getting stuck | Hard step and cost budgets with a graceful stop |
| Context bloat over many steps | Compact history and prune old tool output (Module 21) |
| A wrong early step derails the run | Plan first; add a critic/verifier step (Module 13) |
| Unsafe or irreversible actions | Least-privilege tools; require confirmation for big actions |
| Untrusted input hijacks the agent | Treat tool/document content as data (Module 28) |
Watch out
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.