Claude API Parallel Function Calling Example
When Claude decides it needs to call more than one tool to answer a request, it doesn't do this in separate turns — it returns multiple tool_use blocks in a single response. Your job is to detect all of them, run them (ideally at the same time), and send all the results back together in one tool_result message. That's parallel function calling, and getting the request/response shape right is the main source of confusion for developers trying it for the first time.
This article walks through a complete example: a prompt that triggers two independent tool calls, the code that executes them concurrently with Promise.all, and the follow-up request that returns both results to Claude in the correct format.
What "parallel" actually means here
Claude doesn't call your functions directly — it never executes code. What happens is:
- You send a message with a list of available tools.
- Claude's response contains a
stop_reasonoftool_useand one or moretool_usecontent blocks, each with its ownid,name, andinput. - You run the corresponding functions in your own code.
- You send a new message back containing a
tool_resultblock for everytool_useblock Claude asked for, matched bytool_use_id. - Claude reads all the results and produces a final answer.
The "parallel" part is step 2 (Claude asking for several things at once) and step 3 (you choosing to run them concurrently instead of sequentially). Claude has no visibility into how you execute them — it just expects all results back in the next turn.
Example: weather + currency lookup in one turn
Suppose a user asks: "What's the weather in Tokyo and what's 100 USD in JPY?" A well-designed prompt with two tools will often make Claude request both in a single turn.
1. Define the tools and send the request
{
"model": "claude-opus-4",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get current weather for a city",
"input_schema": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
},
{
"name": "convert_currency",
"description": "Convert an amount from one currency to another",
"input_schema": {
"type": "object",
"properties": {
"amount": { "type": "number" },
"from": { "type": "string" },
"to": { "type": "string" }
},
"required": ["amount", "from", "to"]
}
}
],
"messages": [
{ "role": "user", "content": "What's the weather in Tokyo and what's 100 USD in JPY?" }
]
}
2. Claude's response contains two tool_use blocks
{
"stop_reason": "tool_use",
"content": [
{ "type": "text", "text": "I'll check both of those for you." },
{
"type": "tool_use",
"id": "toolu_01A",
"name": "get_weather",
"input": { "city": "Tokyo" }
},
{
"type": "tool_use",
"id": "toolu_01B",
"name": "convert_currency",
"input": { "amount": 100, "from": "USD", "to": "JPY" }
}
]
}
3. Execute both functions concurrently
async function runTool(block) {
if (block.name === "get_weather") {
return getWeather(block.input.city);
}
if (block.name === "convert_currency") {
return convertCurrency(block.input.amount, block.input.from, block.input.to);
}
throw new Error(`Unknown tool: ${block.name}`);
}
const toolUseBlocks = response.content.filter(b => b.type === "tool_use");
const results = await Promise.all(
toolUseBlocks.map(async (block) => {
const output = await runTool(block);
return {
type: "tool_result",
tool_use_id: block.id,
content: JSON.stringify(output)
};
})
);
Using Promise.all instead of await-ing each call one by one is the whole point: if get_weather takes 400ms and convert_currency takes 300ms, sequential execution costs you 700ms while concurrent execution costs you 400ms. With three or four tool calls in a turn, the gap compounds fast.
4. Send all results back in one message
const followUp = {
model: "claude-opus-4",
max_tokens: 1024,
tools: tools,
messages: [
...previousMessages,
{ role: "assistant", content: response.content },
{ role: "user", content: results }
]
};
Note that results is an array containing both tool_result blocks, sent as a single user message. Claude will not proceed correctly if you split them into two separate messages or omit one — it expects a result for every tool_use_id it issued in that turn.
Common mistakes
- Sending results in separate turns. If Claude asked for two tools, both results must go back in the same message, not two round trips.
- Mismatched
tool_use_id. Each result must reference the exact id from the correspondingtool_useblock — order in the array doesn't matter, the id does. - Running tools sequentially when they're independent. If your tools don't depend on each other's output,
Promise.all(or your language's equivalent) is almost always safe and faster. - Forgetting error handling per tool. If one tool fails, return a
tool_resultwith an error message in the content rather than letting the whole request crash — Claude can often recover and explain the partial failure to the user.
Running this through a hosted API
If you're building this against raw Anthropic credentials, you're responsible for key rotation, streaming the tool-use deltas, and tracking usage per tool call yourself. SubToAPI wraps the same Messages and tool-use behavior behind an HTTPS endpoint with application-scoped keys (sub_live_...), so the pattern above works unchanged — same tools array, same tool_use/tool_result shape — while you get per-key usage metadata and team seats on top. See the tool use docs and the quickstart for the exact request format, or check pricing if you're evaluating options for a team.
FAQ
Does Claude guarantee it will call multiple tools in parallel when possible? No. Claude decides per-request based on the prompt and tool descriptions. Clear, independent tool definitions and a prompt that explicitly needs multiple pieces of information make parallel calls more likely, but it's not something you can force deterministically.
Can I mix streaming with parallel tool use? Yes. In streaming mode you'll receive multiple content_block_start events with type: tool_use, one per parallel call, interleaved with their input deltas. See streaming for how the event sequence differs from a single tool call.
What happens if I only send back one of two requested tool results? Claude will typically error or stall waiting for the missing result — the API expects a tool_result for every tool_use_id from the prior assistant turn before it will continue the conversation.