Vol. I · First Principles · I.3
The Agent Loop
Context in, structured next step, execute, append, repeat. Termination is a first-class intent, not an accident of running out of tokens.
6 min read
Doctrine
- Treat 'done' as a tool or a structured intent, not as falling off the end of a prompt.
- Every iteration must have a budget: steps, tokens, wall clock, money.
- Append observations, not novels. Compact errors. The window is a scarce resource.
The four lines
initial_event → context = [event]. Loop: next = llm.determine_next_step(context); if next.intent == done: return; result = execute(next); context.append(result). That is the entire runtime. LangGraph, the OpenAI Agents SDK, smolagents, and AutoGen all compile to this.
The interesting work is what goes into determine_next_step (prompt, tools, truncated history) and what execute is allowed to touch (sandbox, browser, production database).
Termination and recovery
Agents fail by rambling. Force a done schema. Force a step cap. On tool failure, compact the error into the window (Factor 9) rather than dumping a stack trace or retrying blindly.
LangGraph checkpoints make the loop pausable. Factor 6 says launch/pause/resume should be boring APIs. If you cannot resume after a crash, you built a demo.
Anti-patterns
- Infinite loops with no max-turns.
- Swallowing tool exceptions so the model never sees why the world refused.
- Letting the model emit free text when a schema was required.