Agent Loop
The control flow that lets an LLM act as an agent — repeatedly thinking, calling a tool, observing the result, and deciding the next move.
What Is an Agent Loop
The agent loop is the control flow underneath every AI agent: the model thinks, decides whether to call a tool, the tool runs, the result is fed back, and the model decides what to do next — repeated until the task is done or a stop condition is hit. It’s the engineering shape of ReAct in production code. Use the term when discussing how agents are implemented, not just what they do.
How It Works
A minimal agent loop in pseudocode:
while not done:
response = model.complete(messages, tools=available_tools)
if response.is_tool_call:
result = run_tool(response.tool, response.args)
messages.append(tool_result(result))
else:
done = True
return response.text
Production-grade loops add:
- Step budget — hard limit on iterations to prevent runaway loops (typical: 25–100 steps)
- Cost budget — token or dollar cap that aborts before runaway spend
- Error handling — tool failures are caught and surfaced back as observations, not crashed
- Checkpointing — loop state is serializable so a workflow can resume after process restart
- Concurrency — many production loops dispatch parallel tool calls within a single iteration
Why It Matters
The agent loop is where every interesting failure mode lives: infinite loops, context-window overflow, runaway token cost, tool-call hallucinations, off-task drift. Every framework (LangGraph, CrewAI, Mastra, Pydantic AI, OpenAI Agents SDK) is fundamentally a wrapper around the agent loop with different opinions on state management, retries, and observability. Pick one based on which failure modes you’re most likely to hit.
Examples
- Claude Code — agent loop with file-system and bash tools, step budget enforced by the CLI
- Cursor Composer — multi-file agent loop with IDE-state tools
- LangGraph — graph-based loop with persistent state and conditional edges
- OpenAI Agents SDK — official OpenAI loop primitive, parallel tool calls supported
- Trinity — custom loop in the homelab agent referenced in the MCP production review