Skip to content

The Agentic Loop: Turns, Stop Conditions, Compaction, and Budgets

Every agent runs a loop. The model gets the context, replies with text and/or tool calls, the harness runs the tools and appends the results, and the model goes again. The loop ends when the model replies without calling a tool, or when the harness stops it.

That description has not changed since ReAct. What has changed is how long the loop runs. In 2024 a typical agent run lasted a few turns. In 2026, coding and research agents routinely run for hours, and the longest published runs last days. At that length the interesting questions are no longer “how do I call a tool” but:

  • What exactly is one turn, and what does it cost?
  • What are all the ways the loop can stop, and do I handle each one?
  • What happens when the context window fills up?
  • How do I cap spend without cutting the agent off mid-thought?
  • How do I know the agent actually finished?

This page answers each, with code. For the runtime around the loop (permissions, sandboxing, hooks, subagents), see What Is an Agent Harness?.

Anatomy of one turn

The Claude Agent SDK documentation defines a turn as “one pass through the loop”:

  1. Assemble context: system prompt, tool definitions, instruction files, conversation so far.
  2. Call the model. It returns text, one or more tool calls, or both. On current models it usually also thinks first; that reasoning happens inside the call.
  3. Check why it stopped (stop_reason). This step is often skipped, and skipping it causes bugs.
  4. Run the tools. Calls that only read can run in parallel; calls that change state should run one at a time.
  5. Append results: the assistant message as returned, then all tool results together in one user message.
  6. Decide whether to continue. Check limits, budget and context size.

Two rules from step 5 are easy to get wrong:

  • Append the whole assistant message, not just its text. It contains the tool-call blocks, thinking blocks and (if enabled) compaction blocks the API needs to see again on the next request.
  • Return every tool result from one turn in a single message. Splitting them across messages works, but it teaches the model to stop making parallel calls.

Every way a loop stops

StopWhere it comes fromWhat to do
Model finished (end_turn)ModelRead the final text. Optionally verify it (see below)
Model wants tools (tool_use)ModelRun them, append results, continue
Paused (pause_turn)API, during long server-side tool useAppend the response as-is and call again; nothing to execute
Hit max_tokensAPIThe reply was cut off. Raise max_tokens or stream; don’t run a half-written tool call
Refusal (refusal)Safety classifiersRead stop_details; fall back or report. Don’t treat it as success
Turn limitHarnessReport “ran out of turns” with what was done so far
Spend/token budgetHarness or APIReport what was done; resume with a new budget if worthwhile
Wall-clock timeoutHarnessSame; long tasks should checkpoint progress to disk
Tool or API errorEnvironmentReturn tool errors to the model; retry transient API errors with backoff

A loop that only checks end_turn will spin forever on max_tokens, or treat a refusal as a finished answer. Handle each case on purpose.

A manual loop that handles every stop

This is the loop written by hand against the Anthropic Messages API, using claude-opus-5-5. Opus 5.5 always uses adaptive thinking and defaults to medium effort, so the example sets high effort explicitly for agentic work.

import json
import subprocess
import anthropic
client = anthropic.Anthropic()
TOOLS = [
{
"name": "run_tests",
"description": "Run the project's test suite and return the last 4,000 characters of output. "
"Use after every code change. Takes ~30 seconds.",
"input_schema": {
"type": "object",
"properties": {"path": {"type": "string", "description": "Test file or directory; '.' for all tests"}},
"required": ["path"],
"additionalProperties": False,
},
"strict": True,
},
{
"name": "read_file",
"description": "Read a UTF-8 text file from the repository and return its contents.",
"input_schema": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
"additionalProperties": False,
},
"strict": True,
},
]
def execute(name: str, args: dict) -> str:
if name == "run_tests":
out = subprocess.run(["pytest", args["path"]], capture_output=True, text=True, timeout=300)
return (out.stdout + out.stderr)[-4000:]
if name == "read_file":
with open(args["path"], encoding="utf-8") as f:
return f.read()
raise ValueError(f"unknown tool {name}")
def run_agent(task: str, max_turns: int = 40) -> str:
messages = [{"role": "user", "content": task}]
for turn in range(max_turns):
response = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
output_config={"effort": "high"},
# If a safety classifier declines, re-run on a fallback model chosen by refusal category.
betas=["server-side-fallback-2026-07-01"],
fallbacks="default",
tools=TOOLS,
messages=messages,
)
if response.stop_reason == "refusal":
category = response.stop_details.category if response.stop_details else None
return f"Stopped: request declined (category: {category})"
if response.stop_reason == "max_tokens":
return "Stopped: reply hit max_tokens. Raise the limit or stream the response."
# Always append the full content: tool calls, thinking and compaction blocks included.
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason == "pause_turn":
continue # nothing to execute; send it back to let the model resume
if response.stop_reason == "end_turn":
return "".join(b.text for b in response.content if b.type == "text")
# stop_reason == "tool_use": run every call, return all results in ONE message.
results = []
for block in response.content:
if block.type != "tool_use":
continue
try:
output = execute(block.name, block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
except Exception as exc: # the model can usually recover if it sees the error
results.append({"type": "tool_result", "tool_use_id": block.id,
"content": f"Error: {exc}", "is_error": True})
messages.append({"role": "user", "content": results})
return f"Stopped: reached {max_turns} turns without finishing."

What to notice:

  • Every exit returns a distinct reason, so your logs say why a run ended.
  • strict: True on each tool guarantees the arguments match the schema.
  • fallbacks="default" reruns a refused request on a fallback model the API picks by refusal category. If the fallback also refuses, stop_reason is still refusal.
  • Tool exceptions become is_error results instead of crashing the loop.

The same loop with the Tool Runner

If you don’t need that much control, the Anthropic SDK’s Tool Runner (beta) runs the loop for you. You write the tools as typed functions:

import anthropic
from anthropic import beta_tool
client = anthropic.Anthropic()
@beta_tool
def read_file(path: str) -> str:
"""Read a UTF-8 text file from the repository.
Args:
path: Path relative to the repository root.
"""
with open(path, encoding="utf-8") as f:
return f.read()
runner = client.beta.messages.tool_runner(
model="claude-opus-5-5",
max_tokens=16000,
tools=[read_file],
messages=[{"role": "user", "content": "Summarise what src/app.py does."}],
)
for message in runner: # one BetaMessage per turn; stops when Claude stops calling tools
print(message.stop_reason)

The same loop with the Claude Agent SDK

The Claude Agent SDK runs the full Claude Code loop, with built-in file, shell and search tools. You set limits as options and read the outcome from the final ResultMessage:

import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage
async def main():
options = ClaudeAgentOptions(
allowed_tools=["Read", "Grep", "Glob", "Bash(pytest *)"],
permission_mode="acceptEdits",
max_turns=60,
max_budget_usd=10.00, # covers subagent spend too
effort="high",
)
async for message in query(prompt="Fix the failing test in tests/test_dates.py", options=options):
if isinstance(message, ResultMessage):
# subtype: success | error_max_turns | error_max_budget_usd | error_during_execution | ...
print(message.subtype, message.num_turns, message.total_cost_usd)
if message.subtype == "success":
print(message.result)
asyncio.run(main())

Parallel tool calls

Current models often request several tools in one turn, for example reading five files at once. Run independent read-only calls concurrently. Run calls that change state (edits, shell commands, writes) sequentially, in the order given. The Claude Agent SDK applies exactly this rule by default. Custom tools run sequentially unless marked read-only.

When the context window fills

A 1M-token window sounds endless until an agent reads a large codebase or runs for a day. You have three tools, best used together:

1. Server-side compaction. The API summarizes earlier context once input passes a trigger threshold (150K tokens by default) and returns a compaction block that replaces the old history on the next request. It is a beta feature on current Claude models:

response = client.beta.messages.create(
betas=["compact-2026-01-12"],
model="claude-opus-5-5",
max_tokens=16000,
tools=TOOLS,
messages=messages,
context_management={"edits": [{"type": "compact_20260112"}]},
)
messages.append({"role": "assistant", "content": response.content}) # keeps the compaction block

The Claude Agent SDK compacts automatically, emits a compact_boundary system message when it does, and fires a PreCompact hook first, which you can use to archive the full transcript.

2. Clearing old tool results. Context editing (clear_tool_uses_20250919) removes stale tool outputs while keeping a record that the calls happened. It is cheaper than summarizing and loses less. Use it when most of the context is old file reads.

3. Subagents. Hand read-heavy sub-tasks to a subagent with its own window. Only its final report enters the main context.

Budgets: stopping gracefully instead of abruptly

A turn limit stops the loop from outside, and the model doesn’t know it is coming. Two newer mechanisms let the model pace itself:

  • Task budgets (Anthropic API, beta): give the model a token ceiling for the whole agentic task. The server shows it a countdown, so it can wrap up and report rather than be cut off. Unlike max_tokens, the model knows about it.
  • Spend caps (Claude Agent SDK max_budget_usd, OpenAI and Managed Agents session budgets): a hard dollar limit enforced by the harness. The run ends with a distinct result type (error_max_budget_usd), so you can tell “out of money” from “failed”.
with client.beta.messages.stream(
model="claude-opus-5-5",
max_tokens=128000,
output_config={"effort": "high", "task_budget": {"type": "tokens", "total": 200000}},
betas=["task-budgets-2026-03-13"],
tools=TOOLS,
messages=messages,
) as stream:
response = stream.get_final_message()

Judge cost per completed task, not per request. An agent at lower effort that needs three retries is not cheaper than one at higher effort that finishes first time.

Verification: “done” is a claim, not a fact

When the loop ends on end_turn, the model has only said it is done. Long-running agents need a check that doesn’t rely on the agent’s own word:

  • Executable checks: tests, type checker, linter, build. Run them at the end in your code, not as an optional tool.
  • A reviewer subagent with a fresh context and a narrow brief (“find correctness bugs in this diff”).
  • A rubric grader: a separate model call that scores the output against explicit criteria and sends it back for another pass if it fails.
  • A human approval step for anything irreversible.

If the check fails, append its output to the conversation and continue the loop. That is still one agent run, not a new one.

Failure modes to watch in long loops

SymptomLikely causeFix
Same tool call repeated with same argumentsTool result unhelpful or ambiguousReturn clearer errors; detect repeats in the harness and break
Agent “forgets” an early instructionInstruction lived in chat history and was compacted awayMove it to an instruction file or a hook
Declares success, tests still failNo verification stepRun checks in the harness after end_turn
Costs spike on some runsUnbounded loop, huge tool outputsSpend cap, truncate tool output, clear old results
Stops making parallel callsTool results split across messagesReturn all results from one turn in one message
Edits files it shouldn’tPermissions too broadNarrow allow rules; PreToolUse deny hook; sandbox

Sources

Continue reading