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

# 创建消息

> `/v1/messages` 上兼容 Anthropic 的 Messages 端点。

`/v1/messages` 接受 Anthropic Messages API 请求正文，并返回 Anthropic 格式的响应。

## 流式传输

设置 `stream: true` 即可接收 Anthropic 格式的服务器发送事件（`message_start`、`content_block_*`、`message_delta`、`message_stop`）。

## 说明

* 支持 Anthropic 工具调用字段。
* `stream: true` 也适用于工具调用循环。对于 Gateway 管理的服务器工具，Phaseo 可能会处理上游工具调用轮次、继续循环，再重新发出合成流。
* 也可以直接在 `tools` 中使用 Anthropic 原生网页搜索工具格式，例如 `type: "web_search_20250305"`。
* `X-Phaseo-Strictness` 标头用于控制如何处理不支持的参数。

## 服务器工具

`/v1/messages` 还支持以下由 Gateway 管理的服务器工具：

* `gateway:datetime`
* `phaseo:web_search`
* `phaseo:web_fetch`
* `phaseo:advisor`
* `phaseo:image_generation`

Phaseo 会将这些工具转换为上游兼容 Anthropic 的格式，在服务器端执行，然后继续工具调用循环。

## 提供商原生网页搜索

如果兼容的 Anthropic 模型和提供商组合支持原生网页搜索，也可以直接传入原生工具定义：

```bash theme={null}
curl https://api.phaseo.app/v1/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4",
    "max_tokens": 512,
    "messages": [
      { "role": "user", "content": "Find the latest Anthropic web search guidance." }
    ],
    "tools": [
      {
        "type": "web_search_20250305",
        "name": "web_search",
        "max_uses": 3,
        "allowed_domains": ["docs.anthropic.com"]
      }
    ],
    "tool_choice": { "type": "tool", "name": "web_search" }
  }'
```

执行前，提供商路由仍会检查是否支持 `web_search_options`。

### 日期和时间示例

```bash theme={null}
curl https://api.phaseo.app/v1/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4",
    "max_tokens": 512,
    "stream": false,
    "messages": [
      { "role": "user", "content": "What time is it in America/New_York right now?" }
    ],
    "tools": [
      {
        "type": "gateway:datetime",
        "parameters": { "timezone": "America/New_York" }
      }
    ],
    "tool_choice": { "type": "auto" }
  }'
```

使用服务器工具时，`usage.server_tool_use.*` 计数器会记录对应的调用。


## OpenAPI

````yaml zh-Hans/openapi/v1/openapi.localized.yaml POST /messages
openapi: 3.0.3
info:
  title: Phaseo Gateway API
  description: 通过兼容 OpenAI 的端点访问各种 AI 模型的网关 API。
  version: 1.0.0
  contact:
    name: Phaseo
    url: https://phaseo.app
    email: danielbutler500@gmail.com
servers:
  - url: https://api.phaseo.app/v1
    description: 全球路由
security:
  - BearerAuth: []
tags:
  - name: Gateway
    description: Core Phaseo Gateway operations.
paths:
  /messages:
    post:
      tags:
        - Gateway
      summary: 创建消息
      description: 使用 Anthropic Messages API 创建消息。
      operationId: createAnthropicMessage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AnthropicMessagesRequest'
            examples:
              datetimeServerTool:
                summary: Built-in datetime server tool
                value:
                  model: anthropic/claude-sonnet-4
                  max_tokens: 512
                  stream: false
                  messages:
                    - role: user
                      content: What time is it in America/New_York right now?
                  tools:
                    - type: gateway:datetime
                      parameters:
                        timezone: America/New_York
                  tool_choice:
                    type: auto
      responses:
        '200':
          description: 消息响应
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicMessagesResponse'
            text/event-stream:
              schema:
                type: string
      servers:
        - url: https://api.phaseo.app/v1
          description: 全球路由
        - url: https://eu.api.phaseo.app/v1
          description: 欧盟区域提供商路由（仅文本）
        - url: https://us.api.phaseo.app/v1
          description: 美国区域提供商路由（仅文本）
components:
  schemas:
    AnthropicMessagesRequest:
      type: object
      required:
        - model
        - messages
        - max_tokens
      properties:
        model:
          type: string
        system:
          oneOf:
            - type: string
            - type: array
              items:
                type: object
                properties:
                  type:
                    type: string
                    enum:
                      - text
                  text:
                    type: string
                  cache_control:
                    $ref: '#/components/schemas/CacheControl'
        messages:
          type: array
          minItems: 1
          items:
            $ref: '#/components/schemas/AnthropicMessage'
        max_tokens:
          type: integer
          minimum: 1
        service_tier:
          type: string
          description: 在兼容的文本 API 上选择受支持的路由或定价等级。
          enum:
            - standard
            - default
            - fast
            - ultrafast
            - priority
            - flex
            - batch
        temperature:
          type: number
          minimum: 0
          maximum: 1
        top_p:
          type: number
          minimum: 0
          maximum: 1
        top_k:
          type: integer
          minimum: 1
        tools:
          type: array
          description: 兼容 Anthropic 的工具，以及由网关管理的服务器工具。
          items:
            oneOf:
              - $ref: '#/components/schemas/AnthropicTool'
              - $ref: '#/components/schemas/GatewayDatetimeToolDefinition'
              - $ref: '#/components/schemas/GatewayWebSearchToolDefinition'
              - $ref: '#/components/schemas/GatewayWebFetchToolDefinition'
              - $ref: '#/components/schemas/SubagentToolDefinition'
              - $ref: '#/components/schemas/FusionToolDefinition'
              - $ref: '#/components/schemas/SearchModelsToolDefinition'
        tool_choice:
          description: Anthropic 工具选择对象或字符串。也接受由网关管理的服务器工具名称，并由网关进行改写。
          oneOf:
            - type: object
            - type: string
        stream:
          type: boolean
        metadata:
          type: object
          additionalProperties: true
        session_id:
          type: string
          maxLength: 256
          description: 用于可观测性统计的唯一标识符，可将相关请求（例如对话或代理工作流）归为一组。
        reasoning:
          $ref: '#/components/schemas/ReasoningConfig'
        stop_sequences:
          type: array
          items:
            type: string
        provider_options:
          $ref: '#/components/schemas/ProviderOptions'
        usage:
          type: boolean
        meta:
          type: boolean
        echo_upstream_request:
          type: boolean
        debug:
          $ref: '#/components/schemas/DebugOptions'
        provider:
          $ref: '#/components/schemas/ProviderRoutingOptions'
    AnthropicMessagesResponse:
      type: object
      properties:
        id:
          type: string
        type:
          type: string
        role:
          type: string
          enum:
            - assistant
        model:
          type: string
        content:
          type: array
          items:
            $ref: '#/components/schemas/AnthropicContentBlock'
        stop_reason:
          type: string
        stop_sequence:
          type: string
        usage:
          $ref: '#/components/schemas/AnthropicUsage'
    CacheControl:
      type: object
      properties:
        type:
          type: string
        ttl:
          type: string
        scope:
          type: string
      additionalProperties: true
    AnthropicMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - user
            - assistant
        content:
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/AnthropicContentBlock'
    AnthropicTool:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        async:
          type: boolean
          description: 允许受支持的 OpenAI 模型在调用方执行此工具时继续处理。路由至其他提供商时会忽略此设置。
        description:
          type: string
        input_schema:
          type: object
    GatewayDatetimeToolDefinition:
      type: object
      description: 由网关管理的服务器工具。网关会查询日期时间，并将结果注入模型工具循环。
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:datetime
            - gateway:datetime
        parameters:
          type: object
          properties:
            timezone:
              type: string
              description: IANA 时区名称（例如 Europe/London）。
          additionalProperties: false
        timezone:
          type: string
          description: 用于指定默认时区（IANA）的旧版快捷字段。
      additionalProperties: false
    GatewayWebSearchToolDefinition:
      type: object
      description: 由网关管理的服务器工具。网关会执行网页搜索，并将标准化的搜索结果注入模型工具循环。
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:web_search
            - gateway:web_search
        parameters:
          type: object
          properties:
            engine:
              type: string
              enum:
                - auto
                - native
                - exa
                - firecrawl
                - parallel
                - perplexity
                - tinyfish
            max_results:
              type: integer
              minimum: 1
              maximum: 25
            language:
              type: string
            page:
              type: integer
              minimum: 0
              maximum: 10
            include_text:
              type: boolean
            include_highlights:
              type: boolean
          additionalProperties: false
        max_results:
          type: integer
          minimum: 1
          maximum: 25
        engine:
          type: string
          enum:
            - auto
            - native
            - exa
            - firecrawl
            - parallel
            - perplexity
            - tinyfish
        language:
          type: string
        page:
          type: integer
          minimum: 0
          maximum: 10
        include_text:
          type: boolean
        include_highlights:
          type: boolean
      additionalProperties: false
    GatewayWebFetchToolDefinition:
      type: object
      description: 由网关管理的服务器工具。网关会抓取一个 HTTP(S) 页面，将其压缩为限定长度的文本，并把结果注入模型工具循环。
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:web_fetch
            - gateway:web_fetch
        parameters:
          type: object
          properties:
            max_chars:
              type: integer
              minimum: 1
              maximum: 100000
          additionalProperties: false
        max_chars:
          type: integer
          minimum: 1
          maximum: 100000
      additionalProperties: false
    SubagentToolDefinition:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:subagent
        parameters:
          type: object
          additionalProperties: true
    FusionToolDefinition:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:fusion
        parameters:
          type: object
          required:
            - analysis_models
          properties:
            analysis_models:
              type: array
              minItems: 2
              maxItems: 8
              items:
                type: string
            model:
              type: string
          additionalProperties: true
    SearchModelsToolDefinition:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - phaseo:search_models
        parameters:
          type: object
          properties:
            max_results:
              type: integer
              minimum: 1
              maximum: 20
    ReasoningConfig:
      type: object
      properties:
        effort:
          type: string
          enum:
            - none
            - minimal
            - low
            - medium
            - high
            - xhigh
            - max
          default: medium
        mode:
          type: string
          enum:
            - standard
            - pro
        summary:
          type: string
          enum:
            - auto
            - concise
            - detailed
          default: auto
        enabled:
          type: boolean
        max_tokens:
          type: integer
          minimum: 0
    ProviderOptions:
      type: object
      description: 可选的提供商专用选项。
      properties:
        openai:
          type: object
          properties:
            context_management:
              type: object
              description: 可选的 OpenAI 上下文管理配置。
              properties:
                type:
                  type: string
                  enum:
                    - compaction
                compact_threshold:
                  type: number
              required:
                - type
            prompt_cache_retention:
              type: string
        anthropic:
          type: object
          properties:
            cache_control:
              $ref: '#/components/schemas/CacheControl'
        google:
          type: object
          properties:
            cache_control:
              $ref: '#/components/schemas/CacheControl'
            cached_content:
              type: string
            cache_ttl:
              type: string
    DebugOptions:
      type: object
      description: 网关调试控制项。这些标志绝不会转发给提供商。
      properties:
        enabled:
          type: boolean
        return_upstream_request:
          type: boolean
        return_upstream_response:
          type: boolean
        trace:
          type: boolean
        trace_level:
          type: string
          enum:
            - summary
            - full
    ProviderRoutingOptions:
      type: object
      description: 用于网关选择的提供商路由偏好设置。
      properties:
        order:
          type: array
          items:
            type: string
        only:
          type: array
          items:
            type: string
        ignore:
          type: array
          items:
            type: string
        include_alpha:
          type: boolean
          description: 在路由中包含 alpha 提供商（默认关闭）。
        allow_fallbacks:
          type: boolean
          nullable: true
          description: 失败后允许回退到其他符合条件的提供商。
        require_parameters:
          type: boolean
          nullable: true
          description: 路由前要求提供商支持所请求的参数。
        required_execution_region:
          type: string
          nullable: true
          description: 将路由限制为具有所需执行区域的提供商。
        required_data_region:
          type: string
          nullable: true
          description: 将路由限制为具有所需数据区域的提供商。
        require_zero_data_retention:
          type: boolean
          nullable: true
          description: 将路由限制为支持零数据保留的提供商。
        data_collection:
          type: string
          nullable: true
          enum:
            - allow
            - deny
        zdr:
          type: boolean
          nullable: true
        enforce_distillable_text:
          type: boolean
          nullable: true
        quantizations:
          type: array
          nullable: true
          items:
            type: string
        sort:
          oneOf:
            - type: string
            - type: object
              additionalProperties: true
          description: 为此请求排列提供商，例如按价格、延迟或吞吐量排序。
        max_price:
          type: object
          properties:
            prompt:
              oneOf:
                - type: number
                - type: string
            completion:
              oneOf:
                - type: number
                - type: string
            image:
              oneOf:
                - type: number
                - type: string
            audio:
              oneOf:
                - type: number
                - type: string
            request:
              oneOf:
                - type: number
                - type: string
        preferred_min_throughput:
          oneOf:
            - type: number
            - type: object
              additionalProperties:
                type: number
        preferred_max_latency:
          oneOf:
            - type: number
            - type: object
              additionalProperties:
                type: number
    AnthropicContentBlock:
      type: object
      properties:
        type:
          type: string
          enum:
            - text
            - image
            - tool_use
            - tool_result
        text:
          type: string
        cache_control:
          $ref: '#/components/schemas/CacheControl'
        source:
          type: object
          properties:
            type:
              type: string
            media_type:
              type: string
            data:
              type: string
            url:
              type: string
        id:
          type: string
        name:
          type: string
        input:
          type: object
        tool_use_id:
          type: string
        content:
          type: string
    AnthropicUsage:
      type: object
      properties:
        input_tokens:
          type: integer
        output_tokens:
          type: integer
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: Bearer 令牌身份验证

````

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