Skip to main content

Chat Completions API

The Chat Completions API is TokenFlux’s primary interface for multi-turn conversations. TokenFlux accepts canonical model IDs, resolves them to the best upstream provider, and streams responses using the same wire format as the OpenAI Chat Completions API.
All request and response payloads follow openai.ChatCompletion semantics. You can point any OpenAI-compatible SDK at the TokenFlux base URL and continue to use familiar request shapes, including tools, images, and reasoning tokens.

Create chat completion

Endpoint

Authentication

Include an API credential using either header. API keys are validated before the request is proxied to a provider.
or
Requests from users whose remaining quota is below 0.01 credits are rejected with 403 Forbidden and an insufficient quota error.

Request body

TokenFlux accepts the same JSON schema as OpenAI’s chat.completions.create. The most important fields are:

Message content

Message payloads accept the same typed segments as OpenAI, including structured arrays such as:

Response (non-streaming)

Non-streaming responses are identical to OpenAI’s chat.completion object. TokenFlux injects the provider’s server-side model ID into the model field (for example gpt-4o) and appends aggregate usage data before returning the payload.

Response (streaming)

When stream: true, TokenFlux returns Server-Sent Events (Content-Type: text/event-stream). Each data: line contains an OpenAI chat.completion.chunk JSON payload. TokenFlux automatically requests usage information from providers, so the final chunk includes a usage object alongside the delta, followed by data: [DONE].
Errors during streaming are emitted as SSE messages in the form data: {"error":{"message":"..."}} before the stream closes.

Error handling

Examples

cURL (non-streaming)

cURL (streaming)

JavaScript (OpenAI SDK)

Streaming in Node.js

Operational notes

  • TokenFlux retries alternate providers transparently when an upstream returns a retriable error, up to three attempts per request. You receive the first successful response without extra coordination.
  • Usage for both streaming and non-streaming completions is persisted immediately after completion so that billing dashboards and quota enforcement stay in sync.
  • Pair this API with List models to dynamically select models and their supported parameters at runtime.