Overview

The Chat Completions API is the primary gateway surface for sending multi-turn messages to language models. It accepts a standard chat-completions request body, routes the request through the Allternit model selector or to an explicit provider/model pair, and returns a completion in a familiar shape with usage metadata. The gateway supports non-streaming and streaming responses, tool-call deltas, structured JSON outputs, reasoning controls, multimodal inputs, session reuse, automatic cross-provider failover, and prompt caching.

Base URL

For local development:

Endpoints

Authentication

All gateway requests use a virtual key in the Authorization header. Keys are created through the Keys API and have the prefix ak-.

Create a chat completion

Request body

Message object

Content parts

A message content array may contain:

Example request

Example response

Response fields

Model selection

The model field supports three forms:
  1. Policy aliasauto, allternit-balanced, allternit-code, allternit-reasoning, allternit-knowledge, or allternit-instruct. The gateway scores connected providers against benchmark weights and picks a winner plus a cross-provider fallback chain.
  2. Explicit provider/model — e.g. provider/model. The gateway uses that pair as the primary and still derives a fallback chain from the balanced scorecard.
  3. Bare model id — The gateway looks the model up in the connected catalog and selects the first provider that offers it.
The resolved fallback chain is sent to the runtime and is used automatically on retryable provider failures.

Streaming

Set stream: true to receive server-sent events. The stream emits a role chunk, content deltas, optional tool-call deltas, a finish chunk, and (when stream_options.include_usage is true) a final usage chunk followed by [DONE].

Tool calling

Tool definitions are validated but execution happens inside the Allternit runtime. The final assistant message may include tool_calls describing the requested tool invocations.

Structured outputs

Enforce a JSON schema on the response:

Reasoning mode

Use reasoning_effort to request extended thinking:

Multimodal inputs

Images and videos are accepted as content parts:

Session reuse

Pass the header x-allternit-session-id from a previous completion to continue the same runtime session. Only the last user message is forwarded; the runtime supplies the historical context.

Idempotency

Include an Idempotency-Key header (1–255 ASCII characters) on non-streaming requests. The gateway stores the response body and replays it for identical keys. Keys are scoped to the virtual key and ignored for streaming requests.

Token and cost estimation

POST /v1/tokens returns a token count and cost estimate without invoking a model.

Error codes