The agent loop
A while loop with a model call inside. Understanding the loop is understanding agents.
The agent loop, step by step
- userFix the failing test in cart.test.js
- assistanttool_call: read_file("cart.js")
- toolcontents of cart.js (42 lines)
- assistanttool_call: run_tests()
- toolFAIL expected 30, got 33
- assistanttool_call: edit_file(discount before tax)
- toolrun_tests(): 12 passed
- assistantFixed: the discount now applies before tax. All 12 tests pass.
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.
Step 2 of 8
The model replies with a tool call instead of an answer. It wants to look at the code first.
Step 3 of 8
The harness runs the tool and appends the result. The model cannot run anything itself. It only asks.
Step 4 of 8
Same context, plus the file. The whole history is sent again on every call, so the context grows each step.
Step 5 of 8
The test fails. The error message goes into the context. This is the feedback the model needs.
Step 6 of 8
The model reads the failure and proposes a fix. A good tool error tells the model what to try next.
Step 7 of 8
Edit applied. The model checks its own work by running the tests again.
Step 8 of 8
The reply has no tool call, so the loop stops. The harness also stops if a step, cost or time limit is hit.
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
- Send the model the goal, the history and the definitions.
- If the reply is a , run it.
- Append the call and its result to the history.
- Call the model again.
- 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."A new employee is told "find out why the checkout page is slow". They look at one thing, then decide what to look at next based on what they found, repeating until they have the answer. An agent loop is exactly that: look, decide, act, look at the result, decide again.
Check yourself
What ends an agent loop?
Not quite. The loop runs until the model answers or a step, cost or time limit stops it.
The agent calls the same failing tool again and again. What is the best fix?
Not quite. The model needs to learn what went wrong, and the harness must cap retries.
Your loop has no step limit and the model keeps calling tools. What could happen?
Not quite. Without limits, a confused agent can keep going. Cap steps, cost and time.
You got 0 of 3 right.
Go deeper
- ArticleHow to build an agentArticle
Thorsten Ball · A complete agent in a few hundred lines. Read the code.
- Article
Lilian Weng · The planning, memory and tool components around the loop.
- Repo
Hugging Face · A small library where the loop is easy to read.
- Course
Hugging Face · Free course that builds agents step by step.
Spotted a mistake or something unclear? Report it on GitHub.