# Chat Completions

> `POST /api/v1/chat/completions`. OpenAI-compatible. Drop-in for any OpenAI SDK by changing the base URL to `https://khabot.com/api/v1`. `model` is a KhaBot model (`khabot/…`, covered by plans) or any OpenRouter model id, which is passed through and billed from credit at the provider's cost, see [Drop-in from OpenRouter](/docs/openrouter).

## Request

```
{
  "model": "khabot/glm-5.2",
  "messages": [
    {"role": "system", "content": "You are concise."},
    {"role": "user", "content": "Explain quotas in one line."}
  ],
  "max_tokens": 256,
  "stream": false
}
```

## Response

```
{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "model": "khabot/glm-5.2",
  "choices": [{"index": 0, "message": {"role": "assistant", "content": "..."}, "finish_reason": "stop"}],
  "usage": {"prompt_tokens": 24, "completion_tokens": 18, "total_tokens": 42, "cost": 0.000162, "cost_source": "plan"}
}
```

## Cost in the response

Responses from KhaBot models carry what the call cost you, so you don't need a second lookup:

| Field | Meaning |
| --- | --- |
| `usage.cost` | The amount billed for this call, in USD |
| `usage.cost_source` | `plan`, drawn from your plan quota; `credit`, charged to your credit balance (for example as extra usage) |

Pass-through models report `usage.cost` too, see [Drop-in from OpenRouter](/docs/openrouter).

## Streaming

Set `"stream": true` to receive OpenAI-style SSE chunks (`chat.completion.chunk`) ending with `data: [DONE]`. For KhaBot models the last chunk before `[DONE]` is a usage-only chunk, no choices, just the totals, cost included:

```
data: {"object":"chat.completion.chunk","model":"khabot/glm-5.2","choices":[{"index":0,"delta":{"content":"…"}}]}

data: {"object":"chat.completion.chunk","model":"khabot/glm-5.2","choices":[],"usage":{"prompt_tokens":24,"completion_tokens":18,"total_tokens":42,"cost":0.000162,"cost_source":"plan"}}

data: [DONE]
```

```
curl -N https://khabot.com/api/v1/chat/completions \
  -H "Authorization: Bearer kb_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"khabot/glm-5.2","messages":[{"role":"user","content":"hi"}],"stream":true}'
```

## Images

Models whose `input_modalities` include `"image"` (see [Models & pricing](/docs/models-pricing)) accept images in the request. Send them inline as base64:

```
{
  "model": "khabot/kimi-k2.6",
  "messages": [{
    "role": "user",
    "content": [
      { "type": "text", "text": "What is in this picture?" },
      { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBORw0KGgo…" } }
    ]
  }]
}
```

The same limits apply on both dialects, `/chat/completions` and `/messages`, and on every plan:

| Limit | Value |
| --- | --- |
| Images per request | 20 |
| Size of one image | 5 MiB, decoded |
| All images together | 16 MiB, decoded |
| Request body | 25 MB (26,214,400 bytes) of JSON, base64 included |

KhaBot doesn't check pixel dimensions; the model's own limits on image size still apply, so very large images may be refused or scaled down by the model.

A request over a limit is refused before it reaches the model: it isn't billed and doesn't use up your [rate limit](/docs/rate-limits).

| HTTP | code | When | `details` |
| --- | --- | --- | --- |
| 400 | `invalid_image` | An image can't be decoded or isn't a supported image | `index`, `reason` |
| 400 | `image_too_large` | One image is over 5 MiB decoded | `index`, `bytes`, `max_bytes` |
| 400 | `too_many_images` | More than 20 images | `count`, `max_images` |
| 400 | `images_too_large` | All images together are over 16 MiB decoded | `bytes`, `max_bytes` |
| 413 | `payload_too_large` | The request body is over 25 MB (26,214,400 bytes) | `max_body_bytes` |

`index` says which image: the images are counted in the order they appear in `messages`, starting at 0.

```
{ "error": { "code": "image_too_large", "message": "Image too large",
  "details": { "index": 2, "bytes": 6291456, "max_bytes": 5242880 } } }
```

Every response carries an `x-khabot-request-id` header, quote it when contacting support.
