StudyVisuallyStart studying
Part 03 · Tool calling · Card 9 of 10

Parallel tool calls

The model can request several tool calls in one reply. Run them all, then send back one tool message per call.

Last updated

Analogy

Promise.all for the model. Why fetch three orders one by one when you can ask for all three at once?

Code example

parallel.tsTypeScript
const results = await Promise.all(
message.tool_calls.map(async (call) => ({
role: "tool" as const,
tool_call_id: call.id,
content: JSON.stringify(await runTool(call)),
})),
);
messages.push(message, ...results);

In an agent

Parallel calls cut the number of loop rounds, which saves both time and tokens.

Questions people ask

Can the model call several tools at once?

Yes. One reply can contain several entries in tool_calls, for example fetching three orders. Run them together with Promise.all, then append one tool message per call, each with its own tool_call_id, before calling the model again. This saves loop rounds, which saves time and tokens.

What happens if I skip one of the parallel tool results?

The API expects a tool message for every tool call id in the assistant's message. If one is missing, the next request is usually rejected as invalid. If a tool fails, still send a tool message for it, containing a short error, so the model knows what happened and can decide what to do.