工具搜尋工具讓 Claude 能夠透過動態探索並按需載入的方式,處理數百甚至數千個工具。Claude 不需要預先將所有工具定義載入「context window」(上下文視窗),而是搜尋您的工具目錄(包括工具名稱、描述、參數名稱和參數描述),並僅載入所需的工具。
此方法解決了隨著工具庫規模擴大而迅速加劇的兩個問題:
如需了解工具搜尋所解決的擴展挑戰背景,請參閱 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 的區別。
工具搜尋有兩種變體:
tool_search_tool_regex_20251119):Claude 建構正規表示式模式來搜尋工具tool_search_tool_bm25_20251119):Claude 使用自然語言查詢來搜尋工具當您啟用工具搜尋工具時:
tool_search_tool_regex_20251119 或 tool_search_tool_bm25_20251119)。defer_loading: true。tool_reference 區塊。這能保持您的上下文視窗高效運作,同時維持高工具選擇準確度。
以下是使用延遲載入工具的簡單範例:
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)工具搜尋工具有兩種變體:
{
"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" - 使用 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)都會搜尋工具名稱、描述、參數名稱和參數描述。
延遲載入的內部運作方式: 延遲載入的工具不會包含在系統提示前綴中。當模型透過工具搜尋探索到延遲載入的工具時,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": {
"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,並在 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。此方法讓您能使用更精密的搜尋演算法,同時保持與工具搜尋系統的相容性。
回應格式章節中顯示的 tool_search_tool_result 格式是 Anthropic 內建工具搜尋內部使用的伺服器端格式。對於自訂的用戶端實作,請一律使用標準的 tool_result 格式搭配 tool_reference 內容區塊,如前述範例所示。
如需使用嵌入的完整範例,請參閱 tool search with embeddings cookbook。
工具搜尋工具與工具使用範例不相容。 如果您需要提供工具使用的範例,請使用不含工具搜尋的標準工具呼叫。
這些錯誤會阻止請求被處理:
所有工具皆為延遲載入:
{
"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_search_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您可以在 Messages Batches API 中包含工具搜尋工具。透過 Messages Batches API 進行的工具搜尋操作,其定價與一般 Messages API 請求相同。
適合的使用情境:
傳統工具呼叫可能更適合的情況:
github_、slack_),讓搜尋查詢自然地呈現正確的工具群組工具搜尋工具的使用量會在回應的 usage 物件中追蹤:
{
"usage": {
"input_tokens": 1024,
"output_tokens": 256,
"server_tool_use": {
"tool_search_requests": 2
}
}
}Was this page helpful?