工具搜尋工具讓 Claude 能夠透過按需探索和載入工具,來處理數百或數千個工具。Claude 不會預先將所有工具定義載入到上下文視窗中,而是搜尋您的工具目錄(包括工具名稱、描述、參數名稱和參數描述),並僅載入它需要的工具。
隨著工具庫的增長,預先載入每個工具定義會導致兩個問題:
工具搜尋已在 Claude API 上正式推出。有關支援的模型,請參閱模型相容性。
工具搜尋作為伺服器端工具運行,但您也可以實作自己的客戶端工具搜尋。詳情請參閱自訂工具搜尋實作。
以下模型均支援兩種工具搜尋變體:
| 模型 | 工具版本 |
|---|---|
| Claude Fable 5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Mythos 5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.8 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.7 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.6 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Sonnet 4.6 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Opus 4.5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Sonnet 4.5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
| Claude Haiku 4.5 () | tool_search_tool_regex_20251119、tool_search_tool_bm25_20251119 |
Claude Opus 4.1 及更早的模型不支援工具搜尋工具。
工具搜尋有兩種變體:
tool_search_tool_regex_20251119):Claude 建構 regex 模式來搜尋工具。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-5",
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"
}透過新增 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_reference 區塊的標準 tool_result:
{
"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 區塊。
有關使用嵌入的完整範例,請參閱使用嵌入的工具搜尋範例。
這些錯誤會阻止 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:搜尋輸入無效,例如格式錯誤的 regex 模式或超過 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 個 token)時,不使用工具搜尋的標準工具呼叫是更好的選擇。
github_、slack_),這樣一次搜尋就能匹配整個群組。工具搜尋不會作為單獨的伺服器工具計量。回應的 usage.server_tool_use 物件沒有工具搜尋欄位,而搜尋載入到上下文中的工具定義會像任何其他工具定義一樣計為 input_tokens。
透過在您的應用程式中實作記憶工具的檔案操作,讓 Claude 能夠跨對話儲存和檢索資訊。
Anthropic 提供的工具目錄以及可選工具定義屬性的參考。
配置具有延遲載入的 MCP 工具集。
跨回合快取工具定義,並了解什麼會使您的快取失效。
指定工具結構描述、撰寫有效的描述,並控制 Claude 何時呼叫您的工具。
Was this page helpful?