工具搜尋工具使 Claude 能夠通過動態發現和按需加載工具來處理數百或數千個工具。與其預先將所有工具定義加載到上下文窗口中,Claude 會搜尋您的工具目錄(包括工具名稱、描述、參數名稱和參數描述),並僅加載它需要的工具。
隨著工具庫規模的擴大,這種方法解決了兩個複合增長的問題:
雖然這是作為伺服器端工具提供的,但您也可以實現自己的客戶端工具搜尋功能。有關詳細信息,請參閱自訂工具搜尋實現。
通過反饋表單分享有關此功能的反饋。
This feature qualifies for Zero Data Retention (ZDR) with limited technical retention. See the Data retention section for details on what is retained and why.
在 Amazon Bedrock 上,伺服器端工具搜尋僅通過 invoke API 提供,不支持反向 API。
您也可以通過從自己的搜尋實現返回 tool_reference 塊來實現客戶端工具搜尋。
有兩種工具搜尋變體:
tool_search_tool_regex_20251119):Claude 構造正則表達式模式來搜尋工具tool_search_tool_bm25_20251119):Claude 使用自然語言查詢來搜尋工具當您啟用工具搜尋工具時:
tool_search_tool_regex_20251119 或 tool_search_tool_bm25_20251119)defer_loading: truetool_reference 塊這樣可以保持上下文窗口的效率,同時保持高工具選擇準確性。
以下是一個帶有延遲工具的簡單示例:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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)工具搜尋工具有兩種變體:
{
"type": "tool_search_tool_regex_20251119",
"name": "tool_search_tool_regex"
}{
"type": "tool_search_tool_bm25_20251119",
"name": "tool_search_tool_bm25"
}正則表達式變體查詢格式:Python 正則表達式,不是自然語言
使用 tool_search_tool_regex_20251119 時,Claude 使用 Python 的 re.search() 語法構造正則表達式模式,而不是自然語言查詢。常見模式:
"weather" - 匹配名稱/描述中包含 "weather" 的工具"get_.*_data" - 匹配 get_user_data、get_weather_data 等工具"database.*query|query.*database" - 用於靈活性的 OR 模式"(?i)slack" - 不區分大小寫的搜尋最大查詢長度:200 個字符
BM25 變體查詢格式:自然語言
使用 tool_search_tool_bm25_20251119 時,Claude 使用自然語言查詢來搜尋工具。
通過添加 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 的工具會立即加載到上下文中defer_loading: true 的工具僅在 Claude 通過搜尋發現它們時才加載defer_loading: true兩種工具搜尋變體(regex 和 bm25)都搜尋工具名稱、描述、參數名稱和參數描述。
延遲工作原理: 延遲工具不包含在系統提示前綴中。當模型通過工具搜尋發現延遲工具時,工具定義作為 tool_reference 塊內聯附加到對話中。前綴保持不變,因此提示快取得以保留。嚴格模式的語法從完整工具集構建,因此 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": {
"query": "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 正在調用工具搜尋工具tool_search_tool_result: 包含帶有嵌套 tool_search_tool_search_result 對象的搜尋結果tool_references: 指向發現的工具的 tool_reference 對象數組tool_use: Claude 調用發現的工具tool_reference 塊會自動擴展為完整的工具定義,然後顯示給 Claude。您無需自己處理此擴展。只要您在 tools 參數中提供所有匹配的工具定義,它就會在 API 中自動發生。
有關使用 defer_loading 配置 mcp_toolset 的信息,請參閱 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。這種方法讓您可以使用更複雜的搜尋算法,同時保持與工具搜尋系統的兼容性。
回應格式部分中顯示的 tool_search_tool_result 格式是 Anthropic 內置工具搜尋內部使用的伺服器端格式。對於自訂客戶端實現,始終使用標準 tool_result 格式,其中包含如上所示的 tool_reference 內容塊。
有關使用嵌入的完整示例,請參閱帶嵌入的工具搜尋食譜。
工具搜尋工具與工具使用示例不兼容。 如果您需要提供工具使用示例,請使用不帶工具搜尋的標準工具調用。
這些錯誤會阻止請求被處理:
所有工具都延遲:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "All tools have defer_loading set. At least one tool must be non-deferred."
}
}缺少工具定義:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "Tool reference 'unknown_tool' has no corresponding tool definition"
}
}工具執行期間的錯誤返回 200 回應,正文中包含錯誤信息:
{
"type": "tool_result",
"tool_use_id": "srvtoolu_01ABC123",
"content": {
"type": "tool_search_tool_result_error",
"error_code": "invalid_pattern"
}
}錯誤代碼:
too_many_requests:工具搜尋操作超過速率限制invalid_pattern:格式錯誤的正則表達式模式pattern_too_long:模式超過 200 個字符限制unavailable:工具搜尋服務暫時不可用有關 defer_loading 如何保留提示快取的信息,請參閱帶提示快取的工具使用。
系統會自動在整個對話歷史記錄中擴展 tool_reference 塊,因此 Claude 可以在後續轉向中重複使用發現的工具,而無需重新搜尋。
啟用串流後,您將收到工具搜尋事件作為串流的一部分:
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 query streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"query\":\"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您可以在訊息批次 API 中包含工具搜尋工具。通過訊息批次 API 的工具搜尋操作的定價與常規訊息 API 請求中的相同。
伺服器端工具搜尋(tool_search 工具)索引和存儲工具目錄數據(工具名稱、描述和參數元數據),超出立即 API 回應;此目錄數據根據 Anthropic 的標準保留政策保留。使用標準訊息 API 的自訂客戶端工具搜尋實現完全符合 ZDR 資格。
有關所有功能的 ZDR 資格,請參閱 API 和數據保留。
良好的使用案例:
傳統工具調用可能更好的情況:
github_、slack_),以便搜尋查詢自然地呈現正確的工具組工具搜尋工具使用在回應使用對象中被追蹤:
{
"usage": {
"input_tokens": 1024,
"output_tokens": 256,
"server_tool_use": {
"tool_search_requests": 2
}
}
}Was this page helpful?