Chat Completions API

Send messages to POST /v1/chat/completions and read the answer from choices.

POST /v1/chat/completions takes a list of messages and returns a completion in choices.

request
curl "$KEYDAN_API_BASE/v1/chat/completions" \
  -H "Authorization: Bearer $KEYDAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kd-balanced",
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Say hello in three languages."}
    ]
  }'
response
{
  "id": "chatcmpl-8f2c1d",
  "object": "chat.completion",
  "created": 1763553600,
  "model": "kd-balanced",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "Hello! Bonjour! Hola!"},
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 9,
    "total_tokens": 33,
    "prompt_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0},
    "cost": 0.000248
  }
}

Request body

Request body
FieldTypeNotes
modelstringrequired: a model id from the catalogue whose API styles include chat
messagesarrayrequired: system and developer (joined into the instructions, in order), user, assistant and tool messages; content is a string or a list of parts (text, image_url with an https or a data: URL of a JPEG, PNG, GIF or WebP image)
max_completion_tokens or max_tokensintegerthe most tokens the answer may have (default: the model's maximum)
temperature, top_pnumbersampling controls (temperature 0 to 2; values above 1 are served as 1)
response_formatobjecttext; json_object and json_schema are followed by instruction
stopstring or arraystrings that end the answer
stream, stream_optionsboolean, objectstream chunks; {"include_usage": true} adds a final usage chunk (streaming)
tools, tool_choice, parallel_tool_callsarray, string or object, booleanfunction calling (tools); parallel_tool_calls: false asks for at most one call
userstringyour end user's id (end users)

One choice per request. Refused with 400 unsupported_parameter: n above 1, logprobs, top_logprobs and audio output; with 400 unsupported_content: audio and file parts. Ignored: seed, frequency_penalty, presence_penalty, logit_bias, store, metadata, service_tier, reasoning_effort.

images, stop, end user
curl "$KEYDAN_API_BASE/v1/chat/completions" \
  -H "Authorization: Bearer $KEYDAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kd-balanced",
    "max_tokens": 1024,
    "temperature": 0.2,
    "user": "user-7f3a",
    "messages": [
      {"role": "system", "content": "You describe images briefly."},
      {"role": "user", "content": [
        {"type": "text", "text": "What does this chart show?"},
        {"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgo..."}}
      ]}
    ],
    "stop": ["END"]
  }'

Response

  • choices[0].message: the answer, with content and, when the model calls tools, tool_calls.
  • choices[0].finish_reason: stop, length, tool_calls or content_filter.
  • usage: prompt_tokens (input including cache reads and writes, detailed in prompt_tokens_details), completion_tokens, total_tokens, and cost: the price in USD as debited (0 when a subscription window or a business plan covered the request), and, when part of the input was read from the cache, cost_details.cache_savings: what the cache saved against the input price, in USD.
  • id: a unique id of this completion (chatcmpl-…). To quote a request to support, use the x-request-id header.
  • model: the canonical id of the model that answered (the same id when you sent one of its other names).

Headers: x-request-id, the limit headers, and for subscriptions the window headers.

Fallback models

Send models, a list of up to three more model ids. When the first model has no capacity at the moment, the request goes to the next one; the answer's model, the request log and the price name the model that actually answered. A rejected request (for example invalid parameters) is not retried on another model.

fallback
{
  "model": "kd-max",
  "models": ["kd-balanced", "kd-fast"],
  "messages": [{"role": "user", "content": "Hello!"}]
}

When a fallback model answered, the answer carries a fallback object and two headers: x-fallback-from (the model you asked for) and x-fallback-attempts (how many models were tried). A stream gets the headers only. When the first model answered, none of these are present.

fallback answer
"model": "kd-balanced",
"fallback": {
  "requested_model": "kd-max",
  "served_model": "kd-balanced",
  "attempts": 2,
  "reason": "model_unavailable"
}

Automatic model choice: kd-auto

Send "model": "kd-auto" and the model is chosen for each request. A light request goes to a fast model, a heavy one to the strongest; what counts: the size of the context, tools, images, the answer length you ask for (max_tokens), reasoning (reasoning_effort or thinking) and the task in the last message (code, analysis, refactoring and the like). If the chosen model has no capacity, the request goes to a stronger one, then to a lighter one. You pay the price of the model that answered. Within one conversation the tier never drops (every user turn counts), so a model switch does not throw away the prompt cache. A tier whose catalogue entry lacks what the request uses (images, tools, reasoning) is skipped.

kd-auto
{
  "model": "kd-auto",
  "messages": [{"role": "user", "content": "Refactor this function and explain the changes"}]
}

The answer's model names the model that answered, and a route object says why it was chosen; a stream gets the x-route-model and x-route-tier headers instead. You may add your own models after kd-auto: they are tried after the automatic choice. In the Messages API, kd-auto takes the first available model of the chosen tier. GET /v1/models lists kd-auto first, so coding agents and chat apps that read the list offer it too.

kd-auto answer
"model": "kd-balanced",
"route": {
  "requested_model": "kd-auto",
  "model": "kd-balanced",
  "tier": "balanced",
  "reasons": ["hard_task", "code"]
}