Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.2 The Agent Loop

Every agent, regardless of framework, regardless of language, regardless of vendor, runs the same loop:

  1. Perceive. Read inbound state — the event that triggered this run, the conversation history, the relevant data from external systems.
  2. Reason. Call a model. Get back either a text response, a tool-call request, or a sub-task delegation.
  3. Act. If a tool was requested, execute it. If a sub-task was requested, spawn a sub-agent. If a text response was produced, persist it and emit it.
  4. Observe. Append the action’s result to state. Persist a checkpoint. Emit a trace span.
  5. Loop or terminate. Decide whether to call the model again or stop.

This loop is so simple that the entire agent-frameworks industry is, in its first-order function, a collection of opinions about how to spell it. Mastra spells it as a typed Workflow with Step primitives. LangGraph spells it as a StateGraph with nodes and conditional edges. PydanticAI spells it as Agent.run_stream. The Anthropic Agent SDK spells it as an Agent with tools and an internal harness. The OpenAI Agents SDK spells it as Runner.run over an Agent config. Strands spells it as a model-driven loop with hooks. Cloudflare Agents spells it as a Durable Object method.

A working heuristic: if a framework’s spelling of the loop is more than fifty lines of code thicker than the model SDK’s bare loop, you are paying for opinions that will turn into liabilities. The best frameworks have negative abstractions — they remove code you would have written rather than adding code you would not have. Mastra and PydanticAI sit on the right side of this line in 2026. CrewAI’s role-based abstraction sits on the wrong side for any agent that is not literally a panel of role-playing experts.

The second-order function — what frameworks actually compete on, once they have spelled the loop — is everything around the loop: durability, sub-agent spawning, state persistence, tool dispatch, observability instrumentation, eval hooks, prompt management, deploy targets. The first-order spelling matters for week two. The second-order plumbing matters for month twelve.