# Chat Completions (OpenAI schema)

> POST /v1/chat/completions -- request body incl. vision content parts, SSE streaming format, usage reporting, and common errors.

`POST https://<platform-address>/v1/chat/completions`. Your platform's real
endpoint and model ids are on the APIs page (`/apis`).

## Request headers

```
Content-Type: application/json
Authorization: Bearer onprem-gw-your-key
```

The `anthropic-version` header has no effect on this route. `/v1/chat/completions`
is always parsed as an OpenAI chat completion.

## Request body

```json
{
  "model": "my-model-id",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Hello!" }
  ],
  "stream": true,
  "max_tokens": 1024,
  "temperature": 0.7,
  "stream_options": { "include_usage": true }
}
```

| Parameter | Type | Required | Description |
|---|---|---|---|
| `model` | string | yes | Model id as listed by `/v1/models` (or a configured alias) |
| `messages` | array | yes | Conversation messages; `content` is a string or an array of content parts (see Vision) |
| `stream` | boolean | no (false) | Enable SSE streaming |
| `max_tokens` | integer | no | Max output tokens; the legacy alias `max_completion_tokens` is also read |
| `temperature` | number | no | Sampling temperature |
| `top_p` | number | no | Nucleus sampling |
| `stop` | array of strings | no | Stop sequences |
| `presence_penalty` / `frequency_penalty` | number | no | Repetition penalties |
| `tools` / `tool_choice` | array / object | no | OpenAI-style function tools |
| `stream_options.include_usage` | boolean | no | The gateway sets this to `true` on every streaming request anyway (see Streaming) |

Notes on gateway behavior (verified in the gateway source):

- **Omitted `max_tokens`:** the gateway injects the model's configured
  default cap into the request (clamped to the remaining context window), so
  the engine always enforces a real output bound. Client-declared values are
  never modified.
- **Native passthrough:** for engines that speak OpenAI natively (vLLM),
  the request body is forwarded as-is apart from the model-id rewrite and
  the injections above -- additional OpenAI-compatible parameters supported
  by the engine pass through unvalidated.
- Request bodies are limited to 10 MB.

## Vision (image input)

For multimodal models, `content` becomes an array of typed parts:

```json
{
  "model": "my-vision-model",
  "messages": [{
    "role": "user",
    "content": [
      { "type": "text", "text": "What is in this image?" },
      { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBOR..." } }
    ]
  }],
  "stream": true,
  "max_tokens": 1024
}
```

| Part type | Fields |
|---|---|
| `text` | `text: string` |
| `image_url` | `image_url: { url: string }` -- typically a `data:` URI with base64 image data |

Multiple `image_url` parts per message are allowed. Whether a given model
accepts images depends on the model (see its catalog card / the APIs page).

## Non-streaming response

With `"stream": false`, the engine's chat completion object is returned
directly:

```json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "model": "my-model-id",
  "choices": [{
    "index": 0,
    "message": { "role": "assistant", "content": "Hello! How can I help?" },
    "finish_reason": "stop"
  }],
  "usage": { "prompt_tokens": 13, "completion_tokens": 9, "total_tokens": 15 }
}
```

## Streaming response (SSE)

With `"stream": true` the response is `text/event-stream`. The gateway
injects `stream_options: {"include_usage": true}` into every streaming
request, so the stream always ends with a usage chunk before `[DONE]`:

```
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"}}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"!"}}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15}}

data: [DONE]
```

Read each `data:` line as JSON:

- `choices[0].delta.content` -- incremental text
- `choices[0].finish_reason` -- `"stop"` (or `"length"`, `"tool_calls"`) on the final content chunk
- `usage` -- exact token counts in the final chunk

If the stream fails mid-flight (after headers are sent), the gateway emits
the error envelope as a `data:` event instead of silently closing. If the
model produces no first token within its deadline, no stream starts at all
-- you get a clean 503 (see below).

## Common errors

All use the envelope described in [API Overview](/en/platform/docs/v5.10/guides/inference-api/api-overview/index.md). The ones you will actually
hit:

| HTTP | `type` / `code` | Cause |
|---|---|---|
| 401 | `invalid_request_error` / `authentication_error` | Missing or invalid `Authorization: Bearer` header |
| 404 | `ModelNotFound` | `model` value is not a deployed model id or alias |
| 400 | `Parse` | Body is not valid JSON |
| 502 | `Upstream` | Engine error; `upstream_status` / `upstream_body` carry the engine's real response |
| 503 | `Overloaded` | Load shedding; `code` is one of `concurrency_budget_exceeded`, `ttft_deadline_exceeded`, `kv_cache_utilization_exceeded`; `Retry-After: 2` header set |

Example 503:

```json
{
  "error": {
    "message": "Service overloaded -- model 'my-model-id' produced no first token within 8000ms. Please retry later.",
    "type": "Overloaded",
    "code": "ttft_deadline_exceeded"
  }
}
```

Overload rejections are 503 (not 429). Honor `Retry-After` and back off.
