Tool Use & Function Calling
From the course Anthropic Claude API Deep Dive
Claude Tool Use
Tool use allows Claude to call external functions, enabling it to interact with APIs, databases, and real-world systems.
Defining Tools
const response = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
tools: [{
name: "get_stock_price",
description: "Get the current stock price for a given ticker symbol",
input_schema: {
type: "object",
properties: {
ticker: { type: "string", description: "Stock ticker symbol (e.g., AAPL)" },
currency: { type: "string", enum: ["USD", "EUR"], default: "USD" }
},
required: ["ticker"]
}
}],
messages: [{ role: "user", content: "What's Apple's stock price?" }],
});
Handling Tool Calls
When Claude wants to use a tool, the response includes a tool_use content block:
for (const block of response.content) {
if (block.type === "tool_use") {
const result = await executeFunction(block.name, block.input);
// Send tool result back
messages.push({ role: "assistant", content: response.content });
messages.push({
role: "user",
content: [{ type: "tool_result", tool_use_id: block.id, content: JSON.stringify(result) }]
});
}
}
Multi-Tool Orchestration
Claude can use multiple tools in sequence or parallel:
- Sequential: Claude calls one tool, processes the result, then calls another
- Parallel: Claude requests multiple tool calls simultaneously
- Conditional: Tool choice depends on previous results
Best Practices
- Write clear, specific tool descriptions
- Use JSON Schema validation on tool inputs
- Implement timeouts for external API calls
- Return structured error messages for failed tool calls
Tool Description Quality Matters
Claude's ability to choose the right tool depends almost entirely on your description quality. A vague description like "searches things" will lead to incorrect tool selection. A precise description — "Searches the product catalog by SKU, name, or category. Returns up to 10 matching products with price, stock level, and image URL." — allows Claude to select it exactly when needed and skip it when irrelevant. Include examples of when to use and when NOT to use each tool when you have multiple similar tools.
Handling Parallel Tool Calls
Claude may request multiple tools simultaneously when it determines they are independent. Your loop must collect ALL tool_use blocks before responding:
const toolResults = await Promise.all(
response.content
.filter((b) => b.type === "tool_use")
.map(async (b) => ({
type: "tool_result" as const,
tool_use_id: b.id,
content: JSON.stringify(await executeFunction(b.name, b.input)),
}))
);
messages.push({ role: "assistant", content: response.content });
messages.push({ role: "user", content: toolResults });
Pitfall: if you respond with only a subset of tool results, Claude will either error or produce unreliable completions.
Key Takeaways
- •Define tools with JSON Schema input specifications
- •Handle tool_use content blocks and return tool_result messages
- •Claude supports sequential and parallel tool calling
- •Clear tool descriptions improve Claude's tool selection accuracy
Progress is kept in this browser only.