An agent loop, also called an agentic loop, is the control cycle in which a language model repeatedly decides on an action, executes it through a tool, observes the result and feeds that result back into its next decision until the task is complete or a stopping condition is reached. Among AI agents, it is the mechanism that turns a single-response model into a system that can pursue multi-step goals.
The loop itself is ordinary code, often only a few dozen lines, that sits around the model. What makes it powerful is that the model chooses the next step at runtime instead of following a path fixed in advance, which distinguishes an agent from a predefined agentic workflow.
How the agent loop runs step by step
Each iteration of an agent loop follows the same basic pattern of perceive, reason, act and observe. The surrounding program, often called the agent harness, sends the model the conversation so far together with a list of available tools described by a tool schema. The model replies either with a final answer or with one or more structured tool calls.
If the reply contains tool calls, the harness executes them outside the model, for example by running a search, reading a file or calling an API. It appends each tool result to the context as an observation and calls the model again. The loop ends when the model returns a response without any tool call, or when the harness enforces a limit.
The table below breaks one iteration into its stages and shows who is responsible for each.
| Stage | Performed by | Output |
|---|---|---|
| Context assembly | Harness | Prompt with history, tools and state |
| Reasoning | Model | Plan or chain of thought for the next move |
| Action selection | Model | Tool call with arguments or final answer |
| Execution | Harness and tools | Raw result or error message |
| Observation | Harness | Result appended to the context |
| Termination check | Harness | Continue, stop or escalate |
This pattern was popularized by ReAct prompting, which interleaves written reasoning with actions. Modern models with native tool calling perform the same cycle with structured outputs rather than parsed text, which makes the loop far more reliable to implement.
Variants of the agent loop
Agent loops differ mainly in how much planning happens before acting and in how many models share the work. The simplest variant decides one step at a time, while more structured variants separate planning, execution and critique.
| Variant | How it works | Best suited for |
|---|---|---|
| Single-step reactive loop | Decide and act one tool call at a time | Short tasks with fast feedback |
| Plan-and-execute loop | Write a plan, then run steps in order | Long tasks with clear structure |
| Reflection loop | Critique own output and retry | Tasks with checkable quality criteria |
| Orchestrator-worker loop | Main loop delegates to subagents | Broad research or parallel work |
| Human-in-the-loop cycle | Pause for approval at key steps | Risky or irreversible actions |
These variants can be combined. A coding agent, for example, may plan a change, run a reactive loop to edit and test files, apply Reflexion-style self-critique after a failed test, and still require human approval before pushing code to a shared repository.
Controls that keep the loop safe and bounded
An unbounded agent loop can run indefinitely, waste budget or take harmful actions, so every production harness adds explicit controls. These controls live in the code around the model, not in the prompt, because a prompt instruction can be ignored while code cannot.
| Control | Typical setting | Purpose |
|---|---|---|
| Maximum iterations | 10 to 100 steps | Prevent endless cycling |
| Token or cost budget | Fixed ceiling per task | Cap spending |
| Wall-clock timeout | Minutes to hours | Bound long-running agents |
| Tool permissions | Allowlist per task | Limit what actions are possible |
| Approval gates | Before irreversible actions | Keep a human accountable |
| Sandboxing | Isolated container or VM | Contain side effects of code execution |
Long loops also face a context window limit. As observations accumulate, the harness must apply context compaction, summarizing or dropping older steps, or store information in external agent memory. Poor compaction is a frequent reason why an agent forgets earlier decisions and repeats work.
Agent checkpointing saves the loop state after each step so that a crashed or paused run can resume. Together with agent observability, which records every model call and tool result as a trajectory, it makes long tasks debuggable and auditable.
How the agent loop differs from related patterns
The agent loop is often confused with neighboring concepts, but the distinction rests on who controls the flow. In a workflow the developer fixes the sequence; in an agent loop the model decides it.
| Pattern | Who decides the next step | Number of model calls |
|---|---|---|
| Single model call | Nobody, one response only | One |
| Agentic workflow | Developer-defined code path | Fixed or bounded |
| Agent loop | Model at each iteration | Variable, until done |
| Multi-agent system | Several loops coordinating | Variable across agents |
Predictability decreases as autonomy increases. For well-understood processes, a workflow is cheaper and easier to test, and an agent loop is justified only when the path to the goal cannot be specified in advance.
Common mistakes when building agent loops
Most failures of agent loops come from the environment around the model rather than the model itself. Experienced practitioners spend more time on tools, error messages and stopping rules than on prompt wording.
- Vague tool descriptions: the model misuses a tool it does not understand, so schemas need precise names, parameters and examples.
- Swallowed errors: returning an empty result instead of a clear error message prevents the model from correcting itself.
- Oversized observations: dumping whole files or web pages into context crowds out the task and raises cost.
- No termination rule: relying on the model alone to stop leads to repeated loops on the same failed action.
- Too many tools: dozens of overlapping tools increase wrong selections; a smaller, distinct set works better.
The short version
An agent loop is how an AI assistant works through a job one step at a time. It thinks about what to do. It does one thing, such as opening a file. Then it looks at the result and picks the next step.
It is like a cook tasting a dish. Taste, adjust, taste again, and stop when it is right. The loop runs until the goal is reached or a limit says stop. That is why good agents have clear limits on time, cost and actions.
Where it makes a difference
A development team uses a coding agent to fix a failing test in a repository of about 200,000 lines. The developer gives one instruction: make the date-parsing test pass without changing its expectations.
In the first iterations, the agent searches the code for the parser, reads two files and runs the test suite in a sandbox. The observation shows a time zone error, so it edits one function and runs the tests again. One unrelated test now fails, which the agent notices from the output and corrects in the next step.
After 14 iterations and about six minutes, all tests pass and the agent stops, presenting a three-line change for review. The harness had a limit of 50 iterations and required human approval before any commit, so the developer reviewed the diff and merged it. A task that would have taken an hour of tracing was reduced to a short review.