对于复杂的工具和模糊的查询,请使用最新的 Claude Opus (4.8) 模型;它能更好地处理多个工具,并在需要时寻求澄清。
对于简单的工具,请使用 Claude Haiku 模型,但请注意它们可能会推断缺失的参数。
如果您将 Claude 与工具使用和扩展思考结合使用,请参阅扩展思考指南了解更多信息。
客户端工具(包括 Anthropic 模式和用户定义的工具)在 API 请求的顶级参数 tools 中指定。每个工具定义包括:
| 参数 | 描述 |
|---|---|
name | 工具的名称。必须匹配正则表达式 ^[a-zA-Z0-9_-]{1,64}$。 |
description | 详细的纯文本描述,说明工具的功能、何时应使用以及其行为方式。 |
input_schema | 一个 JSON Schema 对象,定义工具的预期参数。 |
input_examples | (可选)示例输入对象的数组,帮助 Claude 理解如何使用该工具。请参阅提供工具使用示例。 |
有关任何工具定义上可用的完整可选属性集(包括 cache_control、strict、defer_loading 和 allowed_callers),请参阅工具参考。
当您使用 tools 参数调用 Claude API 时,API 会根据工具定义、工具配置和任何用户指定的系统提示构建一个特殊的系统提示。构建的提示旨在指示模型使用指定的工具,并提供工具正常运行所需的上下文:
In this environment you have access to a set of tools you can use to answer the user's question.
{{ FORMATTING INSTRUCTIONS }}
String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.
Here are the functions available in JSONSchema format:
{{ TOOL DEFINITIONS IN JSON SCHEMA }}
{{ USER SYSTEM PROMPT }}
{{ TOOL CONFIGURATION }}要在使用工具时让 Claude 发挥最佳性能,请遵循以下准则:
input_examples。 清晰的描述最为重要,但对于具有复杂输入、嵌套对象或格式敏感参数的工具,您可以使用 input_examples 字段提供经过模式验证的示例。详情请参阅提供工具使用示例。create_pr、review_pr、merge_pr),不如将它们组合成一个带有 action 参数的单一工具。更少但功能更强大的工具可以减少选择歧义,使 Claude 更容易浏览您的工具集。github_list_prs、slack_send_message)。随着工具库的增长,这可以使工具选择更加明确,在使用工具搜索时尤为重要。良好的描述清楚地解释了工具的功能、何时使用、返回什么数据以及 ticker 参数的含义。不良的描述过于简短,让 Claude 对工具的行为和用法产生许多疑问。
有关工具设计(整合、命名和响应塑造)的更深入指导,请参阅为智能体编写工具。
您可以提供有效工具输入的具体示例,帮助 Claude 更有效地理解如何使用您的工具。这对于具有嵌套对象、可选参数或格式敏感输入的复杂工具特别有用。
在工具定义中添加可选的 input_examples 字段,其中包含示例输入对象的数组。每个示例必须符合工具的 input_schema:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature",
},
},
"required": ["location"],
},
"input_examples": [
{"location": "San Francisco, CA", "unit": "fahrenheit"},
{"location": "Tokyo, Japan", "unit": "celsius"},
{
"location": "New York, NY" # 'unit' is optional
},
],
}
],
messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)
print(response)示例会与您的工具模式一起包含在提示中,向 Claude 展示格式良好的工具调用的具体模式。这有助于 Claude 理解何时包含可选参数、使用什么格式以及如何构建复杂的输入。
input_schema。无效的示例会返回 400 错误在某些情况下,您可能希望 Claude 使用特定工具来回答用户的问题,即使 Claude 本可以不调用工具而直接回答。您可以通过在请求的 tool_choice 字段中指定工具来实现这一点。高亮显示的行是与标准工具使用请求的唯一区别:
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"],
},
}
]
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
tool_choice={"type": "tool", "name": "get_weather"},
messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)
print(response)使用 tool_choice 参数时,有四个可能的选项:
auto 允许 Claude 决定是否调用任何提供的工具。当提供了 tools 时,这是默认值。any 告诉 Claude 必须使用提供的工具之一,但不强制使用特定工具。tool 强制 Claude 始终使用特定工具。none 阻止 Claude 使用任何工具。当未提供 tools 时,这是默认值。使用提示缓存时,更改 tool_choice 参数将使缓存的消息块失效。工具定义和系统提示仍保持缓存,但消息内容必须重新处理。
下图说明了每个选项的工作方式:

请注意,当您将 tool_choice 设置为 any 或 tool 时,API 会预填充助手消息以强制使用工具。这意味着即使明确要求,模型也不会在 tool_use 内容块之前发出自然语言响应或解释。
将扩展思考与工具使用结合时,不支持 tool_choice: {"type": "any"} 和 tool_choice: {"type": "tool", "name": "..."},使用它们将导致错误。只有 tool_choice: {"type": "auto"}(默认值)和 tool_choice: {"type": "none"} 与扩展思考兼容。
Claude Mythos Preview 不支持强制工具使用。在此模型上,带有 tool_choice: {"type": "any"} 或 tool_choice: {"type": "tool", "name": "..."} 的请求会返回 400 错误。请使用 tool_choice: {"type": "auto"}(默认值)或 tool_choice: {"type": "none"},并依靠提示来影响工具选择。
测试表明这不会降低性能。如果您希望模型在仍然要求使用特定工具的同时提供自然语言上下文或解释,您可以对 tool_choice 使用 {"type": "auto"}(默认值),并在 user 消息中添加明确的指令。例如:What's the weather like in London? Use the get_weather tool in your response.
使用严格工具保证工具调用
将 tool_choice: {"type": "any"} 与严格工具使用结合使用,可以同时保证您的工具之一会被调用,并且工具输入严格遵循您的模式。在工具定义上设置 strict: true 以启用模式验证。
使用工具时,Claude 通常会在调用工具之前说明它正在做什么或自然地回应用户。
例如,给定提示 "What's the weather like in San Francisco right now, and what time is it there?",Claude 可能会这样响应:
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll help you check the current weather and time in San Francisco."
},
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "San Francisco, CA" }
}
]
}这种自然的响应风格有助于用户理解 Claude 正在做什么,并创造更具对话性的交互。您可以通过系统提示以及在提示中提供 <examples> 来引导这些响应的风格和内容。
需要注意的是,Claude 在解释其操作时可能会使用各种措辞和方式。您的代码应将这些响应视为任何其他助手生成的文本,而不应依赖特定的格式约定。
Was this page helpful?