Glossary
Agent loop
The repeating cycle in which an AI agent calls a model, runs the tools the model asks for, adds the results to its context and goes again until the task is done.
An agent loop is the repeating cycle that drives an AI agent. The agent sends the goal and its current context to a language model, the model either answers or asks for a tool to be run, the agent runs that tool and adds the result to the context, and the cycle repeats until the model produces a final answer or a limit is reached. Almost every agent, whatever agent framework it uses, is built around some version of this loop.
How the agent loop works
Each pass through the loop has four steps. First the agent assembles the context: the system prompt, the task, the conversation so far and any retrieved material. Second it calls the model, which returns either text or a structured request to use a tool through function calling. Third, if a tool was requested, the agent’s code runs it, whether that is a search, an API call or a file edit, which is the mechanism described under tool use. Fourth it appends the tool result to the context and starts again.
The model decides what to do at each step; the surrounding code, often called the agent harness or agent runtime, decides how the steps are executed and when to stop. The ReAct pattern made this structure popular by having the model alternate between reasoning and acting, and most modern loops are descendants of it.
How an agent loop stops
A loop needs clear exit conditions, or a confused agent can call tools indefinitely. The usual ones are below, and production agents normally combine several.
Stop condition | What it checks |
|---|---|
Final answer | The model returns a response with no tool request |
Iteration limit | A maximum number of passes through the loop |
Budget | A ceiling on tokens, cost or time for the run |
Done tool | The model calls a tool that marks the task complete |
Guardrail or approval | A check fails, or a person must approve the next action |
Guardrails and human-in-the-loop checkpoints sit inside the loop rather than outside it: they pause or end a run before a risky action, not after.
Planning, state and self-correction
A bare loop reacts one step at a time. Harder tasks add structure on top. Agent planning and task decomposition break the goal into steps before acting. An agent scratchpad gives the agent somewhere to keep notes and a to-do list as it works, and agent state records where the run has got to. Self-correction lets the agent check a result and retry when something fails. In multi-agent systems, each subagent runs its own loop and reports back to a coordinator.
Why the loop gets expensive
Every pass resends the whole context, and every tool result makes that context longer. A loop that runs twenty times can send the same early messages to the model twenty times, so input tokens grow much faster than the useful work. Long contexts also hurt accuracy, the effect described under context rot and lost in the middle. Common fixes are context compression, trimming old tool output, and moving durable facts out of the context window into agent memory that the loop queries when it needs them. The anatomy of a 15,000-token request breaks down where those tokens go.
Long loops also need to survive crashes and restarts. Recording each step makes a run resumable, which is the idea behind durable execution and a requirement for long-running agents. Recording each step also gives you agent tracing, so you can see which tool call went wrong.
Agent loops and Exabase
Exabase Workers run an agent loop for you on Exabase infrastructure. A Worker takes a prompt and works through it with built-in tools for searching and reading resources in its Base, writing notes, organising files, managing tasks and searching the web, plus scratchpad-todo-write and scratchpad-todo-read for planning within a run. Each run is stored as a conversation with every tool call, which you can read through the run messages endpoint; the About Workers guide covers setup.
If you run your own loop, Exabase can be the tools it calls. Memory search returns the few facts relevant to the current step instead of the whole history, and Deep Search returns the passage that answers a question rather than whole documents, which keeps each pass short. The reduce token costs page describes the approach, and the token cost calculator estimates the saving for your numbers.
FAQs
What is an agent loop?
An agent loop is the cycle an AI agent repeats to complete a task: call the model, run any tool it asks for, add the result to the context and call the model again, until it gives a final answer or hits a limit.
What is the difference between an agent loop and a workflow?
In a workflow the developer fixes the sequence of steps in advance. In an agent loop the model chooses the next step each time, based on what earlier steps returned. Agents vs workflows covers when to use each.
How do you stop an agent loop from running forever?
Set an iteration limit and a token or cost budget, and give the model a clear way to signal completion. Most frameworks support all three.
Why do agent loops use so many tokens?
Each pass resends the accumulated context, including every earlier tool result. Trimming old output and retrieving only relevant facts from memory keeps each call small.
Is ReAct the same as an agent loop?
ReAct is one well-known form of agent loop, in which the model writes a reasoning step before each action. Most current agents follow the same pattern, usually with native tool calls instead of parsed text. See ReAct agent.
Do I need a framework to build an agent loop?
No. A basic loop is a few dozen lines of code around a model API. Frameworks add tracing, state, retries and multi-agent support; the best AI agent frameworks roundup compares them.
Related pages: ReAct agent, Tool use, Agent planning, Agent state, Agent harness, Background agents, Context rot, Workers, Coding assistants use case, CrewAI vs LangGraph, Self-correction, Long-running agents.