

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

# Anthropic Claude 模型
<a name="model-parameters-claude"></a>

本節說明 Anthropic Claude 模型的請求參數和回應欄位。使用此資訊透過 [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) (串流) 操作對 Anthropic Claude 模型進行推論呼叫。本節也包含 Python 程式碼範例，示範如何呼叫 Anthropic Claude 模型。若要在推論操作中使用模型，您需要模型的模型 ID。若要取得模型 ID，請參閱[Amazon Bedrock 中支援的基礎模型](models-supported.md)。某些模型也可以使用 [Converse API](conversation-inference.md)。若要檢查 Converse API 是否支援特定 Anthropic Claude 模型，請參閱 [支援的模型和模型功能](conversation-inference-supported-models-features.md)。如需更多程式碼範例，請參閱 [使用 AWS SDKs Amazon Bedrock 程式碼範例](service_code_examples.md)。

Amazon Bedrock 中的基礎模型支援輸入和輸出模態，因模型而異。若要檢查 Anthropic Claude 模型支援的模態，請參閱 [Amazon Bedrock 中支援的基礎模型](models-supported.md)。若要檢查 Anthropic Claude 模型支援的 Amazon Bedrock 功能，請參閱 [Amazon Bedrock 中支援的基礎模型](models-supported.md)。若要檢查哪些AWS區域提供AnthropicClaude模型，請參閱 [Amazon Bedrock 中支援的基礎模型](models-supported.md)。

當您使用 Anthropic Claude 模型進行推論呼叫時，您會包含模型的提示。如需建立 Amazon Bedrock 支援之模型提示的相關資訊，請參閱 [提示工程概念](prompt-engineering-guidelines.md)。如需 Anthropic Claude 特定提示資訊，請參閱《[Anthropic Claude 提示工程指南](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview)》。

您可以使用 Amazon Bedrock 傳送 [Anthropic Claude 文字完成 API](model-parameters-anthropic-claude-text-completion.md) 或 [Anthropic Claude 訊息 API](model-parameters-anthropic-claude-messages.md) 推論請求。

您可以使用訊息 API 來建立對話式應用程式，例如虛擬助理或指導應用程式。將文字完成 API 用於單回合文字產生應用程式。例如，產生部落格文章的文字或摘要使用者提供的文字。

Anthropic Claude 模型支援使用 XML 標籤來建構和描述您的提示。例如，您可以在提示中以 `<examples>` 標籤括住範例。使用描述性標籤名稱以獲得最佳結果。如需詳細資訊，請參閱《[Anthropic 使用者指南](https://docs.anthropic.com/en/docs/welcome)》中的[使用 XML 標籤](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/use-xml-tags)。

Anthropic Claude 模型支援使用 PDF 文件處理和引用。引用提供對回應中模型所使用文件中資訊的參考。

**注意**  
若要在推論呼叫中使用系統提示，您必須使用 2.1 或更新版本的AnthropicClaude版本。  
如需有關建立系統提示的資訊，請參閱 Anthropic Claude 文件中的[使用系統提示給予 Claude 角色](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/system-prompts)。  
為了避免 Anthropic Claude version 2.1 版逾時，我們建議將 `prompt` 欄位中的輸入字符計數限制為 180K。我們預計很快就會解決這個逾時問題。

在推論呼叫中，將符合您要進行之呼叫類型 ([Anthropic Claude 文字完成 API](model-parameters-anthropic-claude-text-completion.md) 或 [Anthropic Claude 訊息 API](model-parameters-anthropic-claude-messages.md)) 的 JSON 物件填入 `body` 欄位。

**Topics**
+ [Anthropic Claude 文字完成 API](model-parameters-anthropic-claude-text-completion.md)
+ [Anthropic Claude 訊息 API](model-parameters-anthropic-claude-messages.md)

# Anthropic Claude 文字完成 API
<a name="model-parameters-anthropic-claude-text-completion"></a>

本節提供推論參數和使用 Anthropic Claude 模型搭配文字完成 API 的程式碼範例。

**Topics**
+ [Anthropic Claude 文字完成 API 概觀](#model-parameters-anthropic-claude-text-completion-overview)
+ [支援的模型](#claude-messages-supported-models)
+ [請求與回應](#model-parameters-anthropic-claude-text-completion-request-response)
+ [程式碼範例](#api-inference-examples-claude-text-completion)

## Anthropic Claude 文字完成 API 概觀
<a name="model-parameters-anthropic-claude-text-completion-overview"></a>

使用文字完成 API，從使用者提供的提示產生單回合文字。例如，您可以使用文字完成 API 來產生部落格文章的文字，或摘要來自使用者的文字輸入。

如需建立 Anthropic Claude 模型提示的相關資訊，請參閱[提示設計簡介](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design)。如果您想要搭配 [Anthropic Claude 訊息 API](model-parameters-anthropic-claude-messages.md) 使用現有的文字完成提示，請參閱[從文字完成遷移](https://docs.anthropic.com/claude/reference/migrating-from-text-completions-to-messages)。

## 支援的模型
<a name="claude-messages-supported-models"></a>

您可以搭配下列 Anthropic Claude 模型使用文字完成 API。
+ Anthropic Claude Instant v1.2
+ Anthropic Claude v2
+ Anthropic Claude v2.1 

## 請求與回應
<a name="model-parameters-anthropic-claude-text-completion-request-response"></a>

請求本文在請求 `body` 欄位中傳遞到 [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)。

如需詳細資訊，請參閱 Anthropic Claude 文件中的 [https://docs.anthropic.com/claude/reference/complete\$1post](https://docs.anthropic.com/claude/reference/complete_post)。

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

Anthropic Claude 具有下列文字完成推論呼叫的推論參數。

```
{
    "prompt": "\n\nHuman:<prompt>\n\nAssistant:",
    "temperature": float,
    "top_p": float,
    "top_k": int,
    "max_tokens_to_sample": int,
    "stop_sequences": [string]
}
```

以下是必要的參數。
+  **prompt** – (必要) 您想要讓 Claude 完成的提示。為了產生適當的回應，您需要使用交替 `\n\nHuman:` 和 `\n\nAssistant:` 對話回合來格式化提示。例如：

  ```
  "\n\nHuman: {userQuestion}\n\nAssistant:"
  ```

  如需詳細資訊，請參閱 Anthropic Claude 文件中的[提示驗證](https://docs.anthropic.com/claude/reference/prompt-validation)。
+  **max\$1tokens\$1to\$1sample** – (必要) 停止之前要產生的字符數目上限。我們建議 4,000 個記號上限以獲得最佳效能。

  請注意，Anthropic Claude 模型可能會在達到 `max_tokens_to_sample` 的值之前停止產生字符。不同的 Anthropic Claude 模型對此參數具有不同的最大值。如需詳細資訊，請參閱 Anthropic Claude 文件中的[模型比較](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)

以下是選用參數。
+  **stop\$1sequences** – (選用) 導致模型停止產生的序列。

  Anthropic Claude 模型會在 `"\n\nHuman:"` 上停止，且未來可能包含額外的內建停止序列。使用 `stop_sequences` 推論參數來包含其他字串，向模型發送訊號以停止產生文字。
+  **temperature** – (選用) 注入回應的隨機性量。對於分析/複選，請使用接近 0 的值，對於創意和生成任務，請使用接近 1 的值。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)
+  **top\$1p** – (選用) 使用核心取樣。

  在核心取樣中，Anthropic Claude 會以遞減的機率順序計算每個後續字符所有選項的累積分佈，並在達到 `top_p` 指定的特定機率時將其切斷。您應該更改 `temperature` 或 `top_p`，但不能同時更改。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)
+  **top\$1k** – (選用) 僅針對每個後續字符杖從前 K 個選項中取樣。

  使用 `top_k` 移除長尾低機率回應。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)

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

此 Anthropic Claude 模型會傳回文字完成推論呼叫的下列欄位。

```
{
    "completion": string,
    "stop_reason": string,
    "stop": string
}
```
+ **completion** – 產生的完成直到並排除停止序列。
+ **stop\$1reason** – 模型停止產生回應的原因。
  + **"stop\$1sequence"** – 模型已達到停止序列 — 您使用 `stop_sequences` 推論參數提供，或模型內建的停止序列。
  + **"max\$1tokens"** – 超過模型 `max_tokens_to_sample`，或模型的字符數目上限。
+ **stop** – 如果您指定 `stop_sequences` 推論參數，則 `stop` 包含向模型發送訊號以停止產生文字的停止序列。例如，下列回應中的 `holes`。

  ```
  {
      "completion": " Here is a simple explanation of black ",
      "stop_reason": "stop_sequence",
      "stop": "holes"
  }
  ```

  如未指定 `stop_sequences`，`stop` 的值將會是空白。

------

## 程式碼範例
<a name="api-inference-examples-claude-text-completion"></a>

這些範例展示如何利用隨需輸送量呼叫 *Anthropic Claude V2* 模型。若要使用 Anthropic Claude version 2.1，請將 `modelId` 的值變更為 `anthropic.claude-v2:1`。

```
import boto3
import json
brt = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
    "prompt": "\n\nHuman: explain black holes to 8th graders\n\nAssistant:",
    "max_tokens_to_sample": 300,
    "temperature": 0.1,
    "top_p": 0.9,
})

modelId = 'anthropic.claude-v2'
accept = 'application/json'
contentType = 'application/json'

response = brt.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)

response_body = json.loads(response.get('body').read())

# text
print(response_body.get('completion'))
```

以下範例展示如何使用 Python 產生串流文字，並使用提示*寫一篇關於在火星生活的 1000 字文章*以及 Anthropic Claude V2 模型：

```
import boto3
import json

brt = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
    'prompt': '\n\nHuman: write an essay for living on mars in 1000 words\n\nAssistant:',
    'max_tokens_to_sample': 4000
})
                   
response = brt.invoke_model_with_response_stream(
    modelId='anthropic.claude-v2', 
    body=body
)
    
stream = response.get('body')
if stream:
    for event in stream:
        chunk = event.get('chunk')
        if chunk:
            print(json.loads(chunk.get('bytes').decode()))
```

# Anthropic Claude 訊息 API
<a name="model-parameters-anthropic-claude-messages"></a>

本節提供使用 Anthropic Claude 訊息 API 的推論參數和程式碼範例。

**Topics**
+ [Anthropic Claude 訊息 API 概觀](#model-parameters-anthropic-claude-messages-overview)
+ [工具使用](model-parameters-anthropic-claude-messages-tool-use.md)
+ [延伸思考](claude-messages-extended-thinking.md)
+ [適應性思維](claude-messages-adaptive-thinking.md)
+ [思考加密](claude-messages-thinking-encryption.md)
+ [跨模型版本思考的差異](claude-messages-thinking-differences.md)
+ [壓縮](claude-messages-compaction.md)
+ [從模型取得經過驗證的 JSON 結果](claude-messages-structured-outputs.md)
+ [請求與回應](model-parameters-anthropic-claude-messages-request-response.md)
+ [程式碼範例](api-inference-examples-claude-messages-code-examples.md)
+ [支援的模型](claude-messages-supported-models.md)

## Anthropic Claude 訊息 API 概觀
<a name="model-parameters-anthropic-claude-messages-overview"></a>

您可以使用訊息 API 來建立聊天機器人或虛擬助理應用程式。API 會管理使用者與 Anthropic Claude 模型 (助手) 之間的對話式交換。

**注意**  
本主題說明如何搭配使用 Anthropic Claude 訊息 API 與基本推論操作 ([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))。不過，我們建議您使用 Converse API 在應用程式中實作訊息。Converse API 提供一組統一的參數，可用於支援訊息的所有模型。如需詳細資訊，請參閱[與 Converse API 操作進行對話](conversation-inference.md)。
限制適用於下列操作：`InvokeModel`、`Converse`、 `InvokeModelWithResponseStream`和 `ConverseStream`。如需詳細資訊，請參閱 [API 限制](inference-api-restrictions.md)。

Anthropic 會訓練 Claude 模型在交替使用者和助理對話回合時操作。建立新訊息時，您可以使用訊息參數指定先前的對話回合。模型接著會在對話中產生下一個訊息。

每個輸入訊息必須是具有角色和內容的物件。您可以指定單一使用者角色訊息，也可以包含多個使用者和助理訊息。

如果您使用從 Claude 預先填入回應的技術 (使用最終助理角色訊息在 Claude 回應的開頭填入)，Claude 將透過從您離開的位置挑選來回應。使用此技術時，Claude 仍會傳回具有助理角色的回應。

如果最終訊息使用助理角色，回應內容會立即從該訊息中的內容繼續。您可以使用它來限制模型回應的一部分。

具有單一使用者訊息的範例：

```
[{"role": "user", "content": "Hello, Claude"}]
```

具有多個對話回合的範例：

```
[
  {"role": "user", "content": "Hello there."},
  {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
  {"role": "user", "content": "Can you explain LLMs in plain English?"},
]
```

來自 Claude 部分填入回應的範例：

```
[
  {"role": "user", "content": "Please describe yourself using only JSON"},
  {"role": "assistant", "content": "Here is my JSON description:\n{"},
]
```

每個輸入訊息內容可能是單一字串或內容區塊陣列，其中每個區塊都有特定類型。對於類型為 "text" 的單一內容區塊陣列，使用字串是一種速記。下列輸入訊息相當於：

```
{"role": "user", "content": "Hello, Claude"}
```

```
{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}
```

如需有關建立 Anthropic Claude 模型提示的資訊，請參閱 Anthropic Claude 文件中的[提示簡介](https://docs.anthropic.com/claude/docs/intro-to-prompting)。如果您有要遷移至訊息 API 的現有[文字完成](model-parameters-anthropic-claude-text-completion.md)提示，請參閱[從文字完成遷移](https://docs.anthropic.com/claude/reference/migrating-from-text-completions-to-messages)。

**重要**  
Anthropic Claude 3.7 Sonnet 和 Claude 4 模型的推論呼叫逾時期間為 60 分鐘。根據預設， AWS SDK 用戶端會在 1 分鐘後逾時。建議您將 AWS SDK 用戶端的讀取逾時期間增加到至少 60 分鐘。例如，在 AWS Python botocore SDK 中，將 [botocore.config](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#) 中的 `read_timeout` 欄位值變更為至少 3600。

### 系統提示
<a name="model-parameters-anthropic-claude-messages-system-prompts"></a>

您也可以在請求中包含系統提示。系統提示可讓您提供內容和指示給 Anthropic Claude，例如指定特定目標或角色。在 `system` 欄位中指定系統提示，如下列範例所示。

```
"system": "You are Claude, an AI assistant created by Anthropic to be helpful,
                harmless, and honest. Your goal is to provide informative and substantive responses
                to queries while avoiding potential harms."
```

如需詳細資訊，請參閱 Anthropic 文件中的[系統提示](https://docs.anthropic.com/en/docs/system-prompts)。

### 多模態提示
<a name="model-parameters-anthropic-claude-messages-multimodal-prompts"></a>

多模態提示會在單一提示中結合多個模態 (影像和文字)。您可以在 `content` 輸入欄位中指定模態。下列範例顯示您可以如何要求 Anthropic Claude 描述所提供影像的內容。如需範例程式碼，請參閱 [多模態程式碼範例](api-inference-examples-claude-messages-code-examples.md#api-inference-examples-claude-multimodal-code-example)。

```
{
    "anthropic_version": "bedrock-2023-05-31", 
    "max_tokens": 1024,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": "iVBORw..."
                    }
                },
                {
                    "type": "text",
                    "text": "What's in these images?"
                }
            ]
        }
    ]
}
```

您在請求中包含的每個影像都會計入您的字符用量。如需詳細資訊，請參閱 Anthropic 文件中的[影像成本](https://docs.anthropic.com/claude/docs/vision#image-costs)。

# 工具使用
<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>

電腦使用是 Claude 3.5 Sonnet v2、Claude Sonnet 4.5、Claude Haiku 4.5、Claude 3.7 Sonnet、Claude Sonnet 4 和 Claude Opus 4 提供的 Anthropic Claude 模型功能 (Beta 版)。在使用電腦時，Claude 可協助您透過基本的 GUI 動作自動化任務。

**警告**  
電腦使用功能會以「Beta Service」的形式提供給您，如 AWS 服務條款所定義。它受您與 AWS 和 服務條款以及適用模型 EULA 的協議約束 AWS 。請注意，電腦使用 API 會產生與標準 API 功能或聊天介面不同的唯一風險。使用電腦使用 API 與網際網路互動時，這些風險會提高。為了將風險降至最低，請考慮採取預防措施，例如：  
在專用虛擬機器或容器中以最低權限操作電腦使用功能，以防止直接系統攻擊或意外事故。
為避免資訊遭竊，請避免讓電腦使用 API 存取敏感帳戶或資料。
限制電腦使用 API 對必要網域的網際網路存取，以減少對惡意內容的暴露。
為了確保適當的監督，請在執行敏感任務 (例如可能對現實世界產生重大影響的決策) 以及需要明確同意的任何項目 (例如接受 Cookie、執行金融交易，或同意服務條款) 時，保留人工介入環節。
您啟用 Claude 以查看或存取的任何內容都可能覆寫指示，或導致 Claude 發生錯誤或執行意外動作。採取適當的預防措施 (例如將 Claude 與敏感表面隔離) 至關重要，包括避免與提示注入相關的風險。在啟用或請求啟用自有產品中電腦使用功能所需的許可之前，請通知最終使用者任何相關風險，並適時取得其同意。

電腦使用 API 提供數個預先定義的電腦使用工具供您使用。然後，您可以使用請求建立提示，例如「傳送電子郵件給 Ben，其中包含上次會議的筆記」和螢幕擷取畫面 (必要時)。回應包含 JSON 格式的 `tool_use` 動作清單 (例如 scroll\$1down、left\$1button\$1press、螢幕擷取畫面)。程式碼會執行電腦動作，並向 Claude 提供顯示輸出的螢幕擷取畫面 (當要求時)。

自 Claude 3.5 v2 發行以來，工具參數已更新為接受多型工具類型；已新增 `tool.type` 屬性來區分它們。`type` 是選用的；如果忽略，則假設工具是自訂工具 (先前支援的唯一工具類型)。若要存取電腦使用，您必須使用 `anthropic_beta` 參數搭配對應的列舉，其值取決於使用的模型版本。如需詳細資訊，請參閱下表。

只有使用此參數和列舉提出的請求才能使用電腦使用工具。其可以指定如下：`"anthropic_beta": ["computer-use-2025-01-24"]`。


| 模型 | Beta 版標頭 | 
| --- | --- | 
|  Claude Opus 4.5 Claude Opus 4.1 Claude Opus 4 Claude Sonnet 4.5 Claude Haiku 4.5 Claude Sonnet 4 Claude 3.7 Sonnet  | computer-use-2025-01-24 | 
| Claude 3.5 Sonnet v2 | computer-use-2024-10-22 | 

如需詳細資訊，請參閱 Anthropic 文件中的[電腦使用 (Beta 版)](https://docs.anthropic.com/en/docs/build-with-claude/computer-use)。

以下是範例回應，假設請求包含具有 Firefox 圖示的桌面螢幕擷取畫面。

```
{
    "id": "msg_123",
    "type": "message",
    "role": "assistant",
    "model": "anthropic.claude-3-5-sonnet-20241022-v2:0",
    "content": [
        {
            "type": "text",
            "text": "I see the Firefox icon. Let me click on it and then navigate to a weather website."
        },
        {
            "type": "tool_use",
            "id": "toolu_123",
            "name": "computer",
            "input": {
                "action": "mouse_move",
                "coordinate": [
                    708,
                    736
                ]
            }
        },
        {
            "type": "tool_use",
            "id": "toolu_234",
            "name": "computer",
            "input": {
                "action": "left_click"
            }
        }
    ],
    "stop_reason": "tool_use",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 3391,
        "output_tokens": 132
    }
}
```

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

Anthropic 提供一組工具，讓特定 Claude 模型有效地使用電腦。指定 Anthropic 定義的工具時，不需要或不允許 `description` 和 `tool_schema` 欄位。Anthropic 定義的工具是由 Anthropic 定義，但您必須明確評估工具的結果，並將 `tool_results` 傳回給 Claude。如同任何工具，模型不會自動執行工具。每個 Anthropic 定義的工具都有針對特定模型 Claude 3.5 Sonnet (新) 和 Claude 3.7 Sonnet 最佳化的版本：


| 模型 | 工具 | 備註 | 
| --- | --- | --- | 
|  Claude Claude Opus 4.1 Claude Claude Opus 4 Claude Sonnet 4.5 Claude Haiku 4.5 Claude Sonnet 4  |  <pre>{ <br />    "type": "text_editor_20250124", <br />    "name": "str_replace_based_edit_tool" <br />}</pre>  | 更新現有 `str_replace_editor` 工具 | 
|  Claude 3.7 Sonnet  |  <pre>{ <br />    "type": "computer_20250124", <br />    "name": "computer" <br />}</pre>  |  包含新動作以更精確地控制  | 
|  Claude 3.7 Sonnet  |  <pre>{ <br />    "type": "text_editor_20250124", <br />    "name": "str_replace_editor"<br />}</pre>  | 與 20241022 版本相同的功能 | 
|  Claude 3.5 Sonnet v2  |  <pre>{ <br />    "type": "bash_20250124", <br />    "name": "bash" <br />}</pre>  |  與 20241022 版本相同的功能  | 
|  Claude 3.5 Sonnet v2  |  <pre>{ <br />    "type": "text_editor_20241022", <br />    "name": "str_replace_editor"<br />}</pre>  | 
|  Claude 3.5 Sonnet v2  |  <pre>{ <br />    "type": "bash_20241022", <br />    "name": "bash"<br />}</pre>  | 
|  Claude 3.5 Sonnet v2  |  <pre>{ <br />    "type": "computer_20241022", <br />    "name": "computer"<br />}</pre>  | 

`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\$1beta 請求參數上的 Beta 標頭清單。您也需要指定 的使用`clear_tool_uses_20250919`，並從下列組態選項中選擇。

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


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

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

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

```
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
        },
       {
            "type": "web_search_20250305",
            "name": "web_search",
            "max_uses": 3
       }
    ],
    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": ["web_search"]
                }
            ]
       }
    }
 )
```

------
#### [ 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 版功能提供。若要使用此功能，您必須使用 `context-management-2025-06-27` Beta 版標頭。

工具定義：

```
{
  "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 Claude Opus 4.1 Claude Opus 4 Claude Sonnet 4.5 Claude Haiku 4.5 Claude Sonnet 4 Claude 3.7 Sonnet Claude 3.5 Sonnet v2  | auto 或 none | 346 | 
|  Claude Opus 4.5 Claude Opus 4.1 Claude Opus 4 Claude Sonnet 4.5 Claude Haiku 4.5 Claude Sonnet 4 Claude 3.7 Sonnet 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`，而不是立即宣告所有工具。

若要存取此功能，您必須使用 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 輸出完全相同，而不需要翻譯成其他格式。若要使用此功能，您必須傳遞 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"
}
```

# 延伸思考
<a name="claude-messages-extended-thinking"></a>

延伸思考可為複雜任務提供 Claude 增強的推理功能，同時在交付最終答案之前，為其逐步思考程序提供不同程度的透明度。每當您啟用 Claude 的思考模式時，您將需要為 Claude 可用於其內部推理程序的字符數量上限設定預算。

支援的模型如下：


| 模型 | 模型 ID | 
| --- | --- | 
| Claude Opus 4.5 | `anthropic.claude-opus-4-5-20251101-v1:0` | 
| Claude Opus 4 | `anthropic.claude-opus-4-20250514-v1:0` | 
| Claude Sonnet 4 | `anthropic.claude-sonnet-4-20250514-v1:0` | 
| Claude Sonnet 4.5 | `anthropic.claude-sonnet-4-5-20250929-v1:0` | 
| Claude Haiku 4.5 | `anthropic.claude-haiku-4-5-20251001-v1:0` | 
| Claude 3.7 Sonnet | `anthropic.claude-3-7-sonnet-20250219-v1:0` | 
| Claude Sonnet 4.5 | `anthropic.claude-opus-4-5-20251101-v1:0` | 

**注意**  
API 行為在 Claude 3.7 和 Claude 4 模型之間有所不同。如需詳細資訊，請參閱[跨模型版本思考的差異](claude-messages-thinking-differences.md)。

**Topics**
+ [延伸思考的最佳實務和考量事項](#claude-messages-extended-thinking-bps)
+ [延伸思考的運作方式](#claude-messages-how-extended-thinking-works)
+ [如何使用延伸思考](#claude-messages-use-extended-thinking)
+ [工具使用的延伸思考](#claude-messages-extended-thinking-tool-use)
+ [思維區塊清除 (Beta 版）](#claude-messages-thinking-block-clearing)
+ [具有提示快取的延伸思考](#claude-messages-extended-thinking-prompt-caching)
+ [了解思考區塊快取行為](#claude-messages-extended-thinking-caching-behavior)
+ [具有延伸思考的字符上限和內容視窗大小](#claude-messages-extended-thinking-max-tokens)
+ [延伸思考字符成本考量](#claude-messages-extended-thinking-cost)

## 延伸思考的最佳實務和考量事項
<a name="claude-messages-extended-thinking-bps"></a>

使用指引
+ **任務選擇**：針對數學、編碼和分析等需要逐步推理的特別複雜任務，使用延伸思考。
+ **內容處理**：您不需要自行移除先前的思考區塊。Anthropic API 會自動忽略先前回合的思考區塊，而且計算內容用量時不會包含這些區塊。
+ **提示工程**：如果您想要最大化 Claude 的思考功能，請檢閱 Anthropic 的[延伸思考提示秘訣](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/extended-thinking-tips)。

效能考量
+ **回應時間**：由於推理程序所需的額外處理，因此請為可能較長的回應時間做好準備。考量產生思考區塊可能會增加整體回應時間。
+ **串流需求**：當 `max_tokens` 大於 21,333 時，需要串流。串流時，請準備好在 `thinking` 和 `text` 內容區塊到達時同時處理這些區塊。

功能相容性
+ 思考與 `temperature`、`top_p` 或 `top_k` 修改或[強制工具使用](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#forcing-tool-use)不相容。
+ 啟用思考時，您無法預先填入回應。
+ 變更思考預算會使內含訊息的快取提示字首失效。不過，當思考參數變更時，快取的系統提示和工具定義將繼續運作。

使用思考預算
+ **預算最佳化**：最低預算為 1,024 個字符。Anthropic 建議從最低值開始，並逐步提高思考預算，以找出適合您使用案例的最佳範圍。較大的字符計數可能允許更全面和細微的推理，但也可能根據任務減少回合。思考預算是目標 (而不是嚴格的限制) - 實際字符用量可能會根據任務而有所不同。
+ **最低和最佳設定**：最低預算為 1,024 個字符。我們建議您從最低值開始，並逐步提高思考預算，以尋找最適合您使用案例的 Claude 最佳範圍。較高的字符計數可能允許您實現更全面和細微的推理，但也可能根據任務減少回合。思考預算是目標 (而不是嚴格的限制) - 實際字符用量可能會根據任務而有所不同。
+ **實驗**：模型在不同的最大思考預算設定中可能會有不同的效能。提高思考預算上限可以讓模型有更完善或更深入的思考，但代價是延遲的增加。對於關鍵任務，請考慮測試不同的預算設定，以找出品質和效能之間的最佳平衡。
+ **大筆預算**：對於超過 32K 的思考預算，我們建議您使用批次處理來避免聯網問題。推送模型以思考超過 32K 字符的請求會導致長時間執行的請求，這可能會導致系統逾時和開放連線限制。請注意，`max_tokens` 限制因 Claude 模型而異。如需詳細資訊，請參閱[具有延伸思考的字符上限和內容視窗大小](#claude-messages-extended-thinking-max-tokens)。
+ **字符用量追蹤**：監控思考字符用量，以最佳化成本和效能。

## 延伸思考的運作方式
<a name="claude-messages-how-extended-thinking-works"></a>

開啟延伸思考時，Claude 會在輸出其內部推理的位置建立 `thinking` 內容區塊。Claude 會在製作最終回應之前納入此推理的洞見。API 回應將包含 `thinking` 內容區塊，後面接著 `text` 內容區塊。

以下是預設回應格式的範例：

```
{
  "content": [
    {
      "type": "thinking",
      "thinking": "Let me analyze this step by step...",
      "signature": "WaUjzkypQ2mUEVM36O2TxuC06KN8xyfbJwyem2dw3URve/op91XWHOEBLLqIOMfFG/UvLEczmEsUjavL...."
    },
    {
      "type": "text", 
      "text": "Based on my analysis..."
    }
  ]
}
```

如需延伸思考回應格式的詳細資訊，請參閱 Anthropic 的訊息 API [請求與回應](model-parameters-anthropic-claude-messages-request-response.md)。

## 如何使用延伸思考
<a name="claude-messages-use-extended-thinking"></a>

若要開啟延伸思考，請新增 `thinking` 物件，並將 `thinking` 參數設為已啟用，並將 `budget_tokens` 設定為指定的延伸思考字符預算。

`budget_tokens` 參數會決定 Claude 允許用於其內部推理程序的字符數量上限。在 Claude 4 模型中，此限制適用於完整的思考字符，不適用於摘要輸出。較大的預算可以透過對複雜問題進行更徹底的分析來改善回應品質，但 Claude 可能不會使用分配的整個預算，尤其是在 32K 以上的範圍。

`budget_tokens` 的值必須設定為小於 `max_tokens` 的值。不過，使用[交錯思考 (Beta 版)](#claude-messages-extended-thinking-tool-use-interleaved) 搭配工具時，您可以超過此限制，因為字符限制會成為整個內容視窗 (20 萬個字符)。

### 摘要思考
<a name="claude-messages-use-extended-thinking-summarized"></a>

啟用延伸思考後，Claude 4 模型的訊息 API 會傳回 Claude 完整思考程序的摘要。摘要思考提供延伸思考的完整智慧優勢，同時能夠防止誤用。

以下是摘要思考的一些重要考量：
+ 您需要為原始請求所產生的完整思考字符 (而不是摘要字符) 付費。
+ 計費輸出字符計數與您在回應中看到的字符計數不相符。
+ 提供給摘要器模型的提示可能會有所變更。
+ 思考輸出的前幾行更為詳細，提供詳細的推理，對於提示工程目的特別有用。

**注意**  
Claude 3.7 Sonnet 仍會傳回完整的思考輸出。  
若要存取 Claude 4 模型的完整思考輸出，請聯絡客戶團隊。

### 串流思考
<a name="claude-messages-use-extended-thinking-streaming"></a>

您可以使用伺服器傳送事件 (SSE) 串流延伸思考回應。啟用串流以進行延伸思考時，您會透過 `thinking_delta` 事件接收思考內容。串流事件不保證會以固定速率傳回。串流事件之間可能會有延遲。如需透過訊息 API 進行串流的詳細資訊，請參閱[串流訊息](https://docs.anthropic.com/en/docs/build-with-claude/streaming)。

以下是如何使用 **InvokeModelWithResponseStream** 以思考方式處理串流：

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 10000,
    "thinking": {
        "type": "enabled",
        "budget_tokens": 4000
    },
    "messages": [
        {
            "role": "user",
            "content": "What is 27 * 453?"
        }
    ]
}
```

回應：

```
event: message_start
data: {"type": "message_start", "message": {"id": "msg_01...", "type": "message", "role": "assistant", "content": [], "model": "claude-3-7-sonnet-20250219", "stop_reason": null, "stop_sequence": null}}

event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "thinking", "thinking": ""}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "Let me solve this step by step:\n\n1. First break down 27 * 453"}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "\n2. 453 = 400 + 50 + 3"}}

// Additional thinking deltas...

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "EqQBCgIYAhIM1gbcDa9GJwZA2b3hGgxBdjrkzLoky3dl1pkiMOYds..."}}

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

event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "text", "text": ""}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "text_delta", "text": "27 * 453 = 12,231"}}

// Additional text deltas...

event: content_block_stop
data: {"type": "content_block_stop", "index": 1}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": null}}

event: message_stop
data: {"type": "message_stop"}
```

**關於具有思考的串流行為**  
在啟用思考的情況下使用串流時，您可能會注意到文字有時會以較大的區塊傳送，有時則會交替以較小的區塊逐字符傳送。這是預期的行為，特別是用於思考內容。串流系統需要批次處理內容以獲得最佳效能，這可能會導致此交付模式。

## 工具使用的延伸思考
<a name="claude-messages-extended-thinking-tool-use"></a>

延伸的思考可以與 [工具使用](model-parameters-anthropic-claude-messages-tool-use.md) 一起使用，允許 Claude 透過工具選擇和結果處理來進行推理。將延伸思考與工具使用搭配使用時，請注意下列限制：
+ **工具選擇限制**：搭配思考的工具使用僅支援 `tool_choice: any`。其不支援提供特定工具、`auto` 或任何其他值。
+ **保留思考區塊**：工具使用期間，您必須將最後一個助理訊息的思考區塊傳回 API。將完整的未修改區塊回傳至 API，以維持推理持續性。

以下是內容視窗管理如何與工具搭配使用：

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 10000,
    "thinking": {
        "type": "enabled",
        "budget_tokens": 4000
    },
  "tools": [
  {
    "name": "get_weather",
    "description": "Get current weather for a location",
    "input_schema": {
      "type": "object",
      "properties": {
        "location": {
          "type": "string"
        }
      },
      "required": [
        "location"
      ]
    }
  }
],
    "messages": [
        {
            "role": "user",
            "content": "What's the weather in Paris?"
        }
    ]
}
```

第一個回應如下：

```
{
    "content": [
        {
            "type": "thinking",
            "thinking": "The user wants to know the current weather in Paris. I have access to a function `get_weather`...",
            "signature": "BDaL4VrbR2Oj0hO4XpJxT28J5TILnCrrUXoKiiNBZW9P+nr8XSj1zuZzAl4egiCCpQNvfyUuFFJP5CncdYZEQPPmLxYsNrcs...."
        },
        {
            "type": "text",
            "text": "I can help you get the current weather information for Paris. Let me check that for you"
        },
        {
            "type": "tool_use",
            "id": "toolu_01CswdEQBMshySk6Y9DFKrfq",
            "name": "get_weather",
            "input": {
                "location": "Paris"
            }
        }
    ]
}
```

繼續與工具使用的對話會產生另一個回應。請注意，`thinking_block` 和 `tool_use_block` 都會傳入。如果未傳入，則會發生錯誤。

```
{
  "anthropic_version": "bedrock-2023-05-31",
  "max_tokens": 10000,
  "thinking": {
    "type": "enabled",
    "budget_tokens": 4000
  },
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather for a location",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string"
          }
        },
        "required": [
          "location"
        ]
      }
    }
  ],
      "messages": [
        {
          "role": "user",
          "content": "What's the weather in Paris?"
        },
        {
          "role": "assistant",
          "content": [
            {
              "type": "thinking",
              "thinking": "The user wants to know the current weather in Paris. I have access to a function `get_weather`…",
              "signature": "BDaL4VrbR2Oj0hO4XpJxT28J5TILnCrrUXoKiiNBZW9P+nr8XSj1zuZzAl4egiCCpQNvfyUuFFJP5CncdYZEQPPmLxY",
            },
            {
              "type": "tool_use",
              "id": "toolu_01CswdEQBMshySk6Y9DFKrfq",
              "name": "get_weather",
              "input": {
                "location": "Paris"
              }
            }
          ]
        },
        {
          "role": "user",
          "content": [
            {
              "type": "tool_result",
              "tool_use_id": "toolu_01CswdEQBMshySk6Y9DFKrfq",
              "content": "Current temperature: 88°F"
            }
          ]
        }
      ]
    }
```

API 回應現只會包含文字

```
{
  "content": [
    {
      "type": "text",
      "text": "Currently in Paris, the temperature is 88°F (31°C)"
    }
  ]
}
```

### 保留思考區塊
<a name="claude-messages-extended-thinking-tool-use-thinking-blocks"></a>

在工具使用期間，您必須將思考區塊傳回 API，而且您必須將完整的未修改區塊回傳至 API。這對於維護模型的推理流程和對話完整性至關重要。

**提示**  
雖然您可以省略先前 `assistant` 角色輪換中的 `thinking` 區塊，但我們建議一律將所有思考區塊傳回給 API，以進行任何多回合對話。API 會執行下列動作：  
自動篩選提供的思考區塊
使用保留模型推理所需的相關思考區塊
僅針對向 Claude 顯示的區塊輸入的字符進行計費

當 Claude 調用工具時，其會暫停其回應的建構，以等待外部資訊。傳回工具結果時，Claude 將繼續建置該現有回應。這需要在工具使用期間保留思考區塊，原因如下：
+ **推理持續性**：思考區塊會擷取導致工具請求的 Claude 逐步推理。當您發佈工具結果 (包括原始思考) 時，可確保 Claude 可以從停止的位置繼續推理。
+ **內容維護**：雖然工具結果在 API 結構中顯示為使用者訊息，但其是持續推理流程的一部分。保留思考區塊可跨多個 API 呼叫維持此概念流程。

**重要**  
提供思考區塊時，整個連續思考區塊序列必須符合模型在原始請求期間產生的輸出；您無法重新排列或修改這些區塊的序列。

### 交錯思考 (Beta 版)
<a name="claude-messages-extended-thinking-tool-use-interleaved"></a>

**警告**  
交錯思維是以「Beta Service」的形式提供給您，如 AWS 服務條款所定義。它受您與 AWS 和 AWS 服務條款以及適用模型 EULA 的協議約束。

Claude 4 模型支援交錯思考，這項功能可讓 Claude 在工具呼叫之間進行思考，並在接收工具結果後執行更複雜的推理。這允許更複雜的代理互動，其中 Claude 可以執行下列動作：
+ 在決定接下來要做什麼之前，思考工具呼叫的結果
+ 在之間使用推理步驟鏈結多個工具呼叫
+ 根據中繼結果制定更細微的決策

若要啟用交錯思考，請將 Beta 版標頭 `interleaved-thinking-2025-05-14` 新增至 API 請求。

**注意**  
使用交錯思考時，`budget_tokens` 可能超過 `max_tokens` 參數，因為其代表單一助理回合內所有思考區塊的總預算。

## 思維區塊清除 (Beta 版）
<a name="claude-messages-thinking-block-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\$1beta 請求參數上的 Beta 標頭清單。您也需要指定 的使用`clear_thinking_20251015`，並從下列組態選項中選擇。

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


| **組態選項** | **Description** | 
| --- | --- | 
|  `keep` 預設：1 次思考  |  定義要保留的最近助理輪換的思維區塊數量。`{"type": "thinking_turns", "value": N}` 使用 N 必須 > 0 以保持最後 N 圈，或`{"type": "all"}`保留所有思維區塊。  | 

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

```
{
      "anthropic_version": "bedrock-2023-05-31",
      "max_tokens": 10000,
      "anthropic_beta": [
        "context-management-2025-06-27"
      ],
      "thinking": {
        "type": "enabled",
        "budget_tokens": 4000
      },
      "tools": [
        {
          "name": "get_weather",
          "description": "Get current weather for a location",
          "input_schema": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string"
              }
            },
            "required": [
              "location"
            ]
          }
        }
      ],
      "messages": [
        {
          "role": "user",
          "content": "What's the weather in Paris?"
        },
        {
          "role": "assistant",
          "content": [
            {
              "type": "thinking",
              "thinking": "The user is asking for the weather in Paris. I have access to a get_weather function that takes a location as a parameter. I have all the information I need to make this call - the location is \"Paris\".\n\nLet me call the get_weather function with \"Paris\" as the location.",
              "signature": "ErgDCkgIChABGAIqQC/Ccv8GC+5VfcMEiq78XmpU2Ef2cT+96pHKMedKcRNuPz1x0kFlo5HBpW0r1NcQFVQUPuj6PDmP7jdHY7GsrUwSDKNBMogjaM7wYkwfPhoMswjlmfF09JLjZfFlIjB03NkghGOxLbr3VCQHIY0lMaV9UBvt7ZwTpJKzlz+mulBysfvAmDfcnvdJ/6CZre4qnQJsTZaiXdEgASwPIc5jOExBguerrtYSWVC/oPjSi7KZM8PfhP/SPXupyLi8hwYxeqomqkeG7AQhD+3487ecerZJcpJSOSsf0I1OaMpmQEE/b7ehnvTV/A4nLhxIjP4msyIBW+dVwHNFRFlpJLBHUJvN99b4run6YmqBSf4y9TyNMfOr+FtfxedGE0HfJMBd4FHXmUFyW5y91jAHMWqwNxDgacaKkFCAMaqce5rm0ShOxXn1uwDUAS3jeRP26Pynihq8fw5DQwlqOpo7vvXtqb5jjiCmqfOe6un5xeIdhhbzWddhEk1Vmtg7I817pM4MZjVaeQN02drPs8QgDxihnP6ZooGhd6FCBP2X3Ymdlj5zMlbVHxmSkA4wcNtg4IAYAQ=="
            },
            {
              "type": "tool_use",
              "id": "toolu_bdrk_01U7emCvL5v5z5GT7PDr2vzc",
              "name": "get_weather",
              "input": {
                "location": "Paris"
              }
            }
          ]
        },
        {
          "role": "user",
          "content": [
            {
              "type": "tool_result",
              "tool_use_id": "toolu_bdrk_01U7emCvL5v5z5GT7PDr2vzc",
              "content": "Current temperature: 88°F"
            }
          ]
        }
      ],
      "context_management": {
        "edits": [
          {
            "type": "clear_thinking_20251015",
            "keep": {
              "type": "thinking_turns",
              "value": 1
            }
          }
        ]
      }
    }
```

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

```
{
      "model": "claude-haiku-4-5-20251001",
      "id": "msg_bdrk_01KyTbyFbdG2kzPwWMJY1kum",
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "The current weather in Paris is **88°F** (approximately 31°C). It's quite warm! If you need more detailed information like humidity, wind conditions, or a forecast, please let me know."
        }
      ],
      "stop_reason": "end_turn",
      "stop_sequence": null,
      "usage": {
        "input_tokens": 736,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 0,
        "cache_creation": {
          "ephemeral_5m_input_tokens": 0,
          "ephemeral_1h_input_tokens": 0
        },
        "output_tokens": 47
      },
      "context_management": {
        "applied_edits": [...]
      }
    }
```

------

## 具有提示快取的延伸思考
<a name="claude-messages-extended-thinking-prompt-caching"></a>

具有思考的[提示快取](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html)有幾個重要的考量：

**思考區塊內容移除**
+ 先前回合中的思考區塊會從內容中移除，這可能會影響快取中斷點。
+ 繼續與工具使用的對話時，系統會快取思考區塊，並在從快取讀取時計為輸入字符。這會產生權衡，其中思考區塊不會以視覺方式使用內容視窗空間，但在快取時仍會計入您的輸入字符用量。
+ 如果思考功能變成停用狀態，如果您在目前工具使用回合中傳遞思考內容，請求將會失敗。在其他內容中，只會忽略傳遞給 API 的思考內容。

**快取失效模式**
+ 變更思考參數 (例如啟用、停用或修改預算分配) 會使訊息快取中斷點失效。
+ [交錯思考 (Beta 版)](#claude-messages-extended-thinking-tool-use-interleaved) 會增強快取失效，因為多個工具呼叫之間可能會發生思考區塊。
+ 儘管思考參數變更或區塊移除，系統提示和工具仍會保持快取狀態。

**注意**  
雖然移除了用於快取和內容計算的思考區塊，但在繼續與工具使用 (尤其是與交錯思考) 進行對話時必須保留這些區塊。

## 了解思考區塊快取行為
<a name="claude-messages-extended-thinking-caching-behavior"></a>

將延伸思考與工具使用搭配使用時，思考區塊會展現特定快取行為，此行為會影響字符計數。下列序列會示範其運作方式。

1. 只有在您提出內含工具結果的後續請求時，才會發生快取。

1. 提出後續請求時，可以快取先前的對話歷史記錄 (包括思考區塊)。

1. 從快取讀取這些快取的思考區塊時，這些區塊會計入用量指標中的輸入字符。

1. 當包含非工具結果使用者區塊時，會忽略所有先前的思考區塊，並將這些區塊從內容中去掉。

以下是詳細的範例流程：

請求 1：

```
User: "What's the weather in Paris?"
```

回應 1：

```
[thinking_block 1] + [tool_use block 1]
```

請求 2：

```
User: "What's the weather in Paris?",
Assistant: [thinking_block_1] + [tool_use block 1],
User: [tool_result_1, cache=True]
```

回應 2：

```
[thinking_block 2] + [text block 2]
```

請求 2 會寫入請求內容 (而非回應) 的快取。快取包含原始使用者訊息、第一個思考區塊、工具使用區塊，以及工具結果。

請求 3：

```
User: ["What's the weather in Paris?"],
Assistant: [thinking_block_1] + [tool_use block 1],
User: [tool_result_1, cache=True],
Assistant: [thinking_block_2] + [text block 2],
User: [Text response, cache=True]
```

由於包含非工具結果使用者區塊，因此會忽略所有先前的思考區塊。此請求的處理方式與下列請求相同：

請求 3 替代：

```
User: ["What's the weather in Paris?"]
Assistant: [tool_use block 1]
User: [tool_result_1, cache=True]
Assistant: [text block 2]
User: [Text response, cache=True]
```

無論使用一般思考還是交錯思考，此行為都是一致的。

## 具有延伸思考的字符上限和內容視窗大小
<a name="claude-messages-extended-thinking-max-tokens"></a>

在較舊的 Claude 模型 (在 Claude 3.7 Sonnet 之前) 中，如果提示字符和 max\$1tokens 的總和超過模型的內容視窗，系統會自動調整 max\$1tokens 以符合內容限制。這表示您可以設定較大的 max\$1tokens 值，系統會視需要以無提示的方式減少該值。使用 Claude 3.7 和 4 模型時，會以嚴格限制的形式強制執行 `max_tokens` (包括啟用思考時的思考預算)。如果提示字符 \$1 max\$1tokens 超過內容視窗大小，系統現在會傳回驗證錯誤。

### 具有延伸思考的內容視窗
<a name="claude-messages-extended-thinking-max-tokens-calculate"></a>

在計算啟用思考的情況下的內容視窗用量時，需要注意一些考量事項：
+ 先前回合的思考區塊會遭移除，不會計入您的內容視窗。
+ 目前的回合思考會計入該回合 `max_tokens` 的限制。

有效內容視窗的計算方式為：內容視窗 = (目前輸入字符 - 先前的思考字符) \$1 (思考字符 \$1 加密思考字符 \$1 文字輸出字符)。

### 使用延伸思考和工具使用來管理字符
<a name="claude-messages-extended-thinking-max-tokens-manage-tool"></a>

將延伸思考與工具使用搭配使用時，必須明確保留思考區塊，並隨工具結果傳回。使用工具進行延伸思考的有效內容視窗計算會變成以下內容：

`context window = (current input tokens + previous thinking tokens + tool use tokens) + (thinking tokens + encrypted thinking tokens + text output tokens)`

### 以延伸思考管理字符
<a name="claude-messages-extended-thinking-max-tokens-manage"></a>

鑑於內容視窗和具有延伸思考 Claude 3.7 和 4 模型的 `max_tokens` 行為，您可能需要執行下列其中一個動作：
+ 更積極地監控和管理字符用量。
+ 在提示長度變更時調整 `max_tokens` 值。
+ 請注意，先前的思考區塊不會累積在內容視窗中。此變更旨在提供更可預測且透明的行為，特別是當字符上限大幅增加時。

## 延伸思考字符成本考量
<a name="claude-messages-extended-thinking-cost"></a>

思考程序會產生下列費用：
+ 思考期間使用的字符 (輸出字符)
+ 在後續請求中包含的最後一個助理回合中的思考區塊 (輸入字符)
+ 標準文字輸出字符

**提示**  
啟用延伸思考時，會自動包含專用的 28 或 29 字符系統提示，以支援此功能。

`budget_tokens` 參數會決定 Claude 允許用於其內部推理程序的字符數量上限。較大的預算可以透過對複雜問題進行更徹底的分析來改善回應品質，但 Claude 可能不會使用分配的整個預算，尤其是在 32K 以上的範圍。

使用交錯思考時，`budget_tokens` 可能超過 `max_tokens` 參數，因為其代表單一助理回合內所有思考區塊的總預算。

使用摘要思考時，請記住下列資訊：
+ **輸入字符**：原始請求中的字符
+ **輸出字符 (已計費)**：內部產生 Claude 的原始思考字符
+ **輸出字符 (可見)**：您在回應中看到的摘要思考字符
+ **免費**：用於產生摘要的字符
+ `summary_status` 欄位可以指出字符限制是否影響摘要
+ 計費的輸出字符計數與回應中可見的字符計數不相符。您需要為完整思考程序 (而不是您看到的摘要) 付費。

# 適應性思維
<a name="claude-messages-adaptive-thinking"></a>

調適性思維是[延伸思考](claude-messages-extended-thinking.md)搭配 4.6 Claude Opus 使用的建議方法。調整式思維不會手動設定思維字符預算，而是讓 根據每個請求的複雜性Claude動態決定思考的時間和程度。自適應思維使用固定的 可靠地推動比延伸思維更好的效能`budget_tokens`，我們建議您改用自適應思維，從 4.6 Claude Opus 取得最智慧的回應。不需要 Beta 標頭。

支援的模型如下：


| 模型 | 模型 ID | 
| --- | --- | 
| Claude Opus 4.6 | `anthropic.claude-opus-4-6-v1` | 
| Claude Sonnet 4.6 | `anthropic.claude-sonnet-4-6` | 

**注意**  
`thinking.type: "enabled"` 和 `budget_tokens` 已在 Claude Opus 4.6 上棄用，並將在未來的模型版本中移除。請改為使用 `thinking.type: "adaptive"`搭配 工作量參數。  
較舊的模型 Claude Opus (Claude Sonnet 4.5、4.5 等） 不支援適應性思維，需要使用 `thinking.type: "enabled"` `budget_tokens`。

## 適應性思維的運作方式
<a name="claude-messages-adaptive-thinking-how-it-works"></a>

在自適應模式中， 會Claude評估每個請求的複雜性，並決定是否要考慮和考慮多少。在預設的工作量層級 (`high`) 中， 幾乎一律Claude會考慮。在較低的工作量層級， Claude 可能會略過對更簡單問題的思考。

自適應思維也會自動啟用 [交錯思考 (Beta 版)](claude-messages-extended-thinking.md#claude-messages-extended-thinking-tool-use-interleaved)。這表示Claude可以在工具呼叫之間進行思考，使其對客服人員工作流程特別有效。

在您的 API 請求`"adaptive"`中`thinking.type`將 設定為 ：

------
#### [ CLI ]

```
aws bedrock-runtime invoke-model \
--model-id "us.anthropic.claude-opus-4-6-v1" \
--body '{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 16000,
"thinking": {
"type": "adaptive"
},
"messages": [
{
"role": "user",
"content": "Three players A, B, C play a game. Each has a jar with 100 balls numbered 1-100. Simultaneously, each draws one ball. A beats B if As number > Bs number (mod 100, treating 100 as 0 for comparison). Similarly for B vs C and C vs A. The overall winner is determined by majority of pairwise wins (ties broken randomly). Is there a mixed strategy Nash equilibrium where each player draws uniformly? If not, characterize the equilibrium."
}
]
}' \
--cli-binary-format raw-in-base64-out \
output.json && cat output.json | jq '.content[] | {type, thinking: .thinking[0:200], text}'
```

------
#### [ Python ]

```
import boto3
import json

bedrock_runtime = boto3.client(
    service_name='bedrock-runtime',
    region_name='us-east-2'
)

response = bedrock_runtime.invoke_model(
    modelId="us.anthropic.claude-opus-4-6-v1",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 16000,
        "thinking": {
            "type": "adaptive"
        },
        "messages": [{
            "role": "user",
            "content": "Explain why the sum of two even numbers is always even."
        }]
    })
)

response_body = json.loads(response["body"].read())

for block in response_body["content"]:
    if block["type"] == "thinking":
        print(f"\nThinking: {block['thinking']}")
    elif block["type"] == "text":
        print(f"\nResponse: {block['text']}")
```

------
#### [ TypeScript ]

```
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";

async function main() {
    const client = new BedrockRuntimeClient({});

    const command = new InvokeModelCommand({
        modelId: "us.anthropic.claude-opus-4-6-v1",
        body: JSON.stringify({
            anthropic_version: "bedrock-2023-05-31",
            max_tokens: 16000,
            thinking: {
                type: "adaptive"
            },
            messages: [{
                role: "user",
                content: "Explain why the sum of two even numbers is always even."
            }]
        })
    });

    const response = await client.send(command);
    const responseBody = JSON.parse(new TextDecoder().decode(response.body));

    for (const block of responseBody.content) {
        if (block.type === "thinking") {
            console.log(`\nThinking: ${block.thinking}`);
        } else if (block.type === "text") {
            console.log(`\nResponse: ${block.text}`);
        }
    }
}

main().catch(console.error);
```

------

## 使用工作量參數的適應性思維
<a name="claude-messages-adaptive-thinking-effort"></a>

您可以將適應性思維與工作量參數結合，以引導思考Claude的進行程度。工作量層級可做為 思維配置Claude的軟性指引：


| 投入程度 | 思考行為 | 
| --- | --- | 
| max | Claude 一律在思考深度方面沒有限制的情況下思考。 Claude Opus 僅限 4.6 — 在其他模型max上使用 的請求將傳回錯誤。 | 
| high (default) | Claude 一律會思考。提供複雜任務的深入推理。 | 
| medium | Claude 使用中度思考。可能會略過思考非常簡單的查詢。 | 
| low | Claude 將思維降至最低。略過思考速度最重要的簡單任務。 | 

## 提示快取
<a name="claude-messages-adaptive-thinking-prompt-caching"></a>

使用`adaptive`思維的連續請求會保留提示快取中斷點。不過，在 `adaptive`和 `enabled`/`disabled` 思維模式之間切換會中斷訊息的快取中斷點。無論模式變更為何，系統提示和工具定義都會保持快取。

## 調校思考行為
<a name="claude-messages-adaptive-thinking-tuning"></a>

如果 Claude 的思考頻率高於或低於您想要的頻率，您可以在系統提示中新增指引：

```
Extended thinking adds latency and should only be used when it
will meaningfully improve answer quality — typically for problems
that require multi-step reasoning. When in doubt, respond directly.
```

**警告**  
不太常Claude思考可能會降低受惠於推理的任務品質。在將提示型調校部署至生產環境之前，測量對特定工作負載的影響。請考慮先以較低的工作量層級進行測試。

# 思考加密
<a name="claude-messages-thinking-encryption"></a>

完整思考內容會經過加密，並在簽章欄位中傳回。此欄位用於驗證當傳回給 API 時，Claude 產生的思考區塊。串流回應時，會透過正好在 `content_block_stop` 事件前的 `content_block_delta` 事件內的 `signature_delta` 來新增簽章。

**注意**  
只有在使用具有延伸思考的工具時，才嚴格要求傳送回思考區塊。否則，您可以省略先前回合的思考區塊，或在將資料傳回時讓 API 為您去掉這些區塊。  
如果回傳思考區塊，我們建議完整回傳您收到的所有內容，以確保一致性並避免潛在問題。

## Claude 3.7 Sonnet 中的思考修訂
<a name="claude-messages-thinking-encryption-redaction"></a>

**注意**  
以下資訊特別適用於 Claude 3.7 Sonnet。Claude 4 模型以不同的方式處理思考，且不會產生修訂後的思考區塊。

在 Claude 3.7 Sonnet 中，適用下列條件：
+ 安全系統偶爾會標記 Claude 的內部推理。發生這種情況時，我們會加密部分或全部的思考區塊，並將其作為 redacted\$1thinking 區塊傳回給您。將 redacted\$1thinking 區塊傳回至 API 時，會解密這些區塊，讓 Claude 能夠繼續回應而不會遺失內容。
+ 會在回應中的文字區塊之前傳回 `thinking` 和 `redacted_thinking` 區塊。

建置使用延伸思考搭配 Claude 3.7 Sonnet 的客戶面向應用程式時，請考量下列事項：
+ 請注意，修訂後的思考區塊包含人類無法閱讀的加密內容。
+ 考慮提供簡單的說明，例如：「基於安全考量，部分 Claude 的內部推理已自動加密。這不會影響回應的品質。」
+ 如果您向使用者顯示思考區塊，則可以篩選掉已修訂的區塊，同時保留正常的思考區塊。
+ 請注意，使用延伸思考功能有時可能會導致某些推理遭到加密。
+ 實作適當的錯誤處理，以正常管理修訂後的思考，而不中斷 UI。

以下範例顯示正常和修訂後的思考區塊：

```
{
    "content": [
        {
            "type": "thinking",
            "thinking": "Let me analyze this step by step...",
            "signature":"WaUjzkypQ2mUEVM36O2TxuC06KN8xyfbJwyem2dw3URve/op91XWHOEBLLqIOMfFG/UvLEczmEsUjavL...."                },
        {
            "type": "redacted_thinking",
            "data":"EmwKAhgBEgy3va3pzix/LafPsn4aDFIT2Xlxh0L5L8rLVyIwxtE3rAFBa8cr3qpP..."
        },
        {
            "type": "text",
            "text": "Based on my analysis..."
        }
    ]
}
```

**提示**  
在輸出中查看修訂後的思考區塊是預期的行為。模型仍然可以使用此修訂後的推理來通知其回應，同時維護安全防護機制。  
如果需要在應用程式中測試修訂後的思考處理，您可以使用此特殊測試字串作為提示：`ANTHROPIC_MAGIC_STRING_TRIGGER_REDACTED_THINKING_46C9A13E193C177646C7398A98432ECCCE4C1253D5E2D82641AC0E52CC2876CB`

在多回合對話中將 `thinking` 和 `redacted_thinking` 區塊傳回 API 時，您必須將完整的未修改區塊傳回 API 以進行最後的助理回合。這對維護模型的推理流程至關重要。我們建議一律將所有思考區塊傳回給 API。如需詳細資訊，請參閱「[工具使用的延伸思考保留思考區塊](claude-messages-extended-thinking.md#claude-messages-extended-thinking-tool-use-thinking-blocks)」。

下列範例使用 **InvokeModelWithResponseStream** API，在搭配修訂使用思考字符時示範請求和回應結構。

啟用串流時，您將收到來自 thinking\$1delta 事件的思考內容。以下是如何使用思考處理串流：

**請求**

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 24000,
    "thinking": {
        "type": "enabled",
        "budget_tokens": 16000
    },
    "messages": [
        {
            "role": "user",
            "content": "What is 27 * 453?"
        }
    ]
}
```

**回應**

```
event: message_start
data: {"type": "message_start", "message": {"id": "msg_01...", "type": "message", "role": "assistant", "content": [], "model": "claude-3-7-sonnet-20250219", "stop_reason": null, "stop_sequence": null}}

event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "thinking", "thinking": ""}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "Let me solve this step by step:\n\n1. First break down 27 * 453"}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "\n2. 453 = 400 + 50 + 3"}}

// Additional thinking deltas...

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "EqQBCgIYAhIM1gbcDa9GJwZA2b3hGgxBdjrkzLoky3dl1pkiMOYds..."}}

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

event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "text", "text": ""}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "text_delta", "text": "27 * 453 = 12,231"}}

// Additional text deltas...

event: content_block_stop
data: {"type": "content_block_stop", "index": 1}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": null}}

event: message_stop
data: {"type": "message_stop"}
```

# 跨模型版本思考的差異
<a name="claude-messages-thinking-differences"></a>

Messages API 處理跨 Claude 3.7 Sonnet 和 Claude 4 個模型的不同思考，主要是在修訂和摘要行為。下表摘要說明那些差異。


| 功能 | Claude 3.7 Sonnet | Claude 4 模型 | 
| --- | --- | --- | 
| 思考輸出 | 傳回完整的思考輸出 | 傳回摘要思考 | 
| 修訂處理 | 使用 `redacted_thinking` 區塊 | 修訂和加密在 `signature` 欄位中傳回的完整思考 | 
| 交錯思考 | 不支援 | 支援 Beta 版標頭 | 

# 壓縮
<a name="claude-messages-compaction"></a>

**提示**  
建議將伺服器端壓縮用於管理長時間執行的對話和代理工作流程中的內容，因為它會自動處理內容管理，並將整合工作降至最低。

**注意**  
壓縮目前為 Beta 版。在 API 請求`compact-2026-01-12`中包含 Beta 標頭以使用此功能。Converse API 目前不支援壓縮，但 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 支援壓縮。

壓縮透過在接近內容視窗限制時自動摘要較舊的內容，來擴展長時間執行的對話和任務的有效內容長度。這非常適合：
+ 以聊天為基礎的多轉對話，您希望使用者長時間使用一個聊天
+ 任務導向的提示，需要許多可能超過 200K個內容視窗的後續工作 （通常使用工具）

下列模型支援壓縮：


| 模型 | 模型 ID | 
| --- | --- | 
| Claude Sonnet 4.6 | `anthropic.claude-sonnet-4-6` | 
| Claude Opus 4.6 | `anthropic.claude-opus-4-6-v1` | 

**注意**  
`usage` 最上層 `input_tokens`和 `output_tokens` 欄位中不包含壓縮反覆運算用量，並反映所有非壓縮反覆運算的總和。若要計算所使用和收取的請求權杖總數，請加總`usage.iterations`陣列中的所有項目。  
如果您之前依賴 `usage.input_tokens`和 `usage.output_tokens`進行成本追蹤或稽核，則需要更新追蹤邏輯，以在啟用壓縮`usage.iterations`時彙總 。只有在請求期間觸發新的壓縮時，`iterations`陣列才會存在。重新套用先前的`compaction`區塊不會產生額外的壓縮成本，在這種情況下，最上層的使用欄位會保持準確。

## 壓縮的運作方式
<a name="claude-messages-compaction-how-it-works"></a>

啟用壓縮時， 會在接近設定的字符閾值時Claude自動總結您的對話。API：

1. 偵測輸入字符何時超過您指定的觸發閾值。

1. 產生目前對話的摘要。

1. 建立包含摘要的`compaction`區塊。

1. 使用壓縮的內容繼續回應。

在後續請求中，將回應附加至您的訊息。API 會在`compaction`封鎖之前自動捨棄所有訊息區塊，繼續摘要中的對話。

## 基本使用
<a name="claude-messages-compaction-basic-usage"></a>

在訊息 API 請求`context_management.edits`中將`compact_20260112`策略新增至 ，以啟用壓縮。

------
#### [ CLI ]

```
aws bedrock-runtime invoke-model \
    --model-id "us.anthropic.claude-opus-4-6-v1" \
    --body '{
        "anthropic_version": "bedrock-2023-05-31",
        "anthropic_beta": ["compact-2026-01-12"],
        "max_tokens": 4096,
        "messages": [
            {
                "role": "user",
                "content": "Help me build a website"
            }
        ],
        "context_management": {
            "edits": [
                {
                    "type": "compact_20260112"
                }
            ]
        }
    }' \
    --cli-binary-format raw-in-base64-out \
    /tmp/response.json

echo "Response:"
cat /tmp/response.json | jq '.content[] | {type, text: .text[0:500]}'
```

------
#### [ Python ]

```
import boto3
import json

bedrock_runtime = boto3.client(service_name='bedrock-runtime')

messages = [{"role": "user", "content": "Help me build a website"}]

response = bedrock_runtime.invoke_model(
    modelId="us.anthropic.claude-opus-4-6-v1",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "anthropic_beta": ["compact-2026-01-12"],
        "max_tokens": 4096,
        "messages": messages,
        "context_management": {
            "edits": [
                {
                    "type": "compact_20260112"
                }
            ]
        }
    })
)

response_body = json.loads(response["body"].read())

# Append the response (including any compaction block) to continue the conversation
messages.append({"role": "assistant", "content": response_body["content"]})

for block in response_body["content"]:
    if block.get("type") == "compaction":
        print(f"[COMPACTION]: {block['content'][:200]}...")
    elif block.get("type") == "text":
        print(f"[RESPONSE]: {block['text']}")
```

------
#### [ TypeScript ]

```
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";

async function main() {
    const client = new BedrockRuntimeClient({});

    const messages: Array<{role: string, content: string | object[]}> = [
        { role: "user", content: "Help me build a website" }
    ];

    const command = new InvokeModelCommand({
        modelId: "us.anthropic.claude-opus-4-6-v1",
        body: JSON.stringify({
            anthropic_version: "bedrock-2023-05-31",
            anthropic_beta: ["compact-2026-01-12"],
            max_tokens: 4096,
            messages,
            context_management: {
                edits: [
                    {
                        type: "compact_20260112"
                    }
                ]
            }
        })
    });

    const response = await client.send(command);
    const responseBody = JSON.parse(new TextDecoder().decode(response.body));

    // Append response to continue conversation
    messages.push({ role: "assistant", content: responseBody.content });

    for (const block of responseBody.content) {
        if (block.type === "compaction") {
            console.log(`[COMPACTION]: ${block.content.substring(0, 200)}...`);
        } else if (block.type === "text") {
            console.log(`[RESPONSE]: ${block.text}`);
        }
    }
}

main().catch(console.error);
```

------

## Parameters
<a name="claude-messages-compaction-parameters"></a>


| 參數 | Type | 預設 | Description | 
| --- | --- | --- | --- | 
| type | string | 必要 | 必須為 "compact\$120260112" | 
| trigger | object | 150，000 個字符 | 何時觸發壓縮。必須至少為 50，000 個字符。 | 
| pause\$1after\$1compaction | boolean | false | 是否在產生壓縮摘要後暫停 | 
| instructions | string | null | 自訂摘要提示。提供時完全取代預設提示。 | 

## 觸發組態
<a name="claude-messages-compaction-trigger"></a>

設定 何時使用 `trigger` 參數觸發壓縮：

```
import boto3
import json

bedrock_runtime = boto3.client(service_name='bedrock-runtime')

response = bedrock_runtime.invoke_model(
    modelId="us.anthropic.claude-opus-4-6-v1",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "anthropic_beta": ["compact-2026-01-12"],
        "max_tokens": 4096,
        "messages": [{"role": "user", "content": "Help me build a website"}],
        "context_management": {
            "edits": [
                {
                    "type": "compact_20260112",
                    "trigger": {
                        "type": "input_tokens",
                        "value": 100000
                    }
                }
            ]
        }
    })
)

response_body = json.loads(response["body"].read())
print(response_body["content"][-1]["text"])
```

## 自訂摘要說明
<a name="claude-messages-compaction-custom-instructions"></a>

根據預設，壓縮會使用下列摘要提示：

```
You have written a partial transcript for the initial task above. Please write a summary of the transcript. The purpose of this summary is to provide continuity so you can continue to make progress towards solving the task in a future context, where the raw history above may not be accessible and will be replaced with this summary. Write down anything that would be helpful, including the state, next steps, learnings etc. You must wrap your summary in a <summary></summary> block.
```

您可以透過 `instructions` 參數提供自訂指示，以完全取代此提示。自訂指示不會補充預設值；它們會完全取代預設值：

```
import boto3
import json

bedrock_runtime = boto3.client(service_name='bedrock-runtime')

response = bedrock_runtime.invoke_model(
    modelId="us.anthropic.claude-opus-4-6-v1",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "anthropic_beta": ["compact-2026-01-12"],
        "max_tokens": 4096,
        "messages": [{"role": "user", "content": "Help me build a website"}],
        "context_management": {
            "edits": [
                {
                    "type": "compact_20260112",
                    "instructions": "Focus on preserving code snippets, variable names, and technical decisions."
                }
            ]
        }
    })
)

response_body = json.loads(response["body"].read())
print(response_body["content"][-1]["text"])
```

## 壓縮後暫停
<a name="claude-messages-compaction-pause"></a>

`pause_after_compaction` 使用 在產生壓縮摘要後暫停 API。這可讓您在 API 繼續回應之前新增其他內容區塊 （例如保留最近的訊息或特定的指令導向訊息）。

啟用時，API 會在產生壓縮區塊後傳回具有`compaction`停止原因的訊息：

```
import boto3
import json

bedrock_runtime = boto3.client(service_name='bedrock-runtime')

messages = [{"role": "user", "content": "Help me build a website"}]

response = bedrock_runtime.invoke_model(
    modelId="us.anthropic.claude-opus-4-6-v1",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "anthropic_beta": ["compact-2026-01-12"],
        "max_tokens": 4096,
        "messages": messages,
        "context_management": {
            "edits": [
                {
                    "type": "compact_20260112",
                    "pause_after_compaction": True
                }
            ]
        }
    })
)

response_body = json.loads(response["body"].read())

# Check if compaction triggered a pause
if response_body.get("stop_reason") == "compaction":
    # Response contains only the compaction block
    messages.append({"role": "assistant", "content": response_body["content"]})

    # Continue the request
    response = bedrock_runtime.invoke_model(
        modelId="us.anthropic.claude-opus-4-6-v1",
        body=json.dumps({
            "anthropic_version": "bedrock-2023-05-31",
            "anthropic_beta": ["compact-2026-01-12"],
            "max_tokens": 4096,
            "messages": messages,
            "context_management": {
                "edits": [{"type": "compact_20260112"}]
            }
        })
    )
    response_body = json.loads(response["body"].read())

print(response_body["content"][-1]["text"])
```

## 使用壓縮區塊
<a name="claude-messages-compaction-blocks"></a>

觸發壓縮時，API 會在助理回應開始時傳回`compaction`區塊。

長時間執行的對話可能會導致多次壓縮。最後一個壓縮區塊會反映提示的最終狀態，在提示之前將內容取代為產生的摘要。

```
{
  "content": [
    {
      "type": "compaction",
      "content": "Summary of the conversation: The user requested help building a web scraper..."
    },
    {
      "type": "text",
      "text": "Based on our conversation so far..."
    }
  ]
}
```

## 串流
<a name="claude-messages-compaction-streaming"></a>

在啟用壓縮的情況下串流回應時，您將在壓縮開始時收到`content_block_start`事件。壓縮區塊串流與文字區塊不同。您將會收到`content_block_start`事件，後面接著一個事件`content_block_delta`，其中包含完整的摘要內容 （無中繼串流），然後是`content_block_stop`事件。

## 提示快取
<a name="claude-messages-compaction-prompt-caching"></a>

您可以在壓縮區塊上新增`cache_control`中斷點，這會快取完整的系統提示以及摘要的內容。會忽略原始壓縮內容。請注意，觸發壓縮時，可能會導致後續請求的快取遺失。

```
{
    "role": "assistant",
    "content": [
        {
            "type": "compaction",
            "content": "[summary text]",
            "cache_control": {"type": "ephemeral"}
        },
        {
            "type": "text",
            "text": "Based on our conversation..."
        }
    ]
}
```

## 了解用量
<a name="claude-messages-compaction-usage"></a>

壓縮需要額外的取樣步驟，這有助於速率限制和計費。API 會在回應中傳回詳細的用量資訊：

```
{
  "usage": {
    "input_tokens": 45000,
    "output_tokens": 1234,
    "iterations": [
      {
        "type": "compaction",
        "input_tokens": 180000,
        "output_tokens": 3500
      },
      {
        "type": "message",
        "input_tokens": 23000,
        "output_tokens": 1000
      }
    ]
  }
}
```

`iterations` 陣列會顯示每次抽樣反覆運算的用量。壓縮發生時，您會看到`compaction`反覆運算，後面接著主要`message`反覆運算。最終反覆運算的權杖計數反映壓縮後的有效內容大小。

# 從模型取得經過驗證的 JSON 結果
<a name="claude-messages-structured-outputs"></a>

您可以搭配 Claude Sonnet 4.5、Claude Haiku 4.5、 Claude Opus 4.5和 使用結構化輸出Claude Opus 4.6。如需進一步了解，請參閱[從模型取得經過驗證的 JSON 結果](structured-output.md)。

# 請求與回應
<a name="model-parameters-anthropic-claude-messages-request-response"></a>

請求本文在請求 `body` 欄位中傳遞到 [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)。

**注意**  
限制適用於下列操作：`InvokeModel`、`Converse`、 `InvokeModelWithResponseStream`和 `ConverseStream`。如需詳細資訊，請參閱 [API 限制](inference-api-restrictions.md)。

**警告**  
Claude Sonnet 4.5 和 Claude Haiku 4.5支援指定 `temperature`或 `top_p` 參數，但不能同時指定兩者。這不適用於任何較舊的模型。

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

Anthropic Claude 具有下列訊息推論呼叫的推論參數。

```
{
    "anthropic_version": "bedrock-2023-05-31", 
    "anthropic_beta": ["computer-use-2024-10-22"] 
    "max_tokens": int,
    "system": string,    
    "messages": [
        {
            "role": string,
            "content": [
                { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": "content image bytes" } },
                { "type": "text", "text": "content text" }
      ]
        }
    ],
    "temperature": float,
    "top_p": float,
    "top_k": int,
    "tools": [
        {
                "type": "custom",
                "name": string,
                "description": string,
                "input_schema": json
            
        },
        { 
            "type": "computer_20241022",  
            "name": "computer", 
            "display_height_px": int,
            "display_width_px": int,
            "display_number": 0 int
        },
        { 
            "type": "bash_20241022", 
            "name": "bash"
        },
        { 
            "type": "text_editor_20241022",
            "name": "str_replace_editor"
        }
        
    ],
    "tool_choice": {
        "type" :  string,
        "name" : string,
    },
    

 
    "stop_sequences": [string]
}
```

下列是必要的參數。
+  **anthropic\$1version** – (必要) Anthropic 版本。值必須為 `bedrock-2023-05-31`。
+ **max\$1tokens** – (必要) 停止之前要產生的字符數目上限。

  請注意，Anthropic Claude 模型可能會在達到 `max_tokens` 的值之前停止產生字符。不同的 Anthropic Claude 模型對此參數具有不同的最大值。如需詳細資訊，請參閱[模型比較](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)。
+ **訊息** – (必要) 輸入訊息。
  + **role** – 對話回合的角色。有效值為 `user` 和 `assistant`。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + **content** – (必要) 對話回合的內容，作為物件陣列。每個物件都包含一個**類型**欄位，您可以在其中指定下列其中一個值：
    + `text` – 如果您指定此類型，則必須包含**文字**欄位，並指定文字提示作為其值。如果陣列中的另一個物件是影像，則此文字提示會套用至影像。
    + `image` – 如果您指定此類型，則必須包含對應至具有下列欄位之物件的**來源**欄位：
      + **type** – (必要) 影像的編碼類型。您可以指定 `base64`。
      + **media\$1type** – (必要) 影像的類型。您可以指定下列影像格式。
        + `image/jpeg`
        + `image/png`
        + `image/webp` 
        + `image/gif`
      + **data** – (必要) 影像的 Base64 編碼影像位元組。影像大小上限為 3.75 MB。影像的高度和寬度上線為 8000 像素。

以下是選用參數。
+  **system** – (選用) 請求的系統提示。

  系統提示是提供內容和指示給 Anthropic Claude 的一種方式，例如指定特定目標或角色。如需詳細資訊，請參閱 Anthropic 文件中的[系統提示](https://docs.anthropic.com/en/docs/system-prompts)。
**注意**  
您可以使用系統提示搭配 Anthropic Claude 2.1 版或更新版本。
+ **anthropic\$1beta** – (選用) anthropic beta 參數是 Beta 版標頭的字串清單，用於表示選擇加入特定的 Beta 版功能。
**注意**  
100 萬個字符內容長度變體Claude Sonnet 4可在特定 AWS 區域中做為「Beta Service」使用，如 AWS 服務條款所定義。它受您與 AWS 和 AWS 服務條款以及適用模型 EULA 的協議約束。如需較長內容請求定價的詳細資訊，請參閱 [Amazon Bedrock 定價](https://aws.amazon.com/bedrock/pricing/)頁面。適用個別的服務配額 （如需詳細資訊，請參閱 中的**Service Quotas** AWS 管理主控台)。

  可用的 Beta 版標頭包括下列項目：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **stop\$1sequences** – (選用) 導致模型停止產生的自訂文字序列。Anthropic Claude 模型通常會在自然完成輪換時停止，在此情況下，`stop_reason` 回應欄位的值為 `end_turn`。如果您希望模型在遇到自訂文字字串時停止產生，您可以使用 `stop_sequences` 參數。如果模型遇到其中一個自訂文字字串，`stop_reason` 回應欄位的值為 `stop_sequence`，而 `stop_sequence` 的值包含相符的停止序列。

  項目的數目上限為 8191。
+  **temperature** – (選用) 注入回應的隨機性量。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\$1p** – (選用) 使用核心取樣。

  在核心取樣中，Anthropic Claude 會以遞減的機率順序計算每個後續字符所有選項的累積分佈，並在達到 `top_p` 指定的特定機率時將其切斷。調整取樣參數時，請修改 `temperature` 或 `top_p`。請勿同時修改兩者。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\$1k** – (選用) 僅針對每個後續字符杖從前 K 個選項中取樣。

  使用 `top_k` 移除長尾低機率回應。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **tools** – (選用) 模型可能使用之工具的定義。
**注意**  
需要 Anthropic Claude 3 模型。

  如果您在請求中包含 `tools`，模型可能會傳回內容 `tool_use` 區塊，代表模型使用這些工具。然後，您可以使用模型產生的工具輸入來執行這些工具，然後使用 `tool_result` 內容區塊選擇性地將結果傳回至模型。

  您可以傳遞下列工具類型：

**Custom**  
自訂工具的定義。
  + (選用) **type** – 工具的類型。如果已定義，請使用值 `custom`。
  + **name** – 工具的名稱。
  + **description** – (選用，但強烈建議) 工具的描述。
  + **input\$1schema** – 工具的 JSON 結構描述。

**Computer**  
您搭配電腦使用之電腦工具的定義會使用 API。
  +  **type** – 值必須為 `computer_20241022`。
  + **name** – 值必須為 `computer`。
  + (必要) **display\$1height\$1px** – 由模型控制的顯示高度，以像素為單位。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + (必要) **display\$1width\$1px** – 模型控制之顯示器的寬度，以像素為單位。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + (選用) **display\$1number** – 要控制的顯示號碼 (僅與 X11 環境相關)。如果指定，工具會在工具定義中提供顯示號碼。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)

**bash**  
您搭配電腦使用的 bash 工具定義會使用 API。
  + (選用) **類型** – 值必須為 `bash_20241022`。
  + **name** – 值必須為 `bash`。工具。

**文字編輯器**  
您搭配電腦使用的文字編輯器工具定義會使用 API。
  + (選用) **類型** – 值必須為 `text_editor_20241022`。
  + **name** – 值必須為 `str_replace_editor`。工具。
+  **tool\$1choice** – (選用) 指定模型應該如何使用提供的工具。模型可以使用特定工具、任何可用的工具，或自行決定。
**注意**  
需要 Anthropic Claude 3 模型。
  + **type** – 工具選擇類型。可能的值為 `any` (使用任何可用的工具)、`auto` (模型決定) 和 `tool` (使用指定的工具)。
  + **name** – (選用) 要使用的工具名稱。如果您在 `type` 欄位中指定 `tool`，則為必要。

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

Anthropic Claude 模型會傳回訊息推論呼叫的下列欄位。

```
{
    "id": string,
    "model": string,
    "type" : "message",
    "role" : "assistant",
    "content": [
        {
            "type": string,
            "text": string,
            "image" :json,
            "id": string,
            "name":string,
            "input": json
        }
    ],
    "stop_reason": string,
    "stop_sequence": string,
    "usage": {
        "input_tokens": integer,
        "output_tokens": integer
    }
    
}
```

具有新 stop\$1reason 值的範例回應：

```
// Example with refusal
{
    "stop_reason": "refusal",
    "content": [
        {
            "type": "text",
            "text": "I can't help with that request."
        }
    ]
}

// Example with tool_use
{
    "stop_reason": "tool_use",
    "content": [
        {
            "type": "tool_use",
            "id": "toolu_123",
            "name": "calculator",
            "input": {"expression": "2+2"}
        }
    ]
}

// Example with model_context_window_exceeded (Claude Sonnet 4.5)
{
    "stop_reason": "model_context_window_exceeded",
    "content": [
        {
            "type": "text",
            "text": "The response was truncated due to context window limits..."
        }
    ]
}
```
+ **id** — 回應的唯一識別符。ID 的格式和長度可能會隨著時間而變更。
+ **model** – 提出請求之 Anthropic Claude 模型的 ID。
+ **stop\$1reason** – Anthropic Claude 停止產生回應的原因。
  + **end\$1turn** – 模型達到自然停止點
  + **max\$1tokens** – 產生的文字超過 `max_tokens` 輸入欄位的值，或超過模型支援的字符數目上限。
  + **stop\$1sequence** – 模型會產生您在 `stop_sequences` 輸入欄位中指定的其中一個停止序列。
  + **拒絕** – 由於安全問題，Claude 拒絕產生回應
  + **tool\$1use** – Claude 正在呼叫工具，並預期您會執行它
  + **model\$1context\$1window\$1exceeded** – 模型因達到內容視窗限制而停止產生。
    + Claude Sonnet 4.5 的新功能
+ **stop\$1sequence** – 結束生成的停止序列。
+ **type** – 回應的類型。值一律為 `message`。
+ **role** – 產生訊息的對話角色。值一律為 `assistant`。
+ **內容** – 模型產生的內容。傳回為陣列。內容有三種類型：*text*、*tool\$1use* 和 *image*。
  + *text* – 文字回應。
    + **type** – 內容的類型。此值為 `text`。
    + **text** – 如果 `type` 的值是文字，則包含內容的文字。
  + *tool use* – 來自模型使用工具的請求。
    + **type** – 內容的類型。此值為 `tool_use`。
    + **id** – 模型請求使用的工具 ID。
    + **name** – 包含所請求工具的名稱。
    + **input** – 要傳遞至工具的輸入參數。
  + *image* – 來自模型使用工具的請求。
    + **type** – 內容的類型。此值為 `image`。
    + **source** – 包含影像。如需詳細資訊，請參閱[多模態提示](model-parameters-anthropic-claude-messages.md#model-parameters-anthropic-claude-messages-multimodal-prompts)。
+ **usage** – 您在請求中所提供字符數目的容器，以及模型在回應中產生的字符數目。
  + **input\$1tokens** – 請求中的輸入字符數目。
  + **output\$1tokens** – 模型在回應中產生的字符數目。
  + **stop\$1sequence** – 模型會產生您在 `stop_sequences` 輸入欄位中指定的其中一個停止序列。

------

## Effort 參數 (Beta 版）
<a name="model-parameters-anthropic-claude-effort-parameter"></a>

`effort` 參數是考慮 4.5 Claude Opus 字符預算的替代方案。此參數會自由地告知應Claude如何花費字符來產生最佳結果，並在思考、工具呼叫和使用者通訊之間調整字符用量。它可以搭配或不搭配延伸思考模式使用。

工作量參數可設定為：
+ `high` （預設） – 視需要Claude花費任意數量的權杖，以獲得最佳結果
+ `medium` – 平衡字符用量
+ `low` – 保守字符用量

若要使用此功能，您必須傳遞 Beta 版標頭 `effort-2025-11-24`。

請求範例：

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "anthropic_beta": [
        "effort-2025-11-24"
    ],
    "max_tokens": 4096,
    "output_config": {
        "effort": "medium"
    },
    "messages": [{
        "role": "user",
        "content": "Analyze this complex dataset and provide insights"
    }]
}
```

# 程式碼範例
<a name="api-inference-examples-claude-messages-code-examples"></a>

下列程式碼範例示範如何使用訊息 API。

**Topics**
+ [訊息程式碼範例](#api-inference-examples-claude-messages-code-example)
+ [多模態程式碼範例](#api-inference-examples-claude-multimodal-code-example)

## 訊息程式碼範例
<a name="api-inference-examples-claude-messages-code-example"></a>

此範例示範如何向 Anthropic Claude 3 Sonnet 模型傳送單一回合使用者訊息，以及具有預先填入助理訊息的使用者回合。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate a message with Anthropic Claude (on demand).
"""
import boto3
import json
import logging

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

def generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens):

    body=json.dumps(
        {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": max_tokens,
            "system": system_prompt,
            "messages": messages
        }  
    )  

    
    response = bedrock_runtime.invoke_model(body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())
   
    return response_body


def main():
    """
    Entrypoint for Anthropic Claude message example.
    """

    try:

        bedrock_runtime = boto3.client(service_name='bedrock-runtime')

        model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
        system_prompt = "Please respond only with emoji."
        max_tokens = 1000

        # Prompt with user turn only.
        user_message =  {"role": "user", "content": "Hello World"}
        messages = [user_message]

        response = generate_message (bedrock_runtime, model_id, system_prompt, messages, max_tokens)
        print("User turn only.")
        print(json.dumps(response, indent=4))

        # Prompt with both user turn and prefilled assistant response.
        #Anthropic Claude continues by using the prefilled assistant text.
        assistant_message =  {"role": "assistant", "content": "<emoji>"}
        messages = [user_message, assistant_message]
        response = generate_message(bedrock_runtime, model_id,system_prompt, messages, max_tokens)
        print("User turn and prefilled assistant response.")
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message=err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occurred: " +
            format(message))

if __name__ == "__main__":
    main()
```

## 多模態程式碼範例
<a name="api-inference-examples-claude-multimodal-code-example"></a>

下列範例示範如何將多模態訊息中的影像和提示文字傳遞至 Anthropic Claude 3 Sonnet 模型。

**Topics**
+ [使用 InvokeModel 的多模態提示](#api-inference-examples-claude-multimodal-code-example-invoke-model)
+ [使用 InvokeModelWithResponseStream 串流多模態提示](#api-inference-examples-claude-multimodal-code-example-streaming)

### 使用 InvokeModel 的多模態提示
<a name="api-inference-examples-claude-multimodal-code-example-invoke-model"></a>

下列範例示範如何使用 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 將多模態提示傳送至 Anthropic Claude 3 Sonnet。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Anthropic Claude (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """



    body = json.dumps(
        {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": max_tokens,
            "messages": messages
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Anthropic Claude multimodal prompt example.
    """

    try:

        bedrock_runtime = boto3.client(service_name='bedrock-runtime')

        model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
        max_tokens = 1000
        input_text = "What's in this image?"
        input_image = "/path/to/image" # Replace with actual path to image file
 
        # Read reference image from file and encode as base64 strings.
        image_ext = input_image.split(".")[-1]
        with open(input_image, "rb") as image_file:
            content_image = base64.b64encode(image_file.read()).decode('utf8')

        message = {
            "role": "user",
            "content": [
                {
                    "type": "image", 
                    "source": {
                        "type": "base64",
                        "media_type": f"image/{image_ext}", 
                        "data": content_image
                    }
                },
                {
                    "type": "text", 
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occurred: " +
              format(message))


if __name__ == "__main__":
    main()
```

### 使用 InvokeModelWithResponseStream 串流多模態提示
<a name="api-inference-examples-claude-multimodal-code-example-streaming"></a>

下列範例示範如何使用 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) 從傳送至 Anthropic Claude 3 Sonnet 的多模態提示串流回應。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to stream the response from Anthropic Claude Sonnet (on demand) for a 
multimodal request.
"""

import json
import base64
import logging
import boto3

from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def stream_multi_modal_prompt(bedrock_runtime, model_id, input_text, image, max_tokens):
    """
    Streams the response from a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        input_text (str) : The prompt text
        image (str) : The path to  an image that you want in the prompt.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """

    with open(image, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())

    body = json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": max_tokens,
        "messages": [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": input_text},
                    {"type": "image", "source": {"type": "base64",
                                                 "media_type": "image/jpeg", "data": encoded_string.decode('utf-8')}}
                ]
            }
        ]
    })

    response = bedrock_runtime.invoke_model_with_response_stream(
        body=body, modelId=model_id)

    for event in response.get("body"):
        chunk = json.loads(event["chunk"]["bytes"])

        if chunk['type'] == 'message_delta':
            print(f"\nStop reason: {chunk['delta']['stop_reason']}")
            print(f"Stop sequence: {chunk['delta']['stop_sequence']}")
            print(f"Output tokens: {chunk['usage']['output_tokens']}")

        if chunk['type'] == 'content_block_delta':
            if chunk['delta']['type'] == 'text_delta':
                print(chunk['delta']['text'], end="")


def main():
    """
    Entrypoint for Anthropic Claude Sonnet multimodal prompt example.
    """

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    input_text = "What can you tell me about this image?"
    image = "/path/to/image"
    max_tokens = 100

    try:

        bedrock_runtime = boto3.client('bedrock-runtime')

        stream_multi_modal_prompt(
            bedrock_runtime, model_id, input_text, image, max_tokens)

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))


if __name__ == "__main__":
    main()
```

# 支援的模型
<a name="claude-messages-supported-models"></a>

您可以搭配下列 Anthropic Claude 模型使用訊息 API。
+ Anthropic Claude Opus 4.5
+ Anthropic Claude Opus 4.1
+ Anthropic Claude Opus 4 
+ Anthropic Claude Sonnet 4.5 
+ Anthropic Claude Haiku 4.5 
+ Anthropic Claude Sonnet 4 
+ Anthropic Claude 3.7 Sonnet 
+ Anthropic Claude 3.5 Sonnet v2 
+ Anthropic Claude 3.5 Sonnet 
+ Anthropic Claude 3 Opus 
+ Anthropic Claude 3 Sonnet 
+ Anthropic Claude 3 Haiku 
+ Anthropic Claude 2 v2.1 
+ Anthropic Claude 2 v2 
+ Anthropic Claude Instant v1.2