工具搜尋工具讓 Claude 能夠透過按需探索和載入的方式,處理數百或數千個工具。Claude 不會預先將所有工具定義載入「context window」(上下文視窗),而是搜尋您的工具目錄(包括工具名稱、描述、參數名稱和參數描述),並僅載入所需的工具。
隨著工具庫的增長,預先載入每個工具定義會導致兩個問題:
工具搜尋已在 Claude API 上正式推出。如需了解支援的模型,請參閱模型相容性。
如需了解工具搜尋所解決的擴展挑戰背景,請參閱 Advanced tool use。工具搜尋的按需載入也是 Effective context engineering 中所述更廣泛的即時擷取原則的一個實例。
工具搜尋以伺服器端工具的形式執行,但您也可以實作自己的用戶端工具搜尋。詳情請參閱自訂工具搜尋實作。
請透過意見回饋表單分享您對此功能的意見。
此功能符合「Zero Data Retention」(零資料保留),即 ZDR 的資格。當您的組織具有 ZDR 安排時,透過此功能傳送的資料在 API 回應返回後不會被儲存。
在 Amazon Bedrock 上,伺服器端工具搜尋僅可透過 InvokeModel API 使用,而非 Converse API。
在 Claude Platform on AWS 上,伺服器端工具搜尋的運作方式與 Claude API 完全相同。Claude Platform on AWS 直接使用 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 之前將其展開為完整的工具定義。前綴保持不變,因此「prompt caching」(提示快取)得以保留。嚴格模式的語法(限制工具呼叫輸出以符合您的結構描述的規則)是從完整工具集建構的,因此 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,並在 content 陣列中包含 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_result 格式並搭配 tool_reference 內容區塊,如前述範例所示。
如需使用嵌入的完整範例,請參閱 tool search with embeddings 範例。
工具使用範例
可與工具搜尋搭配使用:當 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。請將快取斷點放在非延遲載入的工具上。
啟用「streaming」(串流)後,您將在串流中收到工具搜尋事件:
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 物件沒有工具搜尋欄位,而搜尋載入到上下文中的工具定義會像任何其他工具定義一樣計為輸入 token。
透過在您的應用程式中實作記憶體工具的檔案操作,讓 Claude 跨對話儲存和擷取資訊。
Anthropic 提供的工具目錄以及選用工具定義屬性的參考。
設定具有延遲載入的 MCP 工具集。
跨回合快取工具定義,並了解哪些情況會使您的快取失效。
指定工具結構描述、撰寫有效的描述,並控制 Claude 何時呼叫您的工具。
Was this page helpful?