Chat Completions

Read MarkdownFor agents

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.

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:

FieldMeaning
usage.costThe amount billed for this call, in USD
usage.cost_sourceplan, 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.

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) 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:

LimitValue
Images per request20
Size of one image5 MiB, decoded
All images together16 MiB, decoded
Request body25 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.

HTTPcodeWhendetails
400invalid_imageAn image can't be decoded or isn't a supported imageindex, reason
400image_too_largeOne image is over 5 MiB decodedindex, bytes, max_bytes
400too_many_imagesMore than 20 imagescount, max_images
400images_too_largeAll images together are over 16 MiB decodedbytes, max_bytes
413payload_too_largeThe 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.