Chat completions

POST /v1/chat/completions follows the OpenAI Chat Completions format, streamed or not.

Parameters

ParameterNotes
modelRequired. A BaseURL model name, for example gateway-fast.
messagesRequired. A non-empty array of messages, each with a role.
max_tokens / max_completion_tokensThe most tokens the answer may use. Set it: it also sets how much balance is held while the request runs. Without it, each model has a default cap, lowered to what your balance covers if needed.
streamtrue streams the answer as server-sent events. The last chunk carries usage.
temperature, tools and other OpenAI parametersPassed to the model unchanged. If the model does not accept one, you get its 400 error and are not charged.
nMust be 1. Several choices per request are not supported.

Streaming

stream = client.chat.completions.create(
    model="gateway-fast",
    messages=[{"role": "user", "content": "Write a haiku about Lagos traffic."}],
    max_tokens=200,
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Each event is an OpenAI-style chat.completion.chunk. The stream ends with a chunk holding usage and then data: [DONE]. If you disconnect early, you pay only for what was generated.

Response headers

HeaderMeaning
X-Request-IdThe request id. Include it when you contact support.
X-Xllm-Cost-UnitsWhat the request cost, in USD micro-units (1,000,000 = $1.00). Non-streamed responses only; for streams, see your usage page.
X-Xllm-Max-Tokens-AppliedPresent when BaseURL set or lowered max_tokens for you, with the value used.

Safe retries with Idempotency-Key

Send Idempotency-Key: <unique id> to retry a request safely after a network error. Within 24 hours, the same key with the same body replays the stored answer of a completed non-streamed request without charging again.

SituationAnswer
Same key, same body, request still running409 idempotency_key_in_progress, retry after Retry-After
Same key, same body, completed stream409 idempotency_key_replay; streams are not replayed
Same key, different body422 idempotency_key_reused
The first request was refused or charged nothingThe key is freed: retry normally

Questions? Email [email protected] with your X-Request-Id.