Chat Completion API

Chat Completion

This QAnswer endpoint is an OpenAI Compatible chat completion.

See the OpenAI documentation for the full request/response schema.

An API key is required to use this endpoint:

  • Create one on your Profile Page
  • Pass the API key as a Bearer token in the Authorization header.

An administrator must allocate credits to a user. New API keys have 0 credits by default.

Payload Example

POST

/api/v1/{llm_id}/chat/completions


Try it
Code

Request parameters

The endpoint reads the request schema of the current OpenAI API. Points worth knowing:

  • max_completion_tokens caps the answer, reasoning included. The legacy max_tokens is still read when max_completion_tokens is absent. With neither, the model's Max Output Tokens setting applies. Neither may exceed the model's context window.
  • reasoning_effort takes none, minimal, low, medium, high or xhigh, and max on the models that have it. minimal is sent to the model as low. Without it, the model's own reasoning effort setting applies.
  • A developer message reaches the model as a system message.
  • The model's reasoning is left out of the response. To get it, send include_reasoning: true (a QAnswer extension): it then comes in message.reasoning_content, or delta.reasoning_content when streaming.
  • Without temperature, the model's own default applies.
  • n above 1 is refused with a 400 when the model can only return one choice (Anthropic models, for example).
  • An error comes with OpenAI's error object: error.message, error.type, error.param and error.code, the QAnswer error key.

Token usage

Every response reports the tokens it used in usage: prompt_tokens, completion_tokens and total_tokens. When the model's provider reports a breakdown, QAnswer passes it on:

  • prompt_tokens_details.cached_tokens: prompt tokens served from the provider's prompt cache.
  • completion_tokens_details.reasoning_tokens: tokens a reasoning model spent before writing its answer.
  • cache_creation_input_tokens and cache_read_input_tokens: the cache counters of Anthropic models, for tokens written to and read from the cache. Each appears only when it is not 0.
"usage": {
  "prompt_tokens": 1250,
  "completion_tokens": 310,
  "total_tokens": 1560,
  "prompt_tokens_details": { "cached_tokens": 1024 },
  "completion_tokens_details": { "reasoning_tokens": 192 }
}

The breakdown fields appear only when the provider reports them: check that a field exists before reading it.

A streamed response reports usage only when the request sets stream_options.include_usage to true: it then comes in a last chunk, whose choices list is empty.

To price cached tokens separately from other prompt tokens, set the model's Cached Input Token Cost, see LLM Providers.