什么是 Tool Use
Tool Use(也叫函数调用)允许 Claude 与外部工具、API 和数据源交互。Claude 不会直接执行工具——它会生成结构化的请求,由你的应用程序执行。工作流程
- 定义工具 - 名称、描述和输入 schema
- Claude 判断 - 是否需要使用工具
- Claude 生成 - 工具调用请求(JSON)
- 应用执行 - 调用实际的工具
- 返回结果 - 将结果发回给 Claude
- Claude 回复 - 生成最终答案
基础示例
- Python
- Node.js
复制
import os
import anthropic
client = anthropic.Anthropic(
api_key=os.getenv('PIPELLM_API_KEY'),
base_url='https://api.pipellm.ai'
)
# 定义工具
tools = [
{
"name": "get_weather",
"description": "获取指定地点的当前天气",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,如:北京"
}
},
"required": ["location"]
}
}
]
# 第一次调用 - Claude 决定使用工具
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "东京现在天气怎么样?"}
]
)
# 检查 Claude 是否要使用工具
if response.stop_reason == "tool_use":
tool_use = next(b for b in response.content if b.type == "tool_use")
# 执行你的工具(示例)
tool_result = {"temperature": "22°C", "condition": "晴天"}
# 将结果返回给 Claude
final_response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "东京现在天气怎么样?"},
{"role": "assistant", "content": response.content},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": str(tool_result)
}
]
}
]
)
print(final_response.content[0].text)
复制
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.PIPELLM_API_KEY,
baseURL: 'https://api.pipellm.ai'
});
const tools = [
{
name: "get_weather",
description: "获取指定地点的当前天气",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "城市名称,如:北京"
}
},
required: ["location"]
}
}
];
const response = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
tools: tools,
messages: [
{ role: 'user', content: '东京现在天气怎么样?' }
]
});
if (response.stop_reason === 'tool_use') {
const toolUse = response.content.find(b => b.type === 'tool_use');
// 执行你的工具
const toolResult = { temperature: "22°C", condition: "晴天" };
const finalResponse = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
tools: tools,
messages: [
{ role: 'user', content: '东京现在天气怎么样?' },
{ role: 'assistant', content: response.content },
{
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: toolUse.id,
content: JSON.stringify(toolResult)
}
]
}
]
});
console.log(finalResponse.content[0].text);
}
更多资源
Tool Use 官方文档
完整的工具定义和响应处理指南