

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 请求和响应
<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`、`InvokeModelWithResponseStream``Converse`、和`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 | [{"type": "text", "text": 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\_version** –（必要）Anthropic 版本。值必须为 `bedrock-2023-05-31`。
+ **max\_tokens** –（必要）停止前要生成的最大词元数。

  请注意，Anthropic Claude 模型可能会在达到值 `max_tokens` 之前停止生成词元。不同的 Anthropic Claude 模型对此参数有不同的最大值要求。有关更多信息，请参阅[模型比较](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)。
+ **messages** –（必要）输入消息。
  + **role** – 对话回合的角色。有效值为 `user` 和 `assistant`。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + **content** –（必填）对话回合的内容（采用对象数组形式）。每个对象均包含一个 **type** 字段，可以在其中指定下列值之一：
    + `text` – 如果指定此类型，则必须包括一个 **text** 字段并将文本提示指定为其值。如果数组中的另一个对象是图像，则此文本提示将应用于图像。
    + `image` – 如果您指定此类型，则必须包括一个映射到具有以下字段的对象的 **source** 字段：
      + **type** –（必要）图像的编码类型。您可以指定 `base64`。
      + **media\_type** –（必要）图像的类型。您可以指定以下图像格式。
        + `image/jpeg`
        + `image/png`
        + `image/webp` 
        + `image/gif`
      + **data** –（必要）图像的 base64 编码图像字节。最大图像大小为 3.75 MB。图像的最大高度和宽度为 8000 像素。

以下是可选参数。
+  **system** –（可选）请求的系统提示。您可以以字符串或内容块数组（例如`[{"type": "text", "text": "your prompt"}]`）的形式提供系统提示。

  系统提示是一种可以为 Anthropic Claude 提供上下文和说明的方式，例如指定特定的目标或角色。有关更多信息，请参阅 Anthropic 文档中的[系统提示](https://docs.anthropic.com/en/docs/system-prompts)。
**注意**  
您可以在 Anthropic Claude 2.1 或更高版本中使用系统提示。
+ **anthropic\_beta** –（可选）anthropic 测试版参数是一组由测试版标头构成的字符串列表，用于表明选择启用一组特定的测试版功能。
**注意**  
根据服务条款的定义，100万Claude Sonnet 4个代币上下文长度变体可在特定 AWS 地区作为 “测试 AWS 服务” 使用。它受您的协议和 AWS 服务条款 AWS 以及适用的 EULA 范本的约束。请参阅 [Amazon Bedrock 定价](https://aws.amazon.com/bedrock/pricing/)页面，了解有关较长上下文请求的定价的更多信息。对于超过 20 万个输入令牌的请求，长上下文速率适用于整个请求，而不仅仅是超过阈值的令牌。不同的服务配额适用（有关更多信息，请参阅中的 S **ervice Q** uota AWS 管理控制台 s）。

  可用的测试版标头包括：    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **stop\_sequences** –（可选）导致模型停止生成的自定义文本序列。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_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\_p** –（可选）使用 nucleus 采样。

  在 nucleus 采样中，Anthropic Claude 按概率递减顺序计算每个后续词元的所有选项的累积分布，并在达到 `top_p` 指定的特定概率后将其切断。调整采样参数时，请修改 `temperature` 或 `top_p`。请不要同时修改二者。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\_k** –（可选）仅从每个后续词元的前 K 个选项中取样。

  使用 `top_k` 移除长尾低概率响应。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **tools** –（可选）模型可能使用的工具的定义。
**注意**  
需要 Anthropic Claude 3 模型。

  如果您在请求中包含 `tools`，则模型可能会返回 `tool_use` 内容块，表示该模型使用这些工具。然后，您可以使用模型生成的工具输入来运行这些工具，接着使用 `tool_result` 内容块选择性地将结果返回到模型。

  您可以传递以下工具类型：

**自定义**  
自定义工具的定义。
  + （可选）**type** – 工具的类型。如果已定义，则使用值 `custom`。
  + **name** - 工具的名称。
  + **description** –（可选，但强烈推荐）工具的描述。
  + **input\_schema** – 工具的 JSON 架构。

**计算机**  
与 Computer Use API 配合使用的计算机工具的定义。
  +  **type** – 值必须为 `computer_20241022`。
  + **name** – 值必须为 `computer`。
  + （必要）**display\_height\_px** – 模型控制的显示屏高度，以像素为单位。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + （必要）**display\_width\_px** – 模型控制的显示屏宽度，以像素为单位。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + （可选）**display\_number** – 要控制的显示屏编号（仅与 X11 环境相关）。如果指定，则将在工具定义中为该工具提供显示屏编号。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)

**bash**  
与 Computer Use API 配合使用的 bash 工具的定义。
  + （可选）**type** – 值必须为 `bash_20241022`。
  + **name** – 值必须为 `bash`。

**文本编辑器**  
与 Computer Use API 配合使用的文本编辑器工具的定义。
  + （可选）**type** – 值必须为 `text_editor_20241022`。
  + **name** – 值必须为 `str_replace_editor`。
+  **tool\_choice** –（可选）指定模型应如何使用提供的工具。模型可以使用特定的工具、任何可用的工具，也可以自行决定使用什么工具。
**注意**  
需要 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\_reason 值的示例响应：

```
// 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\_reason** – Anthropic Claude 停止生成响应的原因。
  + **end\_turn** – 模型达到了自然停止点
  + **max\_tokens** – 生成的文本超过了 `max_tokens` 输入字段的值或超过了模型支持的最大词元数量。
  + **stop\_sequence** – 模型生成了一个您在 `stop_sequences` 输入字段中指定的停止序列。
  + **refusal** – 由于安全方面的顾虑，Claude 拒绝生成响应。
  + **tool\_use** – Claude 正在调用一个工具，并期望您执行该工具。
  + **model\_context\_window\_exceeded** – 模型因达到上下文窗口限制而停止生成。
    + Claude Sonnet 4.5 的新增功能
+ **stop\_sequence** – 结束生成的停止序列。
+ **type** – 响应的类型。此值始终为 `message`。
+ **role** – 生成的消息的对话角色。此值始终为 `assistant`。
+ **content** – 模型生成的内容。以数组形式返回。有以下三种类型的内容：*text*、*tool\_use* 和 *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\_tokens** – 请求中的输入词元数量。
  + **output\_tokens** – 模型在响应中生成的词元数量。
  + **stop\_sequence** – 模型生成了一个您在 `stop_sequences` 输入字段中指定的停止序列。

------

## 工作量参数（测试版）
<a name="model-parameters-anthropic-claude-effort-parameter"></a>

该`effort`参数可以替代考虑 Claude Opus 4.5的代币预算。这个参数告诉它应该Claude多么宽松地花费代币才能产生最佳结果，调整思维、工具调用和用户沟通中的代币使用情况。它可以与扩展思维模式一起使用，也可以不与扩展思维模式一起使用。

工作量参数可以设置为：
+ `high`（默认）— 根据需要Claude花费任意数量的代币以获得最佳结果
+ `medium`— 平衡代币使用量
+ `low`— 保守的代币使用情况

要使用此功能，您必须通过测试版标题`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"
    }]
}
```