Introduction to Claude Tool Use
What is Tool Use?
Tool use (also called function calling) enables Claude to interact with external systems — databases, APIs, calculators, code interpreters — by requesting that your application execute specific functions. This transforms Claude from a text generator into an active agent that can take actions in the world.
How Tool Use Works
The flow is:
- You define tools (functions) Claude can call
- User sends a message requiring external data/action
- Claude responds with a
tool_usecontent block specifying which tool to call and with what arguments - Your application executes the tool
- You return the result to Claude in a
tool_resultmessage - Claude incorporates the result and responds to the user
Defining a Tool
typescript
const tools: Anthropic.Tool[] = [
{
name: 'get_stock_price',
description: 'Retrieves the current stock price for a given ticker symbol. Use this when the user asks about stock prices or needs current market data.',
input_schema: {
type: 'object',
properties: {
ticker: {
type: 'string',
description: 'The stock ticker symbol (e.g., AAPL, GOOGL, TSLA)',
},
currency: {
type: 'string',
enum: ['USD', 'EUR', 'GBP'],
description: 'The currency for the price. Defaults to USD.',
},
},
required: ['ticker'],
},
},
];
Processing Tool Calls
typescript
async function runWithTools(userMessage: string) {
const messages: Anthropic.MessageParam[] = [
{ role: 'user', content: userMessage }
];
while (true) {
const response = await client.messages.create({
model: 'claude-sonnet-4-5',
max_tokens: 1024,
tools,
messages,
});
if (response.stop_reason === 'end_turn') {
return response.content.find(b => b.type === 'text')?.text;
}
if (response.stop_reason === 'tool_use') {
const toolUseBlocks = response.content.filter(b => b.type === 'tool_use');
const toolResults = [];
for (const toolUse of toolUseBlocks) {
const result = await executeToolCall(toolUse.name, toolUse.input);
toolResults.push({
type: 'tool_result',
tool_use_id: toolUse.id,
content: JSON.stringify(result),
});
}
messages.push({ role: 'assistant', content: response.content });
messages.push({ role: 'user', content: toolResults });
}
}
}
Writing Good Tool Descriptions
The description field is critical — it's what Claude uses to decide when and how to use each tool. Be specific about:
- What the tool does
- When to use it
- What the inputs mean
- What the output represents
Tool use unlocks a new category of applications: agents that can retrieve information, take actions, and interact with the real world on behalf of users.