For AI agents: the complete documentation index is available at https://little-butterfly-9bfe.sentia389.workers.dev/llms.txt, the full documentation bundle is available at https://little-butterfly-9bfe.sentia389.workers.dev/llms-full.txt, and this page is available as Markdown at https://little-butterfly-9bfe.sentia389.workers.dev/start/chat-completions.md.
快速接入POST/v1/chat/completions

OpenAI Chat Completions

向 https://routerlio.com/v1/chat/completions 发送 POST 请求,在请求体中传 messages,从 choices 读取回答。鉴权使用 Authorization: Bearer <ROUTERLIO_API_KEY>。按快速接入设置环境变量,并从 GET /v1/models 的 data[].id 复制完整模型 ID。

可按厂商查阅型号文档和参数。OpenAI · Anthropic Claude · Google Gemini · Moonshot Kimi

最小请求

在 Bash 中运行以下命令。jq 将 ROUTERLIO_MODEL 的值写入请求体。

Bash
payload=$(jq -n \
  --arg model "${ROUTERLIO_MODEL:?请设置 ROUTERLIO_MODEL}" \
  '{
    model: $model,
    messages: [
      {
        role: "user",
        content: "请说你好。"
      }
    ]
  }') &&
curl -sS \
  --url "${ROUTERLIO_BASE_URL:?请设置 ROUTERLIO_BASE_URL}/v1/chat/completions" \
  --header "Authorization: Bearer ${ROUTERLIO_API_KEY:?请设置 ROUTERLIO_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data-binary "$payload"

在 API 客户端中可直接使用以下 JSON 请求体。将 gpt-6.2-sol 换成 GET /v1/models 返回的完整 ID,再用此请求验证该模型能否通过 Chat Completions 生成文本。

JSON
{
  "model": "gpt-6.2-sol",
  "messages": [
    {
      "role": "user",
      "content": "请说你好。"
    }
  ]
}
请求字段类型必填说明
model字符串是从 GET /v1/models 响应的 data[].id 复制完整模型 ID。
messages对象数组普通对话必填按顺序传递对话消息。纯文本用 role 和字符串 content,角色及其它内容类型的支持范围取决于模型。
stream布尔值否设为 true 请求 SSE。
max_completion_tokens整数否生成 token 上限,推理模型还可能把推理计入。按型号文档确认使用 max_completion_tokens 或 max_tokens。
stream_options.include_usage布尔值否请求流式用量。若收到单独的用量块,其中的 choices 可以为空。
reasoning_effort字符串否推理强度,可用枚举和默认值查具体系列页。
temperature、top_p数字否采样设置。能否传入、允许范围及其和推理强度的关系取决于目标模型。
tools、tool_choice数组、字符串或对象否描述工具和选择规则。常见函数工具用 tools[].function.name 和 tools[].function.parameters。
response_format对象否指定结构化输出格式,常见类型为 json_object 或 json_schema。使用 json_schema 时提供对应结构定义。

读取 JSON 响应

响应字段说明
id、model响应 ID 和响应报告的模型。
choices[].message.content文本输出。若模型返回工具调用,检查 choices[].message.tool_calls。
choices[].finish_reason结束原因,例如 stop、length 或 tool_calls。
usage返回时可读取 prompt_tokens、completion_tokens 和 total_tokens。

非流式响应示例数据。

JSON
{
  "id": "chatcmpl_example",
  "object": "chat.completion",
  "created": 1795900800,
  "model": "gpt-6.2-sol",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好。"
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 3,
    "total_tokens": 11
  }
}

读取流式响应

在命令的 jq 对象中加入 stream: true,并给 cURL 加 -N。按各个 choice.index 拼接 delta.content,允许个别数据块只有角色、结束原因或用量。用 finish_reason 判断生成结果,收到 data: [DONE] 时结束解析。若使用函数工具,按 choice.index 和 delta.tool_calls[].index 累积参数片段,完整拼接后再解析参数 JSON。

SSE 示例片段,已省略其它数据块。

文本
data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1795900800,"model":"gpt-6.2-sol","choices":[{"index":0,"delta":{"content":"你好。"},"finish_reason":null}]}

data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1795900800,"model":"gpt-6.2-sol","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

常见失败

现象处理方式
请求提示 field messages is required普通对话请传非空 messages 数组。Responses 的 input 用在 Responses 入口。
HTTP 成功但没有可读文本检查 finish_reason 和 message.tool_calls。流式请求检查 delta,并允许 choices 为空。
stream_options 或 token 上限字段被拒绝先运行最小请求,再对照型号文档逐项加回可选字段。
Kimi K3 请求因 temperature、top_p 或 thinking 报错从请求体移除固定字段和 thinking。如需调整推理强度,使用 reasoning_effort。Kimi 参数限制

其它状态码及请求 ID 的定位方式见故障排查。

官方定义