Tool Use & Function Calling

How do you handle parallel tool calls in Claude?

QUICK ANSWER

When Claude decides to call multiple tools in a single turn, the API returns a message block containing multiple content blocks of type 'tool_use'. Iterate through the list, execute them in parallel, and return corresponding tool result blocks.

Node.js Implementation

if (response.stop_reason === "tool_use") {
  const toolResults = await Promise.all(
    response.content
      .filter(block => block.type === "tool_use")
      .map(async (toolCall) => {
        const result = await executeLocalTool(toolCall.name, toolCall.input);
        return {
          type: "tool_result",
          tool_use_id: toolCall.id,
          content: JSON.stringify(result)
        };
      })
  );

  // Send tool results back to Claude in the next turn
  const nextResponse = await anthropic.messages.create({
    model: "claude-3-5-sonnet-20241022",
    max_tokens: 1024,
    messages: [
      ...existingMessages,
      { role: "assistant", content: response.content },
      { role: "user", content: toolResults }
    ]
  });
}
Verified against: Anthropic API Tool Use v1