Topic 3 of 5 · 5 min read
The call, result and continue loop
Goal: Trace a multi-step tool loop and tell where a bad tool result led the final answer astray.
An agent is a loop: the model asks for a tool, the app runs it, the result goes back into the model's context, and the model decides what to do next. It stops when it writes a normal answer instead of another request.
Check yourself · 1 of 3
In a tool loop, what does the model see when it decides its next step?
Why it matters
One tool call is simple. Real tasks need several: search, open a page, run a calculation, then answer. Each step uses what came before. This is the whole mechanism behind agents in Claude Code, ChatGPT, Cursor and similar tools. It also means an early mistake travels forward. The model treats a tool result as true text in its context, so a wrong result can quietly shape every later step and the final answer.
How the loop runs
- The user asks a question.
- The model writes a tool request.
- The app runs it and appends the result to the conversation.
- The model reads everything again, including the new result, and writes either another request or a final answer.
- Steps 2 to 4 repeat until a final answer, a stop rule or a step limit.
The model does not remember anything between steps except what is in this growing context. Long loops fill the context window, and old results can get pushed out or ignored.
Tracing a loop
To find where an answer went wrong, read it step by step and ask at each result: was this correct, and did the next step use it correctly? Errors come from three places: the request had wrong arguments, the tool returned something wrong or empty, or the model misread a good result.
Before and after
Weak review: "The final answer says revenue grew 40 percent. That seems off, so the model must be bad at maths."
Better review: step 2 searched for the report and got last year's figures. Step 3 summed those. Step 4 reported the result. The maths was fine; the search returned the wrong document, and nothing checked it.
What goes wrong
- Empty results treated as facts. A search returns nothing and the model answers "there is no such law".
- Error text taken as data. A tool returns "404 not found" and the model builds on it as if it were content.
- Endless loops. The model repeats the same failing call. Apps need a step limit.
- Lost thread. In a long loop, the original goal drifts and the model solves a neighbouring problem.
For engineers
The loop is a few lines of code. Keep a hard limit and log every step:
messages = [{"role": "user", "content": question}]
for step in range(8): # hard step limit
reply = model.respond(messages, tools=TOOLS)
messages.append(reply.message)
if not reply.tool_calls:
break # a final answer
for call in reply.tool_calls:
result = run_tool(call.name, call.args) # your code runs it
messages.append({"role": "tool", "id": call.id, "content": result})
When a run goes wrong, replay the log and find the first step where a result was wrong.
Examples
The stale document
A user asks: "What is the current return window?" Step 1: the model searches the help centre. Step 2: the result is a page last edited in 2022 that says 14 days. Step 3: the model answers "14 days". The current policy is 30 days. The model read the result correctly; the tool returned an old page. Fix the source, or return the page date so the model can notice.
The repeated failure
An agent calls read_file with config.yml and gets "file not found". It calls the same path five more times. A step limit would end the run, and a rule such as "on an error, list the directory before retrying" would fix it.
Key points
- A tool loop repeats: the model writes a request, the app runs it, and the result is added to the context.
- The model's only memory between steps is the context, so each result shapes what follows.
- A wrong or empty tool result can pass through later steps and end up in a confident final answer.
- To debug, read the steps in order and find the first one where a request or result was wrong.
- Real loops need a step limit, logging and a rule for what to do with errors and empty results.
Common mistakes
- Blaming the model's reasoning when the first bad step was a tool result.
- Running loops with no step limit or cost limit.
- Treating an empty result or an error message as an answer.
- Never logging intermediate steps, so a bad answer cannot be traced.
Check yourself · 2 of 3
An agent's final answer is wrong. The log shows the search returned an outdated page, and the model summarised it correctly. Where did the error start?
Check yourself · 3 of 3
A tool call fails with an error and the agent repeats the same call again and again. What is a sensible safeguard?
Practice items, not counted towards your mastery.
Start here
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
Shows the request, tool_use, tool_result cycle and who runs each step.
- Function calling guide (OpenAI) (opens in a new tab)DocsFree
The same loop in another vendor's words: the model returns a call, your code runs it, you send the output back.
- Function calling (Gemini API docs) (opens in a new tab)DocsFree
A third vendor's version, with function declarations and how results return to the model.
More for this topic
- Toolformer: Language Models Can Teach Themselves to Use Tools (opens in a new tab)PaperFree
The research paper showing a model can learn when to call a calculator, search or other API.