> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sunra.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a message

使用 Anthropic Messages API 格式创建消息。支持文本、图像、PDF、工具和扩展思维。

## 认证

<ParamField header="Authorization" type="string" required>
  Bearer 令牌。在 Authorization 请求头中使用您的 API 密钥作为 Bearer 令牌。

  格式：`Bearer <SUNRA_KEY>`
</ParamField>

## 请求

此端点接受一个 JSON 对象。

<ParamField body="model" type="string" required>
  将完成您的提示的模型。在 [sunra.ai/models](https://sunra.ai/models) 浏览可用模型。
</ParamField>

<ParamField body="provider" type="object">
  可选的 Provider 路由偏好。省略时使用自动路由。支持的字段和 Provider 查询方式见 [Provider 路由](/zh-Hans/llm/provider-routing)。
</ParamField>

<ParamField body="messages" type="object[]" required>
  输入消息。每条输入消息都有一个 `role` 和 `content`。

  <Expandable title="属性">
    <ParamField body="role" type="string" required>
      消息作者的角色。支持的值：`user`、`assistant`。
    </ParamField>

    <ParamField body="content" type="string | object[]" required>
      消息的内容。可以是单个字符串或内容块数组。

      <Expandable title="内容块类型">
        <ParamField body="type" type="string" required>
          内容块的类型。支持的值：`text`、`image`、`tool_use`、`tool_result`。
        </ParamField>

        <ParamField body="text" type="string">
          文本内容。当类型为 `text` 时使用。
        </ParamField>

        <ParamField body="source" type="object">
          图像来源。当类型为 `image` 时使用。

          <Expandable title="属性">
            <ParamField body="type" type="string" required>
              来源类型。支持的值：`base64`、`url`。
            </ParamField>

            <ParamField body="media_type" type="string" required>
              图像的媒体类型。例如 `image/jpeg`、`image/png`、`image/gif`、`image/webp`。
            </ParamField>

            <ParamField body="data" type="string">
              Base64 编码的图像数据。当来源类型为 `base64` 时必填。
            </ParamField>

            <ParamField body="url" type="string">
              图像 URL。当来源类型为 `url` 时必填。
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="max_tokens" type="integer" required>
  停止前生成的最大令牌数。请注意，模型可能会在达到此最大值之前停止。不同模型的上限不同；超过模型自身上限的值会返回 `400`，而不会被静默削平。见[输出上限](/zh-Hans/llm/limits)。
</ParamField>

<ParamField body="system" type="string | object[]">
  系统提示。系统提示是一种为模型提供上下文和指令的方式。可以是字符串或内容块数组。
</ParamField>

<ParamField body="stream" type="boolean" default={false}>
  是否使用服务器发送事件（SSE）增量流式传输响应。流式请求受空闲超时与生命周期上限约束，见[输出上限与流生命周期](/zh-Hans/llm/limits#流生命周期)。
</ParamField>

<ParamField body="temperature" type="number" default={1}>
  注入到响应中的随机性量。范围从 0.0 到 1.0。对于分析/多选任务使用接近 0.0 的 `temperature`，对于创意和生成任务使用接近 1.0 的值。
</ParamField>

<ParamField body="top_p" type="number">
  使用核采样。在核采样中，我们按概率递减顺序计算所有后续令牌选项的累积分布，并在达到由 `top_p` 指定的特定概率时截断。
</ParamField>

<ParamField body="top_k" type="integer">
  仅从每个后续令牌的前 K 个选项中采样。用于移除"长尾"低概率响应。仅建议高级用例使用。
</ParamField>

<ParamField body="stop_sequences" type="string[]">
  自定义文本序列，将导致模型停止生成。返回的文本将不包含停止序列。
</ParamField>

<ParamField body="tools" type="object[]">
  模型可以使用的工具定义。

  <Expandable title="属性">
    <ParamField body="name" type="string" required>
      工具的名称。
    </ParamField>

    <ParamField body="description" type="string">
      此工具功能的描述。
    </ParamField>

    <ParamField body="input_schema" type="object" required>
      此工具输入的 JSON Schema。定义了工具接受的 `input` 的结构。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="object">
  模型应如何使用提供的工具。

  <Expandable title="属性">
    <ParamField body="type" type="string" required>
      支持的值：`auto`（默认，模型决定）、`any`（模型必须使用工具）、`tool`（模型必须使用特定工具）。
    </ParamField>

    <ParamField body="name" type="string">
      要使用的工具名称。当类型为 `tool` 时必填。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="metadata" type="object">
  描述请求元数据的对象。

  <Expandable title="属性">
    <ParamField body="user_id" type="string">
      与请求关联的用户的外部标识符。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="thinking" type="object">
  扩展思维的配置。启用后，模型将在响应之前进行思考。

  <Expandable title="属性">
    <ParamField body="type" type="string" required>
      必须为 `enabled`。
    </ParamField>

    <ParamField body="budget_tokens" type="integer" required>
      用于思考的最大令牌数。必须大于或等于 1024。
    </ParamField>
  </Expandable>
</ParamField>

## 响应

成功的消息响应。

<ResponseField name="id" type="string">
  唯一消息标识符，例如 `msg_01XFDUDYJgAACzvnptvVoYEL`。
</ResponseField>

<ResponseField name="type" type="string">
  对象类型。始终为 `message`。
</ResponseField>

<ResponseField name="role" type="string">
  生成消息的对话角色。始终为 `assistant`。
</ResponseField>

<ResponseField name="content" type="object[]">
  模型生成的内容。这是一个内容块数组。

  <Expandable title="属性">
    <ResponseField name="type" type="string">
      内容块的类型。可以是 `text`、`tool_use` 或 `thinking`。
    </ResponseField>

    <ResponseField name="text" type="string">
      生成的文本。当类型为 `text` 时存在。
    </ResponseField>

    <ResponseField name="id" type="string">
      工具使用块的 ID。当类型为 `tool_use` 时存在。
    </ResponseField>

    <ResponseField name="name" type="string">
      工具的名称。当类型为 `tool_use` 时存在。
    </ResponseField>

    <ResponseField name="input" type="object">
      工具的输入。当类型为 `tool_use` 时存在。
    </ResponseField>

    <ResponseField name="thinking" type="string">
      思考内容。当类型为 `thinking` 时存在。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="model" type="string">
  处理请求的模型。
</ResponseField>

<ResponseField name="stop_reason" type="string | null">
  模型停止的原因。可以是 `end_turn`（模型到达自然停止点）、`max_tokens`（超过 `max_tokens` 或模型的最大值）、`stop_sequence`（生成了您的自定义停止序列之一）或 `tool_use`（模型调用了一个或多个工具）。
</ResponseField>

<ResponseField name="stop_sequence" type="string | null">
  生成了哪个自定义停止序列（如果有）。
</ResponseField>

<ResponseField name="usage" type="object">
  计费和速率限制使用情况。三个输入桶互斥——参见[令牌用量](/zh-Hans/llm/token-usage)。

  <Expandable title="属性">
    <ResponseField name="input_tokens" type="integer">
      使用的全新输入令牌数。不含两个缓存桶。
    </ResponseField>

    <ResponseField name="output_tokens" type="integer">
      使用的输出令牌数。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      所有输入桶加上 `output_tokens`。streaming 响应中会省略。
    </ResponseField>

    <ResponseField name="cache_creation_input_tokens" type="integer">
      用于创建缓存条目的输入令牌数。
    </ResponseField>

    <ResponseField name="cache_read_input_tokens" type="integer">
      从缓存读取的输入令牌数。
    </ResponseField>

    <ResponseField name="sunra_usage_semantics" type="string | null">
      当 Sunra 对该响应做过归一化时，其值为 `anthropic.exclusive.v1`。请对该值做断言，而不要从数字推断所遵循的约定。参见[令牌用量](/zh-Hans/llm/token-usage)。
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/messages \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "anthropic/claude-sonnet-4-6",
      "max_tokens": 1024,
      "messages": [
        {
          "role": "user",
          "content": "Hello, how are you?"
        }
      ]
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://api-llm.sunra.ai/v1/messages",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "anthropic/claude-sonnet-4-6",
          "max_tokens": 1024,
          "messages": [
              {"role": "user", "content": "Hello, how are you?"}
          ]
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api-llm.sunra.ai/v1/messages", {
    method: "POST",
    headers: {
      "Authorization": "Bearer <SUNRA_KEY>",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "anthropic/claude-sonnet-4-6",
      max_tokens: 1024,
      messages: [
        { role: "user", content: "Hello, how are you?" }
      ]
    })
  });
  const data = await response.json();
  console.log(data);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": "Hello! I'm doing well, thank you for asking. How can I help you today?"
      }
    ],
    "model": "anthropic/claude-sonnet-4-6",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 12,
      "output_tokens": 19,
      "total_tokens": 31,
      "sunra_usage_semantics": "anthropic.exclusive.v1"
    }
  }
  ```
</ResponseExample>
