

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Anthropic Claude 工具使用
<a name="model-parameters-anthropic-claude-messages-tool-use"></a>

**警告**  
以下數個函數會以 Beta 版提供，如下所示。這些功能是以「Beta Service」的形式提供給您，如 AWS 服務條款所定義。它受您與 AWS 和 AWS 服務條款以及適用模型 EULA 的協議約束。

使用 Anthropic Claude 模型時，您可以指定模型可用來回應訊息的工具。例如，您可以指定工具，取得廣播電台上最熱門的歌曲。如果使用者傳遞訊息 *WZPZ 上最熱門的歌曲是什麼？*，模型會判斷您指定的工具可協助回答問題。在其回應中，模型會請求您代其執行工具。接下來，您會執行工具並將工具結果傳遞給模型，然後產生原始訊息的回應。如需詳細資訊，請參閱 Anthropic Claude 文件中的[工具使用 (函數呼叫)](https://docs.anthropic.com/en/docs/tool-use)。

**提示**  
我們建議您使用 Converse API 將工具使用整合至您的應用程式。如需詳細資訊，請參閱[使用工具完成 Amazon Bedrock 模型回應](tool-use.md)。

**重要**  
Claude Sonnet 4.5 現在會在工具呼叫字串參數中保留刻意格式化。先前，字串參數中的結尾換行有時未正確去除。此修正可確保需要精確格式化 (例如文字編輯器) 的工具完全依預期接收參數。這是幕後改善，不需要變更 API。不過，具有字串參數的工具現在可能會收到具有先前已去除之結尾換行的值。

**注意**  
Claude Sonnet 4.5 包含自動最佳化，可改善模型效能。這些最佳化可能會將少量字符新增至請求，但您不需要支付這些系統新增字符的費用。

您可以在 `tools` 欄位中指定要提供給模型的工具。下列範例適用於在廣播電台上取得最熱門歌曲的工具。

```
[
    {
        "name": "top_song",
        "description": "Get the most popular song played on a radio station.",
        "input_schema": {
            "type": "object",
            "properties": {
                "sign": {
                    "type": "string",
                    "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP."
                }
            },
            "required": [
                "sign"
            ]
        }
    }
]
```

當模型需要工具來產生訊息的回應時，它會在訊息 `content` 欄位中傳回所請求工具的相關資訊，以及工具的輸入。它也會將回應的停止原因設定為 `tool_use`。

```
{
    "id": "msg_bdrk_01USsY5m3XRUF4FCppHP8KBx",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-sonnet-20240229",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 375,
        "output_tokens": 36
    },
    "content": [
        {
            "type": "tool_use",
            "id": "toolu_bdrk_01SnXQc6YVWD8Dom5jz7KhHy",
            "name": "top_song",
            "input": {
                "sign": "WZPZ"
            }
        }
    ],
    "stop_reason": "tool_use"
}
```

在程式碼中，您可以代表工具呼叫工具。然後，在使用者訊息中將工具結果 (`tool_result`) 傳遞給模型。

```
{
    "role": "user",
    "content": [
        {
            "type": "tool_result",
            "tool_use_id": "toolu_bdrk_01SnXQc6YVWD8Dom5jz7KhHy",
            "content": "Elemental Hotel"
        }
    ]
}
```

在其回應中，模型會使用工具結果來產生原始訊息的回應。

```
{
    "id": "msg_bdrk_012AaqvTiKuUSc6WadhUkDLP",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-sonnet-20240229",
    "content": [
        {
            "type": "text",
            "text": "According to the tool, the most popular song played on radio station WZPZ is \"Elemental Hotel\"."
        }
    ],
    "stop_reason": "end_turn"
}
```

## 精細工具串流
<a name="model-parameters-anthropic-claude-messages-fine-grained-tool-streaming"></a>

精細工具串流是一種 Anthropic Claude 模型功能，可用於 Claude Sonnet 4.5、Claude Haiku 4.5、Claude Sonnet 4 和 Claude Opus 4。透過精細工具串流，Claude 開發人員可以串流工具使用參數，而無需緩衝或 JSON 驗證，從而減少開始接收大型參數的延遲。

**注意**  
使用精細工具串流時，您可能會收到無效或部分 JSON 輸入。請務必在程式碼中考慮這些邊緣案例。

若要使用此功能，只要將標頭 `fine-grained-tool-streaming-2025-05-14` 新增至工具使用請求即可。

以下是如何指定精細工具串流標頭的範例：

```
{
  "anthropic_version": "bedrock-2023-05-31",
  "max_tokens": 1024,
  "anthropic_beta": ["fine-grained-tool-streaming-2025-05-14"],
  "messages": [
    {
      "role": "user",
      "content": "Can you write a long poem and make a file called poem.txt?"
    }
  ],
  "tools": [
    {
      "name": "make_file",
      "description": "Write text to a file",
      "input_schema": {
        "type": "object",
        "properties": {
          "filename": {
            "type": "string",
            "description": "The filename to write text to"
          },
          "lines_of_text": {
            "type": "array",
            "description": "An array of lines of text to write to the file"
          }
        },
        "required": [
          "filename",
          "lines_of_text"
        ]
      }
    }
  ]
}
```

在此範例中，精細工具串流可讓 Claude 將長詩句的行串流到工具呼叫 `make_file` 中，而不進行緩衝，以驗證 `lines_of_text` 參數是否為有效的 JSON。這表示您可以在參數串流到達時看到它，而不必等待整個參數進行緩衝和驗證。

使用精細工具串流時，工具使用區塊會更快地開始串流，而且通常更長且文字換行更少。這是由於分塊行為的差異。

例如，不使用精細串流 (15 秒延遲)：

```
Chunk 1: '{"'
Chunk 2: 'query": "Ty'
Chunk 3: 'peScri'
Chunk 4: 'pt 5.0 5.1 '
Chunk 5: '5.2 5'
Chunk 6: '.3'
Chunk 8: ' new f'
Chunk 9: 'eatur'
...
```

使用精細串流 (3 秒延遲)：

```
Chunk 1: '{"query": "TypeScript 5.0 5.1 5.2 5.3'
Chunk 2: ' new features comparison'
```

**注意**  
由於精細串流在沒有緩衝或 JSON 驗證的情況下傳送參數，因此無法保證產生的串流會在有效的 JSON 字串中完成。特別是，如果達到停止原因 `max_tokens`，串流可能會透過參數中途結束，並且可能不完整。您通常必須撰寫特定的支援，以便在達到 `max_tokens` 時處理。

## 電腦使用 (Beta 版)
<a name="model-parameters-anthropic-claude-messages-computer-use"></a>

電腦使用是一種AnthropicClaude工具系列 （測試版），用於自動化圖形使用者介面 (GUI) 任務。如需概觀、Amazon Bedrock 特定的請求形狀，以及end-to-end範例，請參閱 [使用電腦使用工具，透過 Amazon Bedrock 模型自動化 GUI 任務](computer-use.md)。若要尋找哪些模型支援電腦在每個端點上使用 ，請參閱每個 中的*功能和功能*表[模型一目了然](model-cards.md)。

若要在請求上啟用電腦使用，`anthropic_beta`請將 設定為電腦使用版本，並包含`type`符合該版本的工具項目。有效的配對為：


| Beta 版標頭 | 電腦工具類型 | 
| --- | --- | 
| computer-use-2025-11-24 | computer\_20251124 | 
| computer-use-2025-01-24 | computer\_20250124 | 
| computer-use-2024-10-22 | computer\_20241022 | 

每個工具類型僅適用於特定的模型子集。提交模型不支援的工具類型會傳回`400 invalid_request_error`訊息如 的 `'claude-opus-4-7' does not support tool types: computer_20241022`。在傳送請求之前，確認模型*的功能和功能*資料表中的支援。

如需基礎工具通訊協定、完整動作詞彙和提示工程指引，請參閱 Anthropic 文件中的[電腦使用](https://docs.anthropic.com/en/docs/build-with-claude/computer-use)。

## Anthropic 定義的工具
<a name="model-parameters-anthropic-anthropic-defined-tools"></a>

Anthropic 提供一組預先定義的工具，Claude模型可用來與電腦互動。指定 Anthropic定義的工具時，不需要或不允許 `description`和 `tool_schema` 欄位。模型不會自動執行這些工具；您必須執行每個請求的動作，並將 傳回`tool_result`給 Claude。若要尋找每個模型接受哪些工具，請參閱模型 中的*功能和功能*表格[模型一目了然](model-cards.md)；提交模型不支援的工具類型會傳回 `400 invalid_request_error`。


| 工具 | 備註 | 
| --- | --- | 
|  <pre>{ <br />    "type": "computer_20251124", <br />    "name": "computer" <br />}</pre>  | 最新的電腦使用工具。與 `"anthropic_beta": ["computer-use-2025-11-24"]` 搭配使用。 | 
|  <pre>{ <br />    "type": "computer_20250124", <br />    "name": "computer" <br />}</pre>  | 與 `"anthropic_beta": ["computer-use-2025-01-24"]` 搭配使用。 | 
|  <pre>{ <br />    "type": "computer_20241022", <br />    "name": "computer" <br />}</pre>  | 舊版。與 `"anthropic_beta": ["computer-use-2024-10-22"]` 搭配使用。 | 
|  <pre>{ <br />    "type": "text_editor_20250124", <br />    "name": "str_replace_based_edit_tool" <br />}</pre>  | 更新現有`str_replace_editor`工具。搭配 `"anthropic_beta": ["computer-use-2025-01-24"]`或 使用 `["computer-use-2025-11-24"]`。 | 
|  <pre>{ <br />    "type": "text_editor_20241022", <br />    "name": "str_replace_editor" <br />}</pre>  | 舊版。與 `"anthropic_beta": ["computer-use-2024-10-22"]` 搭配使用。 | 
|  <pre>{ <br />    "type": "bash_20250124", <br />    "name": "bash" <br />}</pre>  | 搭配 `"anthropic_beta": ["computer-use-2025-01-24"]`或 使用`["computer-use-2025-11-24"]`。 | 
|  <pre>{ <br />    "type": "bash_20241022", <br />    "name": "bash" <br />}</pre>  | 舊版。與 `"anthropic_beta": ["computer-use-2024-10-22"]` 搭配使用。 | 

`type` 欄位識別用於驗證的工具及其參數； `name` 欄位是公開給模型的工具名稱。

如果您想要提示模型使用其中一個工具，您可以依 `name` 欄位明確參考工具。`name` 欄位在工具清單中必須是唯一的；您無法在相同的 API 呼叫中定義與 Anthropic定義工具`name`相同的工具。

## 自動工具呼叫清除 (Beta 版)
<a name="model-parameters-anthropic-claude-automatic-tool-call-clearing"></a>

**警告**  
自動工具呼叫清除會以「Beta Service」的形式提供，如 AWS 服務條款所定義。

**注意**  
Claude Sonnet 4/4.5、Claude Haiku 4.5 和 Claude Opus 4/4.1/4.5 目前支援此功能。

自動工具呼叫清除是 Anthropic Claude 模型功能 （測試版）。透過此功能，Claude 可以在接近字符限制時自動清除舊工具使用結果，以便在多迴轉工具使用案例中實現更有效率的內容管理。若要使用工具使用清除，您需要將 `context-management-2025-06-27` 新增至 anthropic\_beta 請求參數上的 Beta 標頭清單。您還需要指定 的使用`clear_tool_uses_20250919`，並從下列組態選項中選擇。

這些是 `clear_tool_uses_20250919` 內容管理策略的可用控制項。全部皆為選用或具有預設值：


| **組態選項** | **Description** | 
| --- | --- | 
| `trigger`<br />預設：100,000 個輸入字符 | 定義內容編輯策略何時啟用。一旦提示超過此閾值，就會開始清除。您可以在 input\_tokens 或 tool\_uses 中指定此值。 | 
| `keep`<br />預設：3 次工具使用 | 定義清除發生後要保留的最近工具使用/結果對數量。API 會先移除最舊的工具互動，並保留最新的工具互動。當模型需要存取最近的工具互動，以有效繼續對話時很有幫助。 | 
| `clear_at_least` (選用) | 確保在每次啟用策略時清除的字符數目下限。如果 API 無法清除至少指定的數量，則不會套用策略。這有助於判斷內容清除是否值得中斷您的提示快取。 | 
| `exclude_tools` (選用) | 不應清除其工具使用和結果的工具名稱清單。適用於保留重要內容。 | 
| `clear_tool_inputs` (選用，預設值 False) | 控制是否將工具呼叫參數與工具結果一起清除。根據預設，只有工具結果會清除，同時保持 Claude 的原始工具呼叫可見，因此即使在移除結果之後，Claude 也能看到執行的操作。 | 

**注意**  
如果您的字首包含您的工具，工具清除將使快取失效。

**重要**  
Amazon Bedrock 不支援 Anthropic `web_search_20250305` 伺服器工具。

------
#### [ Request ]

```
from anthropic import AnthropicBedrock

client = AnthropicBedrock()

response = client.beta.messages.create(
    betas=["context-management-2025-06-27"],
    model="claude-sonnet-4-20250514",
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content": "Create a simple command line calculator app using Python"
       }
    ],
    tools=[
        {
            "type": "text_editor_20250728",
            "name": "str_replace_based_edit_tool",
            "max_characters": 10000
        }
    ],
    extra_body={
        "context_management": {
            "edits": [
                {
                    "type": "clear_tool_uses_20250919",
                # The below parameters are OPTIONAL:
                    # Trigger clearing when threshold is exceeded
                    "trigger": {
                        "type": "input_tokens",
                        "value": 30000
                    },
                    # Number of tool uses to keep after clearing
                    "keep": {
                        "type": "tool_uses",
                        "value": 3
                    },
                    # Optional: Clear at least this many tokens
                    "clear_at_least": {
                        "type": "input_tokens",
                        "value": 5000
                    },
                    # Exclude these tools uses from being cleared
                    "exclude_tools": ["str_replace_based_edit_tool"]
                }
            ]
       }
    }
 )
```

------
#### [ Response ]

```
{
    "id": "msg_123",
    "type": "message",
    "role": "assistant",
    "content": [
        {
            "type": "tool_use",
            "id": "toolu_456",
            "name": "data_analyzer",
            "input": {
                "data": "sample data"
            }
        }
    ],
    "context_management": {
        "applied_edits": [
            {
                "type": "clear_tool_uses_20250919",
                "cleared_tool_uses": 8,  # Number of tool use/result pairs that were cleared
                "cleared_input_tokens": 50000  # Total number of input tokens removed from the prompt
            }
        ]
    }
    "stop_reason": "tool_use",
    "usage": {
        "input_tokens": 150,
        "output_tokens": 50
    }
}
```

------
#### [ Streaming Response ]

```
data: {"type": "message_start", "message": {"id": "msg_123", "type": "message", "role": "assistant"}}

data: {"type": "content_block_start", "index": 0, "content_block": {"type": "tool_use", "id": "toolu_456", "name": "data_analyzer", "input": {}}}

data: {"type": "content_block_delta", "index": 0, "delta": {"type": "input_json_delta", "partial_json": "{\"data\": \"sample"}}

data: {"type": "content_block_delta", "index": 0, "delta": {"type": "input_json_delta", "partial_json": " data\"}"}}

data: {"type": "content_block_stop", "index": 0}

data: {"type": "message_delta", "delta": {"stop_reason": "tool_use"}}

data: {"type": "message_stop"}

{
  "type": "message_delta",
  "delta": {
    "stop_reason": "end_turn",
    "stop_sequence": null,
  },
  "usage": {
    "output_tokens": 1024
  },
  "context_management": {
    "applied_edits": [...],
  }
}
```

------

**注意**  
Bedrock 目前不支援 CountTokens API 上的 `clear_tool_uses_20250919` 內容管理。

## 記憶體工具 (Beta 版)
<a name="model-parameters-anthropic-claude-memory-tool"></a>

**警告**  
Memory Tool 以「Beta Service」的形式提供，如 AWS 服務條款所定義。

Claude Sonnet 4.5 包含新的記憶體工具。此工具可讓您跨對話管理記憶體。使用此功能，您可以透過提供本機目錄的存取權，允許 Claude 擷取內容視窗外的資訊。此功能可在 Beta 版中使用。若要使用此功能，您必須在 `anthropic_beta` 參數`context-management-2025-06-27`中包含 。

工具定義：

```
{
  "type": "memory_20250818",
  "name": "memory"
}
```

範例請求：

```
{
    "max_tokens": 2048,
    "anthropic_version": "bedrock-2023-05-31",
    "anthropic_beta": ["context-management-2025-06-27"],
    "tools": [{
        "type": "memory_20250818",
        "name": "memory"
    }],
    "messages": [
        {
            "role": "user",
            "content": [{"type": "text", "text": "Remember that my favorite color is blue and I work at Amazon?"}]
        }
    ]
}
```

回應範例：

```
{
    "id": "msg_vrtx_014mQ5ficCRB6PEa5k5sKqHd",
    "type": "message",
    "role": "assistant",
    "model": "claude-sonnet-4-20250514",
    "content": [
        {
            "type": "text",
            "text": "I'll start by checking your memory directory and then record this important information about you."
        },
        {
            "type": "tool_use",
            "id": "toolu_vrtx_01EU1UrCDigyPMRntr3VYvUB",
            "name": "memory",
            "input": {
                "command": "view",
                "path": "/memories"
            }
        }
    ],
    "stop_reason": "tool_use",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 1403,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 0,
        "output_tokens": 87
    },
    "context_management": {
        "applied_edits": []
    }
}
```

## 工具使用的成本考量
<a name="model-parameters-anthropic-claude-tool-use-cost"></a>

工具使用請求會根據下列因素定價：

1. 傳送至模型的輸入字符總數 (包括在工具參數中)。

1. 產生的輸出字符數目。

工具的定價與所有其他 Claude API 請求相同，但每個請求都包含額外的字符。工具使用的其他字符來自下列各項：
+ API 請求中的 `tools` 參數。例如，工具名稱、描述和結構描述。
+ API 請求和回應中的任何 `tool_use` 內容區塊。
+ API 請求中的任何 `tool_result` 內容區塊。

當您使用工具時，Anthropic 模型會自動包含啟用工具使用的特殊系統提示。下表列出每個模型所需的工具使用字符數量。此資料表會排除先前所述的其他字符。請注意，此表格假設至少提供一個工具。如果未提供任何工具，則無工具選擇會使用 0 個額外的系統提示字符。


| 模型 | 工具選擇 | 工具使用系統提示字符計數 | 
| --- | --- | --- | 
| Claude Opus 4.5<br />Claude Opus 4.1<br />Claude Opus 4<br />Claude Sonnet 4.5<br />Claude Haiku 4.5<br />Claude Sonnet 4<br />Claude 3.7 Sonnet<br />Claude 3.5 Sonnet v2 | auto 或 none | 346 | 
| Claude Opus 4.5<br />Claude Opus 4.1<br />Claude Opus 4<br />Claude Sonnet 4.5<br />Claude Haiku 4.5<br />Claude Sonnet 4<br />Claude 3.7 Sonnet<br />Claude 3.5 Sonnet v2 | any 或 tool | 313 | 
| Claude 3.5 Sonnet | auto 或 none | 294 | 
| Claude 3.5 Sonnet | any 或 tool | 261 | 
| Claude 3 Opus | auto 或 none | 530 | 
| Claude 3 Opus | any 或 tool | 281 | 
| Claude 3 Sonnet | auto 或 none | 159 | 
| Claude 3 Sonnet | any 或 tool | 235 | 
| Claude 3 Haiku | auto 或 none | 264 | 
| Claude 3 Haiku | any 或 tool | 340 | 

## 工具搜尋工具 (Beta 版）
<a name="model-parameters-anthropic-claude-tool-search-tool"></a>

工具搜尋工具允許 使用數百甚至數千種工具Claude，而無需預先將其所有定義載入內容視窗。您可以用 標記所有工具，並僅透過工具搜尋機制Claude尋找和載入所需的工具`defer_loading: true`，而不是立即宣告所有工具。

若要存取此功能，您必須在 `anthropic_beta` 參數`tool-search-tool-2025-10-19`中包含 。請注意，此功能目前只能透過 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 和 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) APIs使用。

工具定義：

```
{
    "type": "tool_search_tool_regex",
    "name": "tool_search_tool_regex"
}
```

請求範例：

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "anthropic_beta": [
        "tool-search-tool-2025-10-19"
    ],
    "max_tokens": 4096,
    "tools": [{
            "type": "tool_search_tool_regex",
            "name": "tool_search_tool_regex"
        },
        {
            "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
        },
        {
            "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
        }
    ],
    "messages": [{
        "role": "user",
        "content": "What's the weather in Seattle?"
    }]
}
```

回應範例

```
{
    "role": "assistant",
    "content": [{
            "type": "text",
            "text": "I'll search for the appropriate tools to help with this task."
        },
        {
            "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": "Now I can check the weather."
        },
        {
            "type": "tool_use",
            "id": "toolu_01XYZ789",
            "name": "get_weather",
            "input": {
                "location": "Seattle",
                "unit": "fahrenheit"
            }
        }
    ],
    "stop_reason": "tool_use"
}
```

串流範例

```
# Event 1: content_block_start(with complete server_tool_use block) {
    "type": "content_block_start",
    "index": 0,
    "content_block": {
        "type": "server_tool_use",
        "id": "srvtoolu_01ABC123",
        "name": "tool_search_tool_regex"
    }
}

# Event 2: content_block_delta(input JSON streamed) {
    "type": "content_block_delta",
    "index": 0,
    "delta": {
        "type": "input_json_delta",
        "partial_json": "{\"regex\": \".*weather.*\"}"
    }
}

# Event 3: content_block_stop(tool_use complete) {
    "type": "content_block_stop",
    "index": 0
}

# Event 4: content_block_start(COMPLETE result in single chunk) {
    "type": "content_block_start",
    "index": 1,
    "content_block": {
        "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"
            }]
        }
    }
}

# Event 5: content_block_stop(result complete) {
    "type": "content_block_stop",
    "index": 1
}
```

**自訂工具搜尋工具**  
您可以透過定義傳回`tool_reference`區塊的工具來實作自訂工具搜尋工具 （例如，使用內嵌）。自訂工具必須具有 ，`defer_loading: false`而其他工具應該具有 `defer_loading: true`。當您定義自己的工具搜尋工具時，應該會傳回工具結果，其中包含指向Claude您要使用之工具`tool_reference`的內容區塊。

預期的客戶定義工具搜尋工具結果回應格式：

```
{
    "type": "tool_result",
    "tool_use_id": "toolu_01ABC123",
    "content": [{
            "type": "tool_reference",
            "tool_name": "get_weather"
        },
        {
            "type": "tool_reference",
            "tool_name": "weather_forecast"
        }
    ]
}
```

`tool_name` 必須符合 請求中定義的工具`defer_loading: true`。然後，Claude 將可存取這些工具的完整結構描述。

**自訂搜尋工具 - 詳細範例**  
您可以透過定義傳回`tool_reference`區塊的工具來實作自訂工具搜尋工具 （例如，使用內嵌或語意搜尋）。這可讓複雜的工具探索機制超越 regex 比對。

使用自訂 TST 請求範例：

```
{
    "model": "claude-sonnet-4-5-20250929",
    "anthropic_version": "bedrock-2023-05-31",
    "anthropic_beta": ["tool-search-tool-2025-10-19"],
    "max_tokens": 4096,
    "tools": [{
            "name": "semantic_tool_search",
            "description": "Search for available tools using semantic similarity. Returns the most relevant tools for the given query.",
            "input_schema": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Natural language description of what kind of tool is needed"
                    },
                    "top_k": {
                        "type": "integer",
                        "description": "Number of tools to return (default: 5)"
                    }
                },
                "required": ["query"]
            },
            "defer_loading": false
        },
        {
            "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
        },
        {
            "name": "search_flights",
            "description": "Search for available flights between locations",
            "input_schema": {
                "type": "object",
                "properties": {
                    "origin": {
                        "type": "string"
                    },
                    "destination": {
                        "type": "string"
                    },
                    "date": {
                        "type": "string"
                    }
                },
                "required": ["origin", "destination", "date"]
            },
            "defer_loading": true
        }
    ],
    "messages": [{
        "role": "user",
        "content": "What's the weather forecast in Seattle for the next 3 days?"
    }]
}
```

Claude的回應 （呼叫自訂 TST)：

```
{
    "role": "assistant",
    "content": [{
            "type": "text",
            "text": "I'll search for the appropriate tools to help with weather information."
        },
        {
            "type": "tool_use",
            "id": "toolu_01ABC123",
            "name": "semantic_tool_search",
            "input": {
                "query": "weather forecast multiple days",
                "top_k": 3
            }
        }
    ],
    "stop_reason": "tool_use"
}
```

**客戶提供的工具結果**  
在工具程式庫上執行語意搜尋後，客戶會傳回相符的工具參考：

```
{
    "role": "user",
    "content": [{
        "type": "tool_search_tool_result",
        "tool_use_id": "toolu_01ABC123",
        "content": {
            "type": "tool_search_tool_search_result",
            "tool_references": [{
                "type": "tool_reference",
                "tool_name": "get_weather"
            }]
        }
    }]
}
```

Claude的追蹤 （使用探索到的工具）

```
{
    "role": "assistant",
    "content": [{
            "type": "text",
            "text": "I found the forecast tool. Let me get the weather forecast for Seattle."
        },
        {
            "type": "tool_use",
            "id": "toolu_01DEF456",
            "name": "get_weather",
            "input": {
                "location": "Seattle, WA"
            }
        }
    ],
    "stop_reason": "tool_use"
}
```

**錯誤處理**
+ `defer_loading: true` 設定所有工具 （包括工具搜尋工具） 都會擲回 400 錯誤。
+ 在沒有對應工具定義`tool_reference`的情況下傳遞 會擲回 400 錯誤

## 工具使用範例 (Beta 版）
<a name="model-parameters-anthropic-claude-tool-use-examples"></a>

Claude Opus 4.5 支援使用者在工具定義中提供的範例，以提高 Claude的工具使用效能。您可以提供完整函數呼叫的範例，格式與實際 LLM 輸出完全相同，而不需要翻譯成其他格式。若要使用此功能，您必須在 `anthropic_beta` 參數`tool-examples-2025-10-29`中包含 。

工具定義範例：

```
{
    "name": "get_weather",
    "description": "Get the current weather in a given location",
    "input_schema": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"],
                "description": "Temperature unit"
            }
        },
        "required": ["location"]
    },
    "input_examples": [{
            "location": "San Francisco, CA",
            "unit": "fahrenheit"
        },
        {
            "location": "Tokyo, Japan",
            "unit": "celsius"
        },
        {
            "location": "New York, NY"
        }
    ]
}
```

**驗證規則**
+ 結構描述一致性： 中的每個範例`input_examples`都必須根據工具的 有效`input_schema`。
  + 必要欄位必須存在於至少一個範例中。
  + 欄位類型必須符合結構描述。
  + 列舉值必須來自允許的集合。
  + 如果驗證失敗，請傳回 400 錯誤，其中包含驗證失敗之範例的詳細資訊。
+ 陣列需求： `input_examples` 必須是陣列 （可以是空的）。
  + 空陣列`[]`有效且等同於省略 欄位。
  + 單一範例仍必須包裝在 陣列中： `[{...}]`
  + 長度限制：從每個工具定義 20 個範例的限制開始。

錯誤範例：

```
// Invalid: Example doesn't match schema (missing required field)
{
    "type": "invalid_request_error",
    "message": "Tool 'get_weather' input_examples[0] is invalid: Missing required property 'location'"
}

// Invalid: Example has wrong type for field
{
    "type": "invalid_request_error",
    "message": "Tool 'search_products' input_examples[1] is invalid: Property 'filters.price_range.min' must be a number, got string"
}

// Invalid: input_examples on server-side tool
{
    "type": "invalid_request_error",
    "message": "input_examples is not supported for server-side tool"
}
```