The agent loop
How a model works through a task: think, use a tool, read the result, and go round again.
Read first:Tool callingIn-context learning
Step 1 of 6· Scripted, illustrative trace and tools; the loop, budget, gate and stop logic work as described
A chatbot answers once. An agent works in a loop. Given a goal and a set of tools, the model writes a short thought about what to do next, then asks for a tool to be run, such as looking up an order. Ordinary software runs the tool and adds the result to what the model can see, and the model decides again. The loop ends when the model says it has finished, or when a fixed step budget runs out. Anything that cannot be undone, such as sending money, should wait for a person to approve it. One influential research version of this pattern is ReAct: reasoning plus acting.
Why it matters for your product
Every pass is another model call, so agents cost more and take longer than a single answer, and small errors compound. On τ-bench, a customer-service benchmark, even GPT-4o completed under half the tasks, and under a quarter of retail tasks succeeded in all of eight repeat trials. So give agents a hard step budget, the narrowest tool permissions that work, and a human approval for anything that moves money or data. Anthropic advises starting with the simplest design that works; we usually try a fixed workflow before a free-running agent.
For engineersShow the maths
cₜ = (o₁, a₁, …, oₜ₋₁, aₜ₋₁, oₜ); aₜ ~ π(aₜ | cₜ)
At step t the agent's context c is everything it has seen and done so far, and the model π picks the next action from it. ReAct lets a thought count as an action too: it adds to the context but gets no observation back.
Worked example: After three passes, each with one thought, the context holds the goal plus 3 thoughts, 3 actions and 3 observations: 9 new entries.
repeat until action = finish or steps = B
The two stop conditions every loop needs: the model's own decision that it is done, and a hard budget B that the harness enforces whatever the model says.
Worked example: With B = 6 and a search tool that always comes back empty, the run makes its sixth call and is stopped: 6 of 6 steps, no refund sent.
P(whole run succeeds) = pⁿ
If each of n steps succeeds independently with probability p, the run succeeds only when every step does. Real failures are often linked, so treat this as a rough guide.
Worked example: p = 0.95 and n = 6: 0.95⁶ ≈ 0.74, so about one run in four fails.
A loop does not make a model wiser: one misleading tool result or wrong early thought can steer every later step, and in ReAct's error analysis on HotpotQA, unhelpful search results caused 23% of failures. Budgets and approvals limit the damage but do not make the agent right.
The words you will hear
Agent
Primary sources
- 01ReAct: Synergizing Reasoning and Acting in Language ModelsYao et al. · 2022
- 02Building effective agentsSchluntz and Zhang · 2024
- 03τ-bench: A Benchmark for Tool-Agent-User Interaction in Real-World DomainsYao et al. · 2024
- 04LLM06:2025 Excessive AgencyOWASP Gen AI Security Project · 2025
Connected ideas
Agents and protocols
Agent memory
What an agent keeps in view, what it files away, what it looks up later, and what it does when the facts change.
ExploreAgents and protocols
Tool calling
How a model that can only write text gets real work done: it writes a request, and your software acts on it.
ExploreAgents and protocols
Model Context Protocol (MCP)
One standard plug between AI apps and the tools and data they use, and exactly what travels over it.
ExploreInterpretability and safety
Prompt injection
When text the model reads on your behalf starts giving it orders.
ExploreWant this working on your data?
We design and build the systems these ideas power: retrieval, agents, voice and the models behind them. Start with a free discovery call.