跳转到主要内容

概览

CRUN 在 https://api.crun.ai/api/v1 下提供 OpenAI 和 Anthropic 兼容的语言模型网关。 您只需要修改 base URL 和 API Key,就可以继续使用官方 OpenAI SDK,Anthropic SDK 或任何已经兼容 OpenAI 风格 Chat API 的客户端。
当前支持的兼容接口:
  • GET /models:列出当前可用的公开模型 ID
  • POST /responses:OpenAI最新的接口,推荐用于新的有状态、多模态和工具调用语言模型工作流
  • POST /chat/completions:面向现有聊天客户端的 OpenAI 兼容聊天生成接口
  • POST /messages:面向 Claude 风格消息客户端的 Anthropic 兼容接口
  • POST /completions:旧版文本补全接口;在当前兼容层中不支持流式输出

Base URL

https://api.crun.ai/api/v1

身份验证

您可以使用以下任一方式进行身份验证:
  • Authorization: Bearer YOUR_API_KEY
  • X-API-KEY: YOUR_API_KEY
如果使用官方 OpenAI SDK,推荐使用 Authorization: Bearer YOUR_API_KEY
请妥善保管您的 API Key。切勿在客户端代码或公开代码仓库中暴露它。

第 1 步:列出可用模型

模型目录可能会随时间变化。集成或切换模型前,建议始终调用 /models 获取最新的公开模型 ID。
curl -X GET "https://api.crun.ai/api/v1/models" \
  -H "Authorization: Bearer YOUR_API_KEY"

响应示例

{
  "object": "list",
  "data": [
    {
      "id": "gpt-5.4",
      "object": "model",
      "created": 1772294400,
      "owned_by": "OpenAI"
    },
    {
      "id": "claude-sonnet-4-5",
      "object": "model",
      "created": 1772294400,
      "owned_by": "Anthropic"
    },
    {
      "id": "gemini-3.1-pro-preview",
      "object": "model",
      "created": 1772294400,
      "owned_by": "Google"
    }
  ]
}

第 2 步:发起第一次 Chat Completion 请求

如果您已有 OpenAI Chat Completions 客户端,或需要经典的 messageschoices 响应结构,可以使用 /chat/completions。该接口遵循 OpenAI Chat Completions 的请求和响应结构,因此现有 SDK 集成通常只需要替换 base URL。
curl -X POST "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "You are a concise assistant."
      },
      {
        "role": "user",
        "content": "Write a two-line product description for a wireless keyboard."
      }
    ],
    "temperature": 0.7
  }'

响应示例

{
  "id": "chatcmpl_abc123",
  "object": "chat.completion",
  "created": 1772294400,
  "model": "gpt-5.4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Slim, quiet, and built for all-day comfort.\nA reliable wireless keyboard for focused work at home or on the go."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 21,
    "total_tokens": 45
  }
}

第 3 步:使用 Responses API

新的集成建议优先考虑 /responses。它支持 OpenAI 更新的 Responses 形态,包括顶层 instructions、灵活的 input、用于有状态追问的 previous_response_id、用于结构化输出的 text.format,以及上游模型支持时可用的 tools
curl -X POST "https://api.crun.ai/api/v1/responses" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "instructions": "You are a concise assistant.",
    "input": "Write a two-line product description for a wireless keyboard.",
    "temperature": 0.7
  }'

响应示例

{
  "id": "resp_abc123",
  "object": "response",
  "created_at": 1772294400,
  "status": "completed",
  "model": "gpt-5.4",
  "output": [
    {
      "id": "msg_abc123",
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Slim, quiet, and built for all-day comfort.\nA reliable wireless keyboard for focused work at home or on the go."
        }
      ]
    }
  ],
  "output_text": "Slim, quiet, and built for all-day comfort.\nA reliable wireless keyboard for focused work at home or on the go.",
  "usage": {
    "input_tokens": 20,
    "output_tokens": 21,
    "total_tokens": 41
  }
}

管理对话历史

CRUN 不会为您的应用存储或管理对话状态。 如果希望模型记住之前的对话轮次,您的应用需要自行保存相关消息历史,并在每次新的 /chat/completions 请求中重新传入。
curl -X POST "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "You are a concise travel assistant."
      },
      {
        "role": "user",
        "content": "I will spend 2 days in Tokyo. Build a simple itinerary."
      },
      {
        "role": "assistant",
        "content": "Day 1: Asakusa and Ueno. Day 2: Shibuya and Meiji Shrine."
      },
      {
        "role": "user",
        "content": "Update it for rainy weather and add indoor options."
      }
    ],
    "temperature": 0.7
  }'

流式输出

设置 stream=true 后,可以从 /chat/completions 接收 Server-Sent Events。 如果未提供 stream_options.include_usage,CRUN 会自动启用该选项,以便在流式响应接近结束时包含用量信息。
curl -N "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "stream": true,
    "messages": [
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Write a two-line product description for a wireless keyboard."}
    ]
  }'

响应示例

stream response
data: {"id":"","object":"chat.completion.chunk","created":0,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":"S"}}]}

data: {"id":"","object":"chat.completion.chunk","created":0,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":"le"}}]}

data: {"id":"resp_0c628e55cfc393b50069cddfec989c81939cd408ac45285daa","object":"chat.completion.chunk","created":1775099884,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop"}],"usage":{"prompt_tokens":17,"completion_tokens":40,"total_tokens":57}}

data: [DONE]

注意事项

  • max_tokensmax_completion_tokens 都受支持。实际值会受所选模型的输出上限约束。
  • /responses 支持 max_output_tokens,实际值同样会受所选模型的输出上限约束。
  • 其他 OpenAI 兼容字段也会被接受,并在上游模型支持时透传。
  • 计费基于输入和输出 token 用量,并按具体模型设置最低计费值。最低值通常为 0.01。
  • 新的有状态或工具调用工作流建议使用 /responses。现有聊天客户端仍可继续使用 /chat/completions。旧版 /completions 接口仅为兼容而提供,不支持 stream=true

相关资源

Responses API

使用灵活输入、有状态追问、结构化输出和工具就绪的 response items 构建应用。

Messages API

使用 Anthropic 兼容 messages、system prompt、工具调用、thinking 和流式事件。

Chat Completions API

查看请求参数、流式输出和 OpenAI 风格错误响应的完整 API 说明。

账户积分

在运行大批量语言模型任务前,查询账户剩余积分。

价格

前往价格页面,比较不同模型的计费规则。