Agent loop

Also known as: agentic loop

On this page 7

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.

StagePerformed byOutput
Context assemblyHarnessPrompt with history, tools and state
ReasoningModelPlan or chain of thought for the next move
Action selectionModelTool call with arguments or final answer
ExecutionHarness and toolsRaw result or error message
ObservationHarnessResult appended to the context
Termination checkHarnessContinue, stop or escalate
Stages of a single loop iteration

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.

VariantHow it worksBest suited for
Single-step reactive loopDecide and act one tool call at a timeShort tasks with fast feedback
Plan-and-execute loopWrite a plan, then run steps in orderLong tasks with clear structure
Reflection loopCritique own output and retryTasks with checkable quality criteria
Orchestrator-worker loopMain loop delegates to subagentsBroad research or parallel work
Human-in-the-loop cyclePause for approval at key stepsRisky or irreversible actions
Common loop variants and when they fit

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.

ControlTypical settingPurpose
Maximum iterations10 to 100 stepsPrevent endless cycling
Token or cost budgetFixed ceiling per taskCap spending
Wall-clock timeoutMinutes to hoursBound long-running agents
Tool permissionsAllowlist per taskLimit what actions are possible
Approval gatesBefore irreversible actionsKeep a human accountable
SandboxingIsolated container or VMContain side effects of code execution
Essential loop controls

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.

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.

PatternWho decides the next stepNumber of model calls
Single model callNobody, one response onlyOne
Agentic workflowDeveloper-defined code pathFixed or bounded
Agent loopModel at each iterationVariable, until done
Multi-agent systemSeveral loops coordinatingVariable across agents
Agent loop versus related patterns

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.