Lesson 7 / 25

tool_choice and Parallel Calls

Control whether tools are used and handle several tool calls returned in one reply.

Steering and batching

In the Claude API, tool_choice controls tool use: auto lets the model decide, any forces it to use some tool, tool forces a specific tool, and none disables tools. The model may also return several tool_use blocks in one reply when the calls are independent; you run them all and send back one tool_result per block, matched by id, in a single user message.

Returning parallel results

Each result keeps the id of its request, so order does not matter. This ran as shown.

calls = [("c1", "get_balance", {"account_id": 1}),
         ("c2", "get_balance", {"account_id": 2})]
results = [{"tool_use_id": cid, "content": call_tool(n, a)["content"]}
           for cid, n, a in calls]
print(results)

Output:

[{'tool_use_id': 'c1', 'content': 'account 1: 1500'}, {'tool_use_id': 'c2', 'content': 'account 2: 1500'}]

Run independent calls concurrently

If three tool calls do not depend on each other, run them with threads or asyncio to cut wait time. Calls that change shared state may need to run in order.

Quick check: How are parallel tool results matched to requests?

  • By tool_use id
  • By the order they finish
  • By alphabetical name
  • By file size
Answer

By tool_use id — Each tool_result carries the tool_use_id of the call it answers.