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

# Claude Messages

> 创建 Claude 原生消息，保留内容块与结束原因。

`POST /v1/messages`

使用 `x-api-key` 发送网关令牌，显式设置 `anthropic-version`。原生请求包含 `model`、`messages`；本例显式提供 `max_tokens`，系统指令放在顶层 `system`。参数表保留网关允许缺省预算的行为，实际原生供应商请求仍应提供模型支持的预算。

非流式响应读取 `content[]` 并检查 `stop_reason`。`end_turn` 之外的状态需要相应处理；`tool_use` 不是最终文本，`max_tokens` 可能截断。设置 `stream: true` 时按 Claude SSE 读取，不能套用 Chat / Responses 事件解析。

[原生 Claude 指南与完整 TypeScript 示例](/zh/guides/native-claude)包含环境变量、执行命令及失败行为。本站只展开常用文本字段，不承诺所有原生扩展或转换路径可用。`/v1/messages/count_tokens` 尚未启用。

核对版本：`d851a5faa49a00abaafd27bf91b311506fbfbc52`. 源文件记录在此操作的 `x-source-files`。

[原生 SSE 与工具闭环的 Python 示例](/zh/guides/native-claude-streaming)提供纯文本和工具两种模式，保留原生结果关联与签名。

[Read in English](/en/api-reference/claude-messages)

## 原生缓存与推理控制

请求级与内容块的 `cache_control`、顶层 `thinking`、`output_config` 和 `context_management` 已在项目 DTO 中表达，实际支持、TTL、预算和模式取决于渠道及模型。`context_management` 是 Claude 专用对象，转换成 Responses 时不会直接复制；同名字段不代表跨协议等价。

原生缓存用量可能包含 `usage.cache_read_input_tokens` 与 `cache_creation_input_tokens`。缓存、推理和压缩状态都不能自动增加模型上下文窗口；thinking / signature / redacted\_thinking 等续聊项按原生协议完整保留。

新增字段核对版本：`30a6d88a4`。

[提示词缓存](/zh/guides/prompt-caching) · [推理模型](/zh/guides/reasoning-models) · [上下文管理](/zh/guides/context-management)


## OpenAPI

````yaml openapi/relay.json POST /v1/messages
openapi: 3.0.3
info:
  title: API — Core inference API
  version: 0.5.0
  description: >-
    面向接入的精选规范，不是全部管理或供应商接口。 / Curated integration specification, not the entire
    management or provider API.
  license:
    name: AGPL-3.0-or-later
servers:
  - url: https://gateway.example.com
    description: >-
      Example only; replace with your gateway origin and optional path prefix,
      without /v1.
security:
  - BearerAuth: []
paths:
  /v1/messages:
    post:
      summary: Claude 原生消息 / Native Claude message
      description: >-
        精选文本接入字段；允许扩展字段不代表所有渠道支持。原生流式和工具扩展见指南。 / Selected text integration
        fields; accepting extensions does not guarantee channel support. See the
        guide for native streaming and tool boundaries.
      operationId: createClaudeMessage
      parameters:
        - name: anthropic-version
          in: header
          required: false
          description: 适配器缺省版本；建议显式发送 / Adapter fallback version; send explicitly.
          schema:
            type: string
            default: '2023-06-01'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  minLength: 1
                  example: YOUR_MODEL_ID
                max_tokens:
                  type: integer
                  minimum: 0
                  maximum: 1073741823
                  example: 256
                  description: >-
                    输出预算，实际范围依赖模型；调用原生供应商时应显式设置 / Output budget; model limits
                    apply. Set explicitly for native provider requests.
                system:
                  oneOf:
                    - type: string
                    - type: array
                      items:
                        type: object
                        required:
                          - type
                        properties:
                          type:
                            type: string
                          text:
                            type: string
                          id:
                            type: string
                          name:
                            type: string
                          input:
                            type: object
                            additionalProperties: true
                          tool_use_id:
                            type: string
                          content:
                            oneOf:
                              - type: string
                              - type: array
                                items:
                                  type: object
                                  additionalProperties: true
                          thinking:
                            type: string
                          signature:
                            type: string
                          data:
                            type: string
                          is_error:
                            type: boolean
                          cache_control:
                            type: object
                            description: >-
                              内容块缓存标记；具体支持取决于模型与渠道 / Content-block cache marker;
                              model and channel dependent
                            x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                            additionalProperties: true
                        additionalProperties: true
                messages:
                  type: array
                  minItems: 1
                  items:
                    type: object
                    required:
                      - role
                      - content
                    properties:
                      role:
                        type: string
                        enum:
                          - user
                          - assistant
                      content:
                        oneOf:
                          - type: string
                          - type: array
                            items:
                              type: object
                              required:
                                - type
                              properties:
                                type:
                                  type: string
                                text:
                                  type: string
                                id:
                                  type: string
                                name:
                                  type: string
                                input:
                                  type: object
                                  additionalProperties: true
                                tool_use_id:
                                  type: string
                                content:
                                  oneOf:
                                    - type: string
                                    - type: array
                                      items:
                                        type: object
                                        additionalProperties: true
                                thinking:
                                  type: string
                                signature:
                                  type: string
                                data:
                                  type: string
                                is_error:
                                  type: boolean
                                cache_control:
                                  type: object
                                  description: >-
                                    内容块缓存标记；具体支持取决于模型与渠道 / Content-block cache
                                    marker; model and channel dependent
                                  x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                                  additionalProperties: true
                              additionalProperties: true
                stream:
                  type: boolean
                  default: false
                tools:
                  type: array
                  items:
                    type: object
                    required:
                      - name
                      - input_schema
                    properties:
                      name:
                        type: string
                      description:
                        type: string
                      input_schema:
                        type: object
                        additionalProperties: true
                        description: >-
                          工具参数 Schema；示例额外执行本地校验 / Tool parameter schema;
                          examples also validate locally.
                    additionalProperties: true
                cache_control:
                  type: object
                  description: >-
                    原生缓存控制；依赖渠道，TTL 与断点限制不能跨供应商假定一致 / Native cache control;
                    channel dependent, TTL and breakpoint limits vary
                  x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                  additionalProperties: true
                  example:
                    type: ephemeral
                thinking:
                  type: object
                  description: >-
                    原生推理配置；模式与预算由目标模型决定 / Native reasoning configuration; target
                    model determines modes and budgets
                  x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                  additionalProperties: true
                output_config:
                  type: object
                  description: >-
                    原生输出配置，包含条件支持的 effort / Native output configuration,
                    including conditional effort support
                  x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                  additionalProperties: true
                context_management:
                  type: object
                  description: >-
                    Claude 专用上下文编辑对象；Claude 转 Responses 不直接复制 / Claude-specific
                    context editing; not copied directly into Responses
                  x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                  additionalProperties: true
              additionalProperties: true
      responses:
        '200':
          description: 检查原生完成状态及内容 / Check native completion status and content.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  type:
                    type: string
                  role:
                    type: string
                  model:
                    type: string
                  stop_reason:
                    type: string
                    nullable: true
                  content:
                    type: array
                    items:
                      type: object
                      required:
                        - type
                      properties:
                        type:
                          type: string
                        text:
                          type: string
                        id:
                          type: string
                        name:
                          type: string
                        input:
                          type: object
                          additionalProperties: true
                        tool_use_id:
                          type: string
                        content:
                          oneOf:
                            - type: string
                            - type: array
                              items:
                                type: object
                                additionalProperties: true
                        thinking:
                          type: string
                        signature:
                          type: string
                        data:
                          type: string
                        is_error:
                          type: boolean
                      additionalProperties: true
                  usage:
                    type: object
                    additionalProperties: true
                    properties:
                      cache_read_input_tokens:
                        type: integer
                        description: 原生缓存读数；可能缺失 / Native cache reads; may be absent
                        x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                        minimum: 0
                      cache_creation_input_tokens:
                        type: integer
                        description: 原生缓存写入读数；可能缺失 / Native cache writes; may be absent
                        x-source-revision: 30a6d88a4fa41009755d0f157d1656ced297803d
                        minimum: 0
                additionalProperties: true
              example:
                type: message
                id: msg_example
                role: assistant
                stop_reason: end_turn
                content:
                  - type: text
                    text: Hello.
            text/event-stream:
              schema:
                type: string
              example: |+
                event: message_stop
                data: {"type":"message_stop"}

        default:
          description: >-
            网关或上游错误，结构可能随协议变化 / Gateway or upstream error; structure may vary by
            protocol.
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
      security:
        - ClaudeApiKey: []
        - BearerAuth: []
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: 模型调用 API Key，不是控制台登录凭据 / Model API key, not console login credentials
    ClaudeApiKey:
      type: apiKey
      in: header
      name: x-api-key
      description: 网关令牌 / Gateway token

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.