工具搜索工具让 Claude 能够通过按需发现和加载工具来处理数百或数千个工具。Claude 不会预先将所有工具定义加载到 "context window"(上下文窗口)中,而是搜索您的工具目录(包括工具名称、描述、参数名称和参数描述),并仅加载所需的工具。
随着工具库的增长,预先加载每个工具定义会导致两个问题:
工具搜索已在 Claude API 上正式发布。有关支持的模型,请参阅模型兼容性。
有关工具搜索所解决的扩展挑战的背景信息,请参阅高级工具使用。工具搜索的按需加载也是面向 AI 智能体的有效上下文工程中描述的更广泛的即时检索原则的一个实例。
工具搜索作为服务器端工具运行,但您也可以实现自己的客户端工具搜索。有关详细信息,请参阅自定义工具搜索实现。
请通过反馈表单分享您对此功能的反馈。
此功能符合零数据保留(ZDR)的条件。当您的组织签订了 ZDR 协议时,通过此功能发送的数据在 API 响应返回后不会被存储。
在 Amazon Bedrock 上,服务器端工具搜索仅通过 InvokeModel API 提供,而非 Converse API。
在 AWS 上的 Claude Platform 上,服务器端工具搜索的工作方式与 Claude API 完全相同。AWS 上的 Claude Platform 直接使用 Anthropic Messages API,因此不存在 InvokeModel 或 Converse 的区别。
两种工具搜索变体均可在以下模型上使用:
| 模型 | 工具版本 |
|---|---|
| Claude Fable 5 (claude-fable-5) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Mythos 5 (claude-mythos-5) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.8 (claude-opus-4-8) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.7 (claude-opus-4-7) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.6 (claude-opus-4-6) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Sonnet 4.6 (claude-sonnet-4-6) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.5 (claude-opus-4-5-20251101) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Sonnet 4.5 (claude-sonnet-4-5-20250929) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Haiku 4.5 (claude-haiku-4-5-20251001) | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
Claude Opus 4.1 及更早的模型不支持工具搜索工具。
工具搜索有两种变体:
tool_search_tool_regex_20251119):Claude 构造正则表达式模式来搜索工具。tool_search_tool_bm25_20251119):Claude 使用自然语言查询来搜索工具。当您启用工具搜索工具时:
tools 列表中包含一个工具搜索工具(例如 tool_search_tool_regex_20251119 或 tool_search_tool_bm25_20251119)。tools 数组中提供每个工具定义,并在不应预先加载的工具上设置 defer_loading: true。至少有一个工具(通常是工具搜索工具本身)必须保持非延迟状态。tool_reference 块返回(默认最多 5 个)。以下示例包含工具搜索工具和两个延迟工具:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=2048,
messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
tools=[
{"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"},
{
"name": "get_weather",
"description": "Get the weather at a specific location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
"defer_loading": True,
},
{
"name": "search_files",
"description": "Search through files in the workspace",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string"},
"file_types": {"type": "array", "items": {"type": "string"}},
},
"required": ["query"],
},
"defer_loading": True,
},
],
)
print(response)Claude 搜索目录,发现 get_weather,并调用它。响应以 stop_reason: "tool_use" 结束。执行发现的工具并返回 tool_result,如处理工具调用中所述。响应格式展示了您收到的块以及接下来要发送的内容。
工具搜索工具有两种变体:
{
"type": "tool_search_tool_regex_20251119",
"name": "tool_search_tool_regex"
}{
"type": "tool_search_tool_bm25_20251119",
"name": "tool_search_tool_bm25"
}Regex 变体查询格式:Python 正则表达式,而非自然语言
使用 tool_search_tool_regex_20251119 时,Claude 编写 Python re.search() 模式,而非自然语言查询。匹配不区分大小写。常见模式包括以下内容:
"weather":匹配包含 "weather" 的工具名称和描述"get_.*_data":匹配诸如 get_user_data 和 get_weather_data 之类的工具"database.*query|query.*database":匹配任一词序最大模式长度:200 个字符
BM25 变体查询格式:自然语言
使用 tool_search_tool_bm25_20251119 时,Claude 使用自然语言查询进行搜索。最大查询长度:500 个字符。
通过添加 defer_loading: true 将工具标记为按需加载:
{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": { "type": "string" },
"unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
},
"required": ["location"]
},
"defer_loading": true
}defer_loading 控制的是进入上下文窗口的内容,而不是您在请求中发送的内容:
tools 数组中发送每个工具的完整定义,包括延迟的工具。API 需要它们在服务器端运行搜索并扩展 tool_reference 块。defer_loading 的工具会立即加载到上下文中。defer_loading: true 的工具仅在 Claude 通过搜索发现它们时才加载。defer_loading: true。两种工具搜索变体(regex 和 bm25)都会搜索工具名称、描述、参数名称和参数描述。
在内部,API 会将延迟工具从系统提示前缀中排除。当 Claude 通过工具搜索发现延迟工具时,API 会在对话中内联附加一个 tool_reference 块,然后在传递给 Claude 之前将其扩展为完整的工具定义。前缀保持不变,因此提示缓存得以保留。严格模式的语法(约束工具调用输出以匹配您的模式的规则)是基于完整工具集构建的,因此 defer_loading 和严格模式可以组合使用而无需重新编译语法。
当 Claude 使用工具搜索工具时,响应包含以下块类型:
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll search for tools to help with the weather information."
},
{
"type": "server_tool_use",
"id": "srvtoolu_01ABC123",
"name": "tool_search_tool_regex",
"input": {
"pattern": "weather"
}
},
{
"type": "tool_search_tool_result",
"tool_use_id": "srvtoolu_01ABC123",
"content": {
"type": "tool_search_tool_search_result",
"tool_references": [{ "type": "tool_reference", "tool_name": "get_weather" }]
}
},
{
"type": "text",
"text": "I found a weather tool. Let me get the weather for San Francisco."
},
{
"type": "tool_use",
"id": "toolu_01XYZ789",
"name": "get_weather",
"input": { "location": "San Francisco", "unit": "fahrenheit" }
}
],
"stop_reason": "tool_use"
}server_tool_use: Claude 对工具搜索工具的调用。搜索在 Anthropic 的服务器上运行。切勿为其 srvtoolu_... ID 返回 tool_result。tool_search_tool_result: 搜索结果,位于嵌套的 tool_search_tool_search_result 对象中。将其按原样保留在消息历史记录中。tool_references: 指向已发现工具的 tool_reference 对象数组。API 会为 Claude 扩展这些引用。您无需自行扩展它们。tool_use: Claude 对已发现工具的调用。执行它并返回 tool_result,与标准工具使用完全相同。API 会在向 Claude 展示之前自动将 tool_reference 块扩展为完整的工具定义。只要您在 tools 参数中提供所有匹配的工具定义,就无需自行处理此扩展。
在下一个请求中,将助手的内容原样传回,包括 server_tool_use 和 tool_search_tool_result 块。在用户消息中为已发现的工具添加您的 tool_result,并发送相同的 tools 数组:搜索工具加上每个延迟定义。不要为 srvtoolu_... ID 返回 tool_result:API 会拒绝该请求。API 会在整个对话历史记录中扩展 tool_reference 块,因此 Claude 可以在后续轮次中重用已发现的工具而无需重新搜索。没有匹配结果的搜索会返回一个带有空 tool_references 数组的 tool_search_tool_search_result,而不是错误。
如果您的工具通过 MCP 连接器来自 MCP 服务器,则无需在单个工具定义上设置 defer_loading。而是在 mcp_toolset 条目的 default_config 上为整个服务器设置一次,或在其 configs 中按工具设置。请参阅 MCP 工具集配置。
您可以通过从自定义工具返回 tool_reference 块来实现自己的工具搜索逻辑(例如,使用嵌入或语义搜索)。当 Claude 调用您的自定义搜索工具时,返回一个标准的 tool_result,并在内容数组中包含 tool_reference 块:
{
"type": "tool_result",
"tool_use_id": "toolu_your_tool_id",
"content": [{ "type": "tool_reference", "tool_name": "discovered_tool_name" }]
}每个被引用的工具都必须在顶层 tools 参数中有对应的工具定义,通常带有 defer_loading: true。这使您可以使用内置变体不提供的搜索方法,例如基于嵌入的检索,并且 API 会以相同的方式扩展返回的 tool_reference 块。
响应格式部分中显示的 tool_search_tool_result 格式是 Anthropic 内置工具搜索在内部使用的服务器端格式。对于自定义客户端实现,请始终使用带有 tool_reference 内容块的标准 tool_result 格式,如前面的示例所示。
有关使用嵌入的完整示例,请参阅使用嵌入的工具搜索配方。
工具使用示例
可与工具搜索配合使用:当 Claude 发现延迟工具时,API 会将其
input_examples 与其定义一起扩展。
这些错误会阻止 API 处理请求:
所有工具均为延迟:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "At least one tool must have defer_loading=false. All tools cannot be deferred."
}
}缺少工具定义:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "Tool reference 'unknown_tool' not found in available tools"
}
}当工具搜索操作在执行期间失败时,API 会返回 200 响应,并在正文中包含错误:
{
"type": "tool_search_tool_result",
"tool_use_id": "srvtoolu_01ABC123",
"content": {
"type": "tool_search_tool_result_error",
"error_code": "invalid_tool_input",
"error_message": "Invalid regular expression pattern: missing ) at position 1"
}
}error_code 字段有四个可能的值:
invalid_tool_input:搜索输入无效,例如格式错误的正则表达式模式或超过 200 个字符限制的模式unavailable:搜索无法运行,例如因为超时或服务不可用too_many_requests:工具搜索操作超出速率限制execution_time_exceeded:搜索超出其执行时间限制有关 defer_loading 如何保留提示缓存,请参阅工具使用与提示缓存。
带有 defer_loading: true 的工具不能同时携带 cache_control:API 会返回 400。请将缓存断点放在非延迟工具上。
启用流式传输后,您将在流中接收工具搜索事件:
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "tool_search_tool_regex"}}
// Search pattern streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"pattern\":\"weather\"}"}}
// Pause while search executes
// Search results streamed
event: content_block_start
data: {"type": "content_block_start", "index": 2, "content_block": {"type": "tool_search_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": {"type": "tool_search_tool_search_result", "tool_references": [{"type": "tool_reference", "tool_name": "get_weather"}]}}}
// Claude continues with discovered tools您可以在 Messages Batches API 中包含工具搜索工具。
defer_loading: true 的工具当以下任一情况适用时,请使用工具搜索:
当您的工具少于 10 个、每个请求都使用每个工具,或者您的工具定义很小(总计少于 100 个令牌)时,不使用工具搜索的标准工具调用更为合适。
github_、slack_),以便一次搜索匹配整个组。工具搜索不作为单独的服务器工具计量。响应的 usage.server_tool_use 对象没有工具搜索字段,搜索加载到上下文中的工具定义与任何其他工具定义一样计为输入令牌。
通过在您的应用程序中实现记忆工具的文件操作,让 Claude 跨对话存储和检索信息。
Anthropic 提供的工具目录以及可选工具定义属性的参考。
配置具有延迟加载的 MCP 工具集。
跨轮次缓存工具定义,并了解哪些操作会使您的缓存失效。
指定工具模式、编写有效的描述,并控制 Claude 何时调用您的工具。
Was this page helpful?