Lesson 4 of 9 · How It Works
Parallel Tool Calls
Handle several tool calls at once: run independent calls in parallel, return every result with its ID, handle dependent calls and turn parallel calls off.
- 5 min read
- Intermediate
- Reviewed
- Recognise a response that contains several tool calls
- Return all the results together, each with its own ID
- Run independent calls at the same time to save waiting
- Know when calls depend on each other or must not run in parallel
Several calls, one response
When a request needs several pieces of information that don't depend on each other, modern models ask for them all at once: “Compare the weather in Lisbon, Rome and Athens” becomes three weather calls in a single response.
Handling them
import json
# One model response asking for three independent tool calls at once
response_content = [
{"type": "text", "text": "I'll compare the three cities."},
{"type": "tool_use", "id": "t1", "name": "get_weather", "input": {"city": "Lisbon"}},
{"type": "tool_use", "id": "t2", "name": "get_weather", "input": {"city": "Rome"}},
{"type": "tool_use", "id": "t3", "name": "get_weather", "input": {"city": "Athens"}},
]
WEATHER = {"Lisbon": 19, "Rome": 16, "Athens": 21}
SECONDS = {"Lisbon": 0.8, "Rome": 1.1, "Athens": 0.9} # how long each API call takes
calls = [b for b in response_content if b["type"] == "tool_use"]
results = [{"type": "tool_result", "tool_use_id": c["id"],
"content": json.dumps({"city": c["input"]["city"], "temp_c": WEATHER[c["input"]["city"]]})}
for c in calls]
print(f"{len(calls)} tool calls in one response -> {len(results)} results in ONE message:")
for r in results:
print(" ", r["tool_use_id"], r["content"])
one_by_one = sum(SECONDS[c["input"]["city"]] for c in calls)
all_at_once = max(SECONDS[c["input"]["city"]] for c in calls)
print(f"\nRunning them one after another: {one_by_one:.1f} s")
print(f"Running them at the same time: {all_at_once:.1f} s")Two rules:
- Run every call in the response and return every result in one message, each with its own ID. Returning only some of them typically causes an error on the next request.
- Results can be in any order: the IDs, not the positions, link them to their calls.
Because the calls are independent, they can also run at the same time. Three API calls taking 0.8, 1.1 and 0.9 seconds take 2.8 seconds one after another, but only as long as the slowest (1.1 seconds) when run together.
Running them concurrently
In ordinary Python on your own computer (this page's in-browser Python can't run threads), a thread pool runs I/O-bound tool calls side by side:
from concurrent.futures import ThreadPoolExecutor
calls = [b for b in response.content if b.type == "tool_use"]
def run(call):
output = run_tool(call.name, call.input) # validate, run, catch errors
return {"type": "tool_result", "tool_use_id": call.id, "content": json.dumps(output)}
with ThreadPoolExecutor(max_workers=8) as pool:
results = list(pool.map(run, calls)) # same order as calls
messages.append({"role": "user", "content": results})Async code (asyncio.gather) does the same job in async applications.
When calls depend on each other
Some steps need the output of another: you can't get a city's forecast until you know its coordinates. A model handles this by asking in rounds: first find_city, then, after seeing the result, get_forecast. Your loop doesn't need special logic for it; it just keeps going until the model stops asking for tools.
Common mistakes
Work out how much time running independent tool calls in parallel saves.
# How long each tool call takes, in seconds
durations = {"get_weather": 1.2, "get_flights": 2.5, "get_hotels": 1.8}
# The three calls are independent. Print the total time if run one after
# another, and if run at the same time (the slowest one decides), like:
# One by one: 9.9 s
# At the same time: 9.9 s
A response contains three independent tool calls. What should your code send back?
Summary
- Models can request several independent tool calls in one response.
- Run every call and return all results in one message, matched by ID.
- Run independent calls concurrently to cut waiting to the slowest call.
- Dependent calls happen in rounds; turn parallel calls off when order or side effects matter.
Next: writing tool descriptions that models understand.
Finished reading? Tick it off to track your progress.