NOTES / HARNESSES

The execution loop

The loop at the centre of every harness: call the model, run the tools it asks for, feed the results back, and stop when it asks for nothing more.

UPDATED 18 NOV 2026#harness#agent-loop#claude-agent-sdk

The loop

Underneath the framework names, nearly every agent is one loop. The Claude Agent SDK documentation describes it directly: the SDK runs the same execution loop that powers Claude Code, where Claude evaluates the prompt, calls tools, receives the results, and repeats until the task is done1.

Diagram: prompt goes to a model call; no tool calls leads to the final answer, tool calls lead to running tools, results are appended to history and the loop returns to the model call

Diagram: my own, following the loop described in 1.

In pseudocode, with the model call as the only LLM step:

def run(prompt, tools, max_turns=20):
    messages = [{"role": "user", "content": prompt}]
    for _ in range(max_turns):
        reply = model(messages, tools=tools)       # the model decides
        messages.append(reply)
        if not reply.tool_calls:                    # nothing asked for: done
            return reply.text
        for call in reply.tool_calls:               # the harness acts
            result = run_tool(call)
            messages.append(tool_result(call, result))
    raise RuntimeError("turn limit reached")

The model never runs anything itself. It requests a tool call, and the harness executes it and reports back. That split is why a harness is a safety and control point: everything the model does in the world passes through code you own.

What the harness adds

The loop itself is short. Tian Pan makes the point that the complexity is not in the loop but in the infrastructure around it, such as tool execution, context management, safety checks, error recovery and state persistence2. Each time round the loop a real harness also has to:

  • check permissions before a tool runs, and run hooks at fixed points;
  • keep the history inside the context window, for example by compaction (see Compaction and summarisation);
  • handle tool errors and send them back to the model as results it can react to;
  • save state so a crash does not lose the run;
  • record a trace of each step.

Stopping

An agent that cannot stop is a cost problem. A well-built loop ends for one of several reasons:

Diagram: five stop reasons for an agent loop: no tool calls, turn limit, budget exceeded, error or refusal, and interruption

Diagram: my own.

The normal ending is that the model’s reply contains no tool calls. The others are guard rails the harness imposes: a maximum number of turns, a spending or token budget, an error or refusal, and an interruption by the user or a hook. Always set the limits explicitly. Anthropic’s advice on agents includes including stopping conditions, such as a maximum number of iterations, to keep control3. More on this in Stopping conditions and budgets.

Workflows versus this loop

Workflows vs agents distinguishes fixed pipelines from agents. The execution loop is what makes something an agent: the model chooses the next step each time round, instead of the code following a predefined path3.

Sources

  1. [1]How the agent loop works — Anthropic, Claude Agent SDK docsPARAPHRASED
  2. [2]The Anatomy of an Agent Harness — Tian Pan · tianpan.co · 2026PARAPHRASED
  3. [3]Building effective agents — Erik Schluntz and Barry Zhang · Anthropic · 2024PARAPHRASED

© Ashish Kumar. All rights reserved. Quoted and cited material belongs to its owners. How I attribute sources.

Product, company and framework names are trademarks of their respective owners. Mentioning them does not imply affiliation or endorsement.