Agent loop
An agent loop is the repeating cycle at the heart of an AI agent: the model asks for a tool call, the application runs it and returns the result, and the model decides what to do next.
Also called: agentic loop, tool-use loop
By Divya Sharma · Checked Nov 15, 2026 · How we review
A language model cannot run code or call an API by itself. It can only produce text, including a structured request to use a tool. The agent loop is the code around the model that turns those requests into actions.
How it works
1. Send the model the task and a list of tools. 2. The model replies with one or more tool calls, or with a final answer. 3. If it asked for tools, the application runs them and adds the results to the conversation. 4. Send the updated conversation back and repeat from step 2.
In the Claude API, the loop keeps going while the response's stop reason is tool_use and exits on any other stop reason. Each tool call is a round trip, so a task that needs ten tool calls means at least ten requests. The details of each request are covered under tool calling.
What surrounds the loop
The loop itself is short. Most of the engineering is in what wraps it: limits on the number of turns, error handling when a tool fails, approval prompts for risky actions, and ways to keep the conversation inside the context window as results pile up. That wrapper is often called the agent harness.
Related patterns
The ReAct pattern is one way to structure the loop, alternating reasoning text with actions.
Related terms
- AI agent
- An AI agent is a system in which a language model decides its own next steps: it calls tools, reads the results and repeats until the task is done, instead of following a fixed script.
- Tool calling
- Tool calling, also called function calling, is a model API feature where you describe functions with a schema, the model replies with a structured request to call one, and your code runs it and returns the result.
- Agent harness
- An agent harness is the software around a model that turns it into a working agent: the loop, tools, system prompt, permissions, context management and any files used to carry progress between sessions.
- Context window
- A context window is the maximum amount of text, counted in tokens, that a language model can consider at once, covering the system prompt, conversation, documents, tool results and the response it writes.
- ReAct (reasoning and acting)
- ReAct (reasoning and acting) is a prompting pattern in which a language model alternates between writing a reasoning step, taking an action such as a search or tool call, and reading the result before its next step.