Agent Loop
The core control flow of an AI agent: observe → think → act → observe again. The model receives input, reasons about the next step, calls a tool, reads the result, and decides whether to keep going or stop. Every modern agent framework (LangGraph, OpenAI Agents SDK, Anthropic Claude Agent SDK, CrewAI) is at its heart a loop with different policies for when to halt, how to handle errors, and how many iterations to allow.
Example
A support agent receives a ticket. Iteration 1: it reads the ticket and decides to search the knowledge base (tool call). Iteration 2: it reads the search results and decides it needs the customer's order history (tool call). Iteration 3: it has enough context and drafts a reply (no tool call). The loop halts because no tool was called and the agent has produced a final answer.
Frequently asked questions
- How do you stop an agent loop from running forever?
- Three common guardrails: (1) a max-iteration cap (typically 10–25 for production agents), (2) a max-token budget per run, and (3) a wall-clock timeout. A well-designed agent halts on its own when the task is done; these are safety nets for tasks that confuse it. Always set all three—a single safeguard fails when the model loops in a way that doesn't trigger it.