对于复杂的工具和模糊的查询,使用最新的 Claude Opus (4.7) 模型;它能更好地处理多个工具,并在需要时寻求澄清。
对于直接的工具,使用 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-7",
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 字段中指定工具来执行此操作,如下所示:
tool_choice = {"type": "tool", "name": "get_weather"}使用 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 通常会在调用工具之前评论它在做什么或自然地响应用户。
例如,给定提示"San Francisco 现在的天气如何,那里现在是什么时间?",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?