The Agent Loop
The runtime that keeps agents on track
The agent loop is the core runtime: reasoning turn, tool execution, result ingestion, capped iteration. Getting the loop right — message hygiene, termination, progress checks — determines reliability more than model choice.
What you will learn
- Build a correct reasoning-act loop
- Define termination and stop conditions
- Keep message history healthy and bounded
What is this?
The agent loop alternates model turns and tool turns: the model reasons (and maybe emits tool calls), tools execute and return results, histories accumulate, and the loop checks progress against caps. The quality of the loop — not the model — drives reliability: correct roles, trimmed context, explicit terminations, and honest progress checks.
Why do you need it?
- A subtle loop bug re-plays stale results or drops turn context.
- Termination logic prevents runaway cost, latency, and loops.
- Healthy histories = coherent long tasks.
- Most agent failures trace to loop bugs more than model errors.
Mental model
A relay race with checkpoints
The agent loop
Runners (turns) pass the baton (context) in a fixed rhythm; marshals (loop code) check each checkpoint (progress). The race ends only at the finish or the marshal's whistle (stop condition) — and the baton state must be exact at every handoff.
How it works
- Append user goal to a message history.
- Model turn: may emit tool calls or finish.
- Execute calls (validated), append tool results.
- Check caps (turns, tokens, time) and progress.
- Terminate: success, cap, stall, or explicit stop.
Loop anatomy.
In production
- Standard SDK agent loops (AI SDK, LangGraph) encode correct turns.
- Custom loops parse tool results into structured observations.
- Eval harnesses replay the same loop against golden tasks.
Best practices
- Preserve role correctness in history (user/assistant/tool).
- Keep tool results terse; trim history when long.
- Terminate on explicit completion text, not silence.
- Log turn-by-turn states for debugging.
Common mistakes
- Appending tool results under wrong roles.
- No progress check — loops drift over repeated turns.
- Unbounded history blowing context and confusing grounding.
Common issues
Loop runs forever on some tasksStop condition weak
Add turn/token caps + stall detection + explicit 'done' marker.
Progress evaporates mid-taskTerminal summaries not carried
Carry a 'progress so far' note into each model turn.
Debugging tips
- Repeated identical tool call → stall detection missing.
- Answer quotes stale data → history trimmed the observation; promote key facts.
- Loop exits early → completion criterion too loose; require artifact/evidence.
Interview checkpoint
Hands-on lab
- Implement reasoning→tool→observe iteration
- Enforce turn caps and stall detection
- Return a final summary with progress
Build a Minimal Agent Loop
40 min · intermediate
Quick quiz
The agent loop terminates on…
Tool results can be dropped from history without consequence.