Sub topic 11
Handling several tool calls in one turn
This chapter assumes you have read the earlier chapters. You know the message roles, tool definitions, the dispatcher run_tool_call, and the basic loop. Here you will handle replies where the model asks for more than one tool at once.
What parallel calls look like
A single assistant message can contain several entries in tool_calls. This happens when the calls do not depend on each other. For "Compare the weather in Oslo and Rome", the model can ask for both cities in the same turn.
[
{"role": "user", "content": "Compare the weather in Oslo and Rome."},
{"role": "assistant", "tool_calls": [
{"id": "call_1", "name": "get_weather", "arguments": {"city": "Oslo"}},
{"id": "call_2", "name": "get_weather", "arguments": {"city": "Rome"}}
]},
{"role": "tool", "tool_call_id": "call_1", "content": "{\"temperature\": 7, \"unit\": \"celsius\"}"},
{"role": "tool", "tool_call_id": "call_2", "content": "{\"temperature\": 18, \"unit\": \"celsius\"}"},
{"role": "assistant", "content": "Rome is 18 degrees Celsius and Oslo is 7, so Rome is 11 degrees warmer."}
]Each call has its own id. Each result must carry the tool_call_id of the call it answers. The id is how the model matches results to requests, so you can return results in any order.
Rules for the results
- Answer every call. Most providers reject a request if an assistant message with tool calls is not followed by a result for each id.
- Send all results before the next model call. Do not call the model after only the first result.
- Keep the results in the conversation, one
toolmessage per call. - If one call fails, return an error result for that id and still return the others.
The loop from the earlier chapter already does this. It iterates over calls and appends one message per call. It works unchanged for one call or five.
Running calls concurrently
The loop above runs calls one after another. If each tool waits on a network request, a thread pool cuts the total time to roughly that of the slowest call.
from concurrent.futures import ThreadPoolExecutor
def run_calls_parallel(calls, max_workers=4):
with ThreadPoolExecutor(max_workers=max_workers) as pool:
outcomes = list(pool.map(run_tool_call, calls))
return [
{
"role": "tool",
"tool_call_id": call["id"],
"content": outcome["content"],
}
for call, outcome in zip(calls, outcomes)
]pool.map returns results in input order, so each result stays paired with its call. In run_agent, replace the inner for loop with messages.extend(run_calls_parallel(calls)).
Common errors
- Missing a result. Dropping a failed call without an error message breaks the next request.
- Mismatched ids. Pairing results by position after reordering them sends the wrong data to the model.
- Unsafe concurrency. Calls in one turn are meant to be independent, but the model can get this wrong. Two calls that write to the same file or database row can conflict when run in parallel.
- Ignoring the step limit. Each model turn counts as one step, however many calls it holds. Count total tool executions too if cost matters.
Limiting parallel calls
Many providers let you turn parallel calls off with a request setting. The name varies, for example a parallel_tool_calls flag. Disable it if your tools have side effects that must be ordered, or if concurrency would be hard to make safe. The model then issues one call per turn and takes more turns.
Exercise
The model returns three tool calls. Your code runs them, but the second one raises an exception. What should you send back before the next model call?
Show answer
Send three tool messages, one for each id. The second carries an error message such as "Tool failed: ..." with the id of that call, and the other two carry their normal results. The model sees the failure and can retry or explain it.