> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.openmodels.market/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.openmodels.market/_mcp/server.

# 聊天补全

使用 `POST /chat/completions` 通过 OpenModels 发送兼容 OpenAI 的聊天请求。

基础 URL：

```text
https://api.getopenmodels.com/v1
```

端点：

```text
POST /chat/completions
```

## 请求

```json
{
  "model": "qwen3.5-flash",
  "route": { "provider": "<provider-for-this-model>" },
  "route_mode": "balanced",
  "messages": [
    {
      "role": "system",
      "content": "You are a concise assistant."
    },
    {
      "role": "user",
      "content": "Explain OpenModels in one sentence."
    }
  ],
  "max_tokens": 256,
  "temperature": 0.7
}
```

必填字段：

| 字段         | 描述                                                            |
| ---------- | ------------------------------------------------------------- |
| `model`    | Models 页面中的受支持模型 ID，或 `route:coding-agent` 这样的 Model Route ID |
| `messages` | 兼容 OpenAI 聊天格式的对话消息                                           |

常用可选字段：

| 字段            | 描述                                                     |
| ------------- | ------------------------------------------------------ |
| `max_tokens`  | 要生成的最大输出 token 数                                       |
| `temperature` | 采样温度。较低值更确定                                            |
| `top_p`       | 核采样参数                                                  |
| `stream`      | 设置为 `true` 以启用 server-sent event 流式传输                  |
| `tools`       | 面向支持工具调用模型的函数调用工具                                      |
| `tool_choice` | 控制模型是否可以调用工具                                           |
| `route`       | 可选的 OpenModels 供应商路由覆盖，例如 `{ "provider": "Alibaba" }`  |
| `route_mode`  | 对使用模式映射的 Model Routes 可选的模式：`fast`、`balanced` 或 `deep` |

参数支持情况可能因模型和上游供应商而异。如果所选模型不支持某个参数，请选择其他模型或移除该参数。

## 路由选择

本页示例包含可选的 `route` 和 `route_mode` 字段，方便你了解请求时路由控制应放在哪里。移除这两个字段即可使用所选模型的默认可用路由。

要将请求固定到特定供应商路由，请传入 `route.provider`，其值应是你已在模型表或 OpenModels 控制台中确认过的供应商名称：

```json
{
  "model": "qwen3.5-flash",
  "route": { "provider": "<provider-for-this-model>" },
  "route_mode": "balanced",
  "messages": [
    {
      "role": "user",
      "content": "Hello!"
    }
  ],
  "max_tokens": 256
}
```

固定供应商路由只会发送给所选供应商。如果该供应商路由对此模型不可用，请移除 `route` 或选择另一个供应商路由。

对于在控制台中创建的应用级路由组，请改为把 `route:*` ID 作为 `model` 传入。参见 [模型路由](/api-reference/models-routes/model-routes)。

## cURL

```bash
curl https://api.getopenmodels.com/v1/chat/completions \
  -H "Authorization: Bearer $OM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.5-flash",
    "route": { "provider": "<provider-for-this-model>" },
    "route_mode": "balanced",
    "messages": [
      {
        "role": "system",
        "content": "You are a concise assistant."
      },
      {
        "role": "user",
        "content": "Explain OpenModels in one sentence."
      }
    ],
    "max_tokens": 256,
    "temperature": 0.7
  }'
```

## Python

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.getopenmodels.com/v1",
    api_key=os.environ["OM_API_KEY"],
)

response = client.chat.completions.create(
    model="qwen3.5-flash",
    messages=[
        {
            "role": "system",
            "content": "You are a concise assistant.",
        },
        {
            "role": "user",
            "content": "Explain OpenModels in one sentence.",
        },
    ],
    max_tokens=256,
    temperature=0.7,
    extra_body={
        "route": {"provider": "<provider-for-this-model>"},
        "route_mode": "balanced",
    },
)

print(response.choices[0].message.content)
```

## Node.js

```js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.getopenmodels.com/v1",
  apiKey: process.env.OM_API_KEY,
});

const response = await client.chat.completions.create({
  model: "qwen3.5-flash",
  route: { provider: "<provider-for-this-model>" },
  route_mode: "balanced",
  messages: [
    {
      role: "system",
      content: "You are a concise assistant.",
    },
    {
      role: "user",
      content: "Explain OpenModels in one sentence.",
    },
  ],
  max_tokens: 256,
  temperature: 0.7,
});

console.log(response.choices[0].message.content);
```

## 响应

响应遵循兼容 OpenAI 的聊天补全结构：

```json
{
  "id": "chatcmpl_123",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "OpenModels provides low-cost access to supported models through one OpenAI-compatible API key."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 20,
    "total_tokens": 38
  }
}
```

使用 `usage` 对象了解请求的 token 消耗。Credit spend 基于实际输入和输出 token 用量计算。