Industry concept · Runtime and execution

Agent loop

Last reviewed
2026-10-28

The agent loop is the repeating cycle at the center of an agent: the model reads the current context, chooses an action, the action runs, and the result is added to the context for the next turn. The loop ends when the model decides the task is complete or an external limit stops it.

Most of what makes an AI agent different from a chat model is this loop. A chat model answers once. An agent answers, acts, looks at what happened, and goes again, for as many turns as the task takes. Anthropic’s description is deliberately plain: agents are typically language models using tools, based on feedback from their environment, in a loop. The plainness hides three properties that matter once agents do real work: any turn that calls a tool can change the world, the context accumulates from turn to turn, and the decision to stop belongs mostly to the model.

One turn, step by step

The pattern was named in the ReAct paper (Yao et al., 2022), which had the model interleave reasoning with actions. Modern frameworks run the same cycle:

context = [system prompt, tool definitions, task]
while True:
    response = model(context)
    if response has no tool calls:
        return response            # the model says it is done
    for call in response.tool_calls:
        result = run(call)         # the action reaches a real system
        context.append(call, result)
    if limit_reached():
        stop()                     # an external limit ends the loop

ReAct drew a distinction that is still the most useful way to read a loop. A thought changes the context and nothing else. An action changes the environment and returns an observation. Everything the agent reasons about stays inside the model’s context. Everything it does goes through a tool call, and the moment the call is dispatched is an execution boundary: the point where a decision becomes an effect.

How loops stop

There are two ways out, and real implementations treat them differently.

The model declares it is done. In the Claude Agent SDK and the OpenAI Agents SDK alike, a response with no tool calls ends the run, and the run is reported as a success. The model’s judgment that the task is complete is accepted as the result.

An external limit is reached. Every framework offers one, but they count different things. The OpenAI Agents SDK counts turns, with a default of ten. Pydantic AI counts model requests, with a default of fifty, and can also cap tool calls, tokens and cost. The Claude Agent SDK can cap turns and dollars, and neither limit is set by default. LangGraph counts graph steps through a recursion limit. “A limit of 25” therefore means different things in different stacks.

ReAct itself shipped with hard caps of five and seven steps and a fallback when the loop ran out. Limits have been part of the pattern from the start, because loops fail in recognizable ways: repeating the same action, never recognizing that the work is finished, or stopping early with a confident answer that was never verified. A study of 1,600 multi-agent traces (Cemri et al., 2025) found step repetition, unawareness of termination conditions and premature termination among the common failure modes. Those rates come from multi-agent systems, but the mechanisms belong to the loop.

Three things the loop implies

Actions happen inside turns, not at the edges of a run. A turn ends either in an answer or in one or more tool calls, and the tool calls are where the loop reaches real systems. Input and output checks that run at the start and end of a run see the request and the answer. They do not see turn fourteen, where the agent wrote to a database. In the OpenAI Agents SDK, for example, input guardrails run only for the first agent and output guardrails only for the agent that produces the final output; checks on individual tool calls are a separate mechanism. Anything meant to apply to what the agent does has to apply at each tool call, in whichever turn it happens.

A limit ends the loop, not its consequences. When a run hits its turn cap, the framework reports an error such as error_max_turns. The actions taken in earlier turns have already happened. A limit is a safety net for runaway cost and time. It is not a review of what the loop did before it stopped.

Accumulation and compaction change the context in different ways. Within a run the context accumulates: each turn appends the model’s output and any tool calls and results, and the whole history is sent again with the next request, which is why later turns cost more than early ones. Compaction is a separate step that some harnesses take when the history nears the context window limit, automatically or when configured: older material is summarized or cleared to make room. Accumulation keeps everything; compaction is where something can be lost. Anthropic’s documentation notes that instructions from early in the conversation may not survive it. An agent can therefore behave differently after compaction than before, with no new instruction from anyone. This is one of the ordinary mechanisms behind agent drift.

An example

An agent is asked to reconcile last quarter’s vendor invoices and report the discrepancies. Turns one to twelve read invoices and ledger entries. At turn thirteen the context is compacted, and the summary keeps the findings but loses the line in the task that said “report, do not correct”. At turn fifteen the agent finds a duplicate payment and, reasoning that fixing it serves the goal, calls the payments tool to issue a reversal. At turn sixteen it returns a tidy summary and no tool calls, and the run ends as a success.

Nothing in the run’s start or end would show the problem. The request was legitimate and the summary was accurate. The consequential decision was made in one turn in the middle, after the instruction that ruled it out had been compressed away.

Where Sentience fits

Because the tool calls inside turns are where agents act, that is where Sentience attaches. Sentience Governor, the free, open-source runtime governance library, records each agent action at the execution boundary: the tool, the operation and the target system. It evaluates the action against the session’s declared scope and the operator’s policy, keeps the declared objective as context, and records the finding. In the example, if the session had declared a scope covering the invoice and ledger systems, the payments call would be recorded as an action on a target outside that scope and flagged when it was attempted. Where the integration reports model usage, the record also attributes tokens to each model turn. Governor is advisory: it records and flags, and the call proceeds.

For teams that need the loop to respond, not only to be recorded, Sentience Runtime Enforcement applies policy at the execution control points in your own infrastructure: course-correcting a routine deviation, pausing a consequential action for a human reviewer, or stopping the run. Which interventions are available depends on the control points integrated with your environment.

Often confused with

An agentic workflow. A workflow is the whole process, which may contain several loops, fixed steps and human checkpoints. The loop is the mechanism inside an agent that repeats until it stops.

An agent harness. The harness is the software that runs the loop, along with context management, tools and permissions. The loop is one of the things a harness does.

Chain-of-thought. Reasoning steps change only the model’s context. A loop turn ends in an action that reaches the outside world.

Where the concept stops

The loop describes how an agent proceeds, not whether it is proceeding well. It establishes that an action was chosen and run at a given turn. It does not establish that the action served the task, that the model’s “done” was true, or that a limit which ended the run undid anything. Those questions need a statement of the task, an evaluation at each turn, and, where the consequences warrant it, the authority to intervene before the next action runs.

Often confused with

Related entries

Bridges

See in practice

Sources

  1. Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models
  2. Anthropic, Building effective agents
  3. Claude Agent SDK, How the agent loop works
  4. OpenAI Agents SDK, Running agents
  5. Anthropic, Effective context engineering for AI agents
Return to the glossary