Track 03 · Agents & toolsLesson 01 of 12
The agent loop, written by hand
A while loop around a model call, with state and stop conditions you can name.
- The FDE path · Stage 4 of 6
- 35 min read
- 60 min hands-on
- Pro
What you'll be able to do
01- Explain the agent loop as a program: a
whileloop around a model call, with explicit state, and name what happens in each of the perceive, reason, act and observe phases. - Implement a loop from scratch with at least five stop conditions (end turn, max steps, token budget, deadline, no-progress) and a per-step trace.
- Calculate how input tokens grow per step and size a step budget and token budget that fit a customer's cost and latency limits.
- Diagnose a looping or runaway agent from its trace and choose the right fix (tool error design, stop condition, or architecture change).
- Map the parts of your hand-written loop onto the equivalent settings in the Claude Agent SDK, OpenAI Agents SDK, LangGraph and a managed platform such as Lyzr, so you can reason about any runtime you are handed.
Before you start
- ReAct, chain of thought and reflection in practice: the reasoning patterns the model uses inside each step.
- Typed output: schemas, validation and tool calls: how a tool call is represented on the wire.
- Tokens, context windows and why answers go wrong: token counting.
- Context on a budget: what to keep, compress and cut: the techniques you apply when the loop's context grows too large.
- Comfortable Python: dataclasses, type hints, dictionaries.
The short version
An agent is a program that calls a model in a loop. Each iteration assembles the context (perceive), calls the model (reason), executes any tool calls the model requested (act), and appends the results to the history (observe). The loop ends when the model says it is done, or when your code decides it must stop: too many steps, too many tokens, too much time, no progress, or a human interrupt. The model decides what to do next; your code decides whether it is allowed to keep going. Every framework and platform you will be handed is a variation on this loop, and most production incidents with agents trace back to a missing stop condition, unbounded context growth, or an error that was thrown instead of observed.
Key takeaways:
- The loop is ordinary code you own. Stop conditions, budgets and state are your responsibility, not the model's.
- Input tokens grow every step because the whole history is resent. Cumulative input cost grows roughly with the square of the step count.
- Tool failures should come back to the model as observations it can act on. Exceptions end the loop; observations let it recover.
- "The model said it was done" is only one of several stop reasons. Always record which one fired.
- Instrument every step: model call, tool call, tokens, latency, stop reason. Without the trace you cannot explain a failure to the customer.
Pro lesson
The rest of this lesson is in Pro.
The mental model, worked example, field note, failure table, exercise and self-check, with the voice tutor.
Pro opens this stage
Stage 4 · Agents that act safely- 7 lessons, with the voice tutor
- Lab · Find the intake agent's failed writes, graded here
Also in Pro
- Every stage and specialisation, in full
- Every lab and every role project
- Halden weeks 2–10, on your record
- Every drill, live in the playground
- All solutions, plus the server judge
- Lab Assistant and every Teach with AI