Skip to content
mentoor

Topic 4 of 5 · 4 min read

Handling parallel tool calls in one turn

Goal: Execute several tool calls from one model turn concurrently and return every result, matched by id, in one reply.

A model can request several tools in one response; run them all, then return every result together in one reply, each tied to its own call id. If you answer only the first, the others stay open and the API rejects the next request or the model works with a gap.

Check yourself · 1 of 3

A response contains three tool requests. What must your next request contain?

Choose one answer

Why it matters

Ask "Compare the weather in Cairo, Oslo and Lima" and a good model sends three get_weather requests at once. Run them one after another and you wait three times as long. Run them concurrently and the wait is about the slowest single call. Parallel calls also change what your loop must do: handle a list of requests, not one.

How to do it

  1. Collect every tool request in the response, not just the first. Each has its own id.
  2. Check that they are independent. The model only requests calls together when it thinks none needs another's output. It can be wrong, so make sure running them at the same time is safe.
  3. Run them concurrently with your language's tools: Promise.all in JavaScript, asyncio.gather in Python, a thread pool for blocking calls. Set a timeout on each.
  4. Return all results in one message, each with the id of its request. Order does not matter, ids do. On Claude, put every tool_result block in the same user message. OpenAI takes one output per call id.
  5. Handle partial failure. If one of three calls fails, return two successes and one error result. Do not drop the turn.
  6. Limit concurrency when the tools hit a rate-limited service.

Before and after

Weak: sequential, and only the first request is answered.

const first = toolCalls[0];
const out = await runTool(first.name, first.input);
messages.push({ role: "user", content: [{ type: "tool_result", tool_use_id: first.id, content: out }] });

Better: all requests, concurrent, one reply.

const results = await Promise.all(
  toolCalls.map(async (c) => {
    try {
      const out = await runTool(c.name, c.input);
      return { type: "tool_result", tool_use_id: c.id, content: out };
    } catch (e) {
      return { type: "tool_result", tool_use_id: c.id, content: String(e.message), is_error: true };
    }
  })
);
messages.push({ role: "user", content: results });

What goes wrong

  • Answering only some requests. Open ids cause an API error or confused follow-ups.
  • Splitting results across messages. Send them together in one reply.
  • Running dependent calls at once. Writing to a record while reading it can give stale data.
  • Letting one failure cancel the rest. Catch each call on its own.

Examples

Three lookups in the time of one

A travel assistant gets three get_flight_price requests, each taking about 1.2 seconds. Run in sequence, the turn takes about 3.6 seconds. Run with Promise.all, it takes about 1.2 seconds, and the results go back in one message.

One call fails, two succeed

Of three get_stock requests, one ticker does not exist. The reply holds two normal results and one error result for the bad ticker. The model reports the two prices and says the third symbol was not found.

Key points

  • One response can contain several tool requests, each with its own id.
  • Return a result for every request, in a single reply.
  • Run independent calls concurrently to cut waiting time to the slowest call.
  • Match results by id; their order does not matter.
  • Catch each call separately so one failure does not lose the others.

Common mistakes

  • Reading only the first tool request and ignoring the rest of the response.
  • Sending each result as its own message instead of one reply.
  • Running calls at the same time that depend on each other's side effects.
  • Using one try/catch around all calls, so one failure drops every result.

Check yourself · 2 of 3

Why run independent parallel tool calls concurrently?

Choose one answer

Check yourself · 3 of 3

One of three parallel calls throws an error. What should the reply contain?

Choose one answer

Practice items, not counted towards your mastery.

Start here