Topic 2 of 5 · 5 min read
Running the tool call loop to completion
Goal: Implement a loop that runs requested tools, returns results with matching call ids and stops on a final answer or a step limit.
A model never runs your tool. It asks for a call, your code runs it, and you send the result back; this repeats until the model answers in plain text. Getting that loop right is most of tool use.
Check yourself · 1 of 3
After the model requests a tool call, what must your code do?
Why it matters
One request is often not enough. The model may look up an order, then a refund policy, then answer. A loop that stops after the first tool request returns half an answer. A loop with no limit can run forever and spend money.
How the loop works
- Send the messages and the tool definitions.
- Read the response. If it holds one or more tool requests, the model is not done. On Claude the stop reason is
tool_use; on OpenAI the output holds function calls. - Run each requested tool with the arguments the model gave you.
- Append two things to the history: the model's own tool request message, then your result, tied to the request by its call id. The id is how the model knows which result belongs to which request.
- Send again with the longer history.
- Stop when the response has no tool requests, or when you hit a step limit you set, for example 8 rounds.
Before and after
Weak: a single pass.
resp = client.messages.create(model=MODEL, tools=TOOLS, messages=msgs)
print(resp.content[0].text) # crashes when content[0] is a tool request
Better: a bounded loop.
for step in range(8):
resp = client.messages.create(model=MODEL, tools=TOOLS, messages=msgs)
if resp.stop_reason != "tool_use":
break
msgs.append({"role": "assistant", "content": resp.content})
results = []
for block in resp.content:
if block.type == "tool_use":
out = run_tool(block.name, block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": out})
msgs.append({"role": "user", "content": results})
else:
raise RuntimeError("tool loop hit the step limit")
The names differ by provider, but the shape is the same: request, run, return with the id, repeat.
What goes wrong
- Dropping the assistant message. If you send the result without the model's own request, the API rejects the history or the model loses the thread.
- A wrong or missing call id. The provider returns an error, or the model attaches a result to the wrong request.
- No step limit. A model that keeps calling the same tool loops until your budget or timeout ends it.
- Reading only the first block. A reply can mix text and several tool requests. Walk every block.
Examples
Two rounds for one question
The user asks, "Can I return order 5521?" Round one: the model requests get_order(5521). Your code returns the order. Round two: the model requests get_return_policy(category="electronics"). Your code returns the policy. Round three: the response has no tool request, only the final answer. Three API calls, one answer.
A loop stopped by its limit
A model keeps calling search_docs with slightly different queries and never answers. At step 8 the code stops, logs the last three queries and returns a plain message to the user. Without the limit, that request would have kept going until a timeout.
Key points
- The model asks for a tool call; your code runs it and returns the result.
- Every result goes back with the call id of the request it answers.
- Append the model's own tool-request message to the history before the results.
- Loop until the response has no tool requests, and never without a step limit.
- Check every content block, because one reply can hold text and several tool requests.
Common mistakes
- Treating the first response as final and printing the first content block.
- Sending the tool result without the assistant message that requested it.
- Using a new id, or no id, on the result instead of the one from the request.
- Writing
while Truewith no step limit or token budget.
Check yourself · 2 of 3
What is the call id on a tool result used for?
Check yourself · 3 of 3
A loop keeps calling the same search tool and never answers. What should the code have?
Practice items, not counted towards your mastery.
Start here
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
Anthropic's reference for tool definitions, the tool_use and tool_result blocks, tool_choice and parallel calls.
- Function calling guide (OpenAI) (opens in a new tab)DocsFree
OpenAI's guide to function definitions, the call loop, parallel calls and tool_choice.
- Function calling (Gemini API docs) (opens in a new tab)DocsFree
Google's guide to declaring functions and returning function responses to Gemini.