Skip to content
AI Engineering
Agents

The agent loop

A while loop with a model call inside. Understanding the loop is understanding agents.

Chapter 9: AgentsLesson 3 of 10Level: CoreAbout 15 min

The agent loop, step by step

ModelToolsYou
  • userFix the failing test in cart.test.js

Context sent on the next call: ~14 tokens

Step 1 of 8

You give the agent a goal. The harness starts the loop with that message and the list of tools the agent may use.

Words on this page (10)
agent
: A model run in a loop that chooses its own tools and steps until the task is done.
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
tool use
: The model asks your program to run a function and reads the result.
logarithm
: The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.
harness
: Everything around the model that makes it an agent: prompts, tools, rules, sandboxes, hooks, memory and checks.
trace
: The full record of one run: inputs, model calls, tool calls, outputs, timing and cost.
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
trajectory
: The sequence of steps and tool calls an agent took, not just its final answer.
eval
: A repeatable test of AI output quality, run before and after every change.
human-in-the-loop
: A design where a person approves risky steps.

The steps

  1. Send the model the goal, the history and the definitions.
  2. If the reply is a , run it.
  3. Append the call and its result to the history.
  4. Call the model again.
  5. If the reply is a final answer, stop.

Around the loop, add limits: a maximum number of steps, a cost cap and a timeout. Also every call, so you can see what the did, and give a person a way to interrupt.

What goes wrong

The model picks the wrong tool, repeats the same call, or loses the goal as the history grows. Unclear tool errors make the model retry the same call over and over. Context fills with old results. Fixes are in the : clear tool errors, a step limit, trimming or summarizing old output and checks the agent can run itself.

Try it

Step through the loop in the demo. Watch the context grow with each tool call, and see where the loop stops.

A trace of three turns

Goal: "What is the total for order 8123 including shipping?" Invented counts. The goal plus the tool definitions take 450 tokens.

Turn 1 sends 450 tokens. The model replies with a call to get_order (30 tokens). Your program runs it, and the tool result adds 100 tokens.

Turn 2 sends 450 + 30 + 100 = 580 tokens. The model calls get_shipping (30 tokens) and the result adds 120.

Turn 3 sends 580 + 30 + 120 = 730 tokens. The model writes the final answer and the loop stops.

The new information was about 280 tokens, but the loop sent 450 + 580 + 730 = 1,760. Each turn resends the whole history, so a long run costs far more than the sum of its parts.

What goes wrong: the repeat spiral

The symptom is a log showing the same tool with the same arguments five times in a row, each returning the same result. The model does not see progress and tries again. Confirm it by logging every call and checking for identical pairs of tool name and arguments. A simple fix is for the harness to notice a repeat and either stop the run or return the message "You already tried this and got this result. Try something different."

When the loop is the wrong tool

Read the logs of twenty runs. If every run makes the same three calls in the same order, the model is not deciding anything. Write those three calls as plain code and skip the loop. It will be faster, cheaper and easy to test.

Where this connects

Agents need tests of their own, and the loop needs limits for cost and steps. See Trajectory evals for agents and Human approval.

The code

The loop from the steps above, in Python. It stops when the model answers, and also after 10 steps. Tool results go back in a message with the role "user", because from the model's side your program is the one answering its request. tools is the list of tool definitions and run_tool is your function that runs one, as in the tool-use lesson.

from anthropic import Anthropic

client = Anthropic()  # reads ANTHROPIC_API_KEY from the environment

def run_agent(goal, tools, run_tool, max_steps=10):
    messages = [{"role": "user", "content": goal}]
    for _ in range(max_steps):
        reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=1000, tools=tools, messages=messages)
        messages.append({"role": "assistant", "content": reply.content})
        if reply.stop_reason != "tool_use":
            return reply.content[0].text  # no tool call: this is the final answer
        results = []
        for block in reply.content:
            if block.type == "tool_use":
                try:
                    output = run_tool(block.name, block.input)
                except Exception as e:  # return the error as a result so the model can recover
                    output = f"Error: {e}"
                results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)})
        messages.append({"role": "user", "content": results})
    return "Stopped: step limit reached."

Check yourself

What ends an agent loop?

The agent calls the same failing tool again and again. What is the best fix?

Your loop has no step limit and the model keeps calling tools. What could happen?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page