Skip to content

Chat completions

Creates a chat completion for the given conversation messages.

Send requests to POST /v1/chat/completions on the API host (production: https://api.rikaii.com). Paths are relative to that host unless your integration uses a custom base URL.

Request and response bodies follow the OpenAI Chat Completions JSON shape so official OpenAI SDKs can often be pointed at Rikaii with minimal configuration—same top-level fields (model, messages, temperature, max_tokens, stream) and the familiar completion object on success.

Note on Models

Rikaii accepts the model parameter to maintain 100% compatibility with official OpenAI SDKs. However, Rikaii's Smart Routing Engine intercepts each request and dynamically routes it to the optimal provider based on your Dashboard configuration—routing rules, fallback behavior, and keyword triggers—not solely on the model string in the payload.

Endpoint

http
POST /v1/chat/completions

Example URL: https://api.rikaii.com/v1/chat/completions

Headers

HeaderRequiredDescription
AuthorizationYesBearer <api-key> — see Authentication
Content-TypeYesapplication/json

Request body

FieldJSON keyTypeRequiredDescription
modelmodelstringNoAccepted for OpenAI SDK compatibility; routing follows your dashboard rules (see note above).
messagesmessagesarrayYesConversation turns (see Messages below).
temperaturetemperaturenumberNoSampling temperature.
max_tokensmax_tokensintegerNoMaximum tokens to generate in the reply.
streamstreambooleanNotrue streams the completion; omit or false for a single JSON response.

Parameters & forwarding (OpenAI-compatible)

When you include temperature, max_tokens, or stream in the JSON body, Rikaii forwards them on upstream provider calls (OpenAI-shaped payloads use the same field names). Streaming requests set stream: true (and related streaming options where the upstream API requires them). Provider adapters that use different schemas—such as Anthropic’s Messages API—map these knobs to the equivalent upstream parameters (temperature, max_tokens, stream) rather than dropping them.

Messages

Each item in messages is an object with:

FieldTypeDescription
rolestringTypically system, user, or assistant.
contentstringMessage text for that turn.

Advanced OpenAI features such as tool calling may use additional fields in OpenAI’s own API; only role and content are documented here as the portable subset for Rikaii chat requests.

Request schema (reference)

json
{
  "type": "object",
  "properties": {
    "model": { "type": "string" },
    "messages": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "role": { "type": "string" },
          "content": { "type": "string" }
        },
        "required": ["role", "content"]
      }
    },
    "temperature": { "type": "number" },
    "max_tokens": { "type": "integer" },
    "stream": { "type": "boolean" }
  }
}

Example payload

json
{
  "model": "gpt-4o-mini",
  "messages": [
    { "role": "system", "content": "You are a concise assistant." },
    { "role": "user", "content": "Give two benefits of using a unified LLM API." }
  ],
  "max_tokens": 256,
  "temperature": 0.2
}

Response

On success, 200 OK with application/json in the shape of an OpenAI chat completion:

FieldJSON keyDescription
ididCompletion identifier.
objectobjectObject type (e.g. chat.completion).
createdcreatedUnix timestamp.
modelmodelModel identifier returned for this completion.
choiceschoicesIncludes message, finish_reason, etc.
usageusageprompt_tokens, completion_tokens, total_tokens.

Responses may include extra fields from upstream providers; treat unknown keys as optional.

Example response

json
{
  "id": "chatcmpl_…",
  "object": "chat.completion",
  "created": 1735689600,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "…"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 42,
    "completion_tokens": 28,
    "total_tokens": 70
  }
}

X-Rikaii-Fallback

When a fallback route is used to fulfill the request, the response may include:

http
X-Rikaii-Fallback: true

Streaming

Set stream to true to receive a text/event-stream (SSE) response. Parse events using your HTTP client; chunks follow the usual streaming chat-completion pattern.

Examples

bash
curl -sS "https://api.rikaii.com/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "user", "content": "Ping"}
    ],
    "max_tokens": 16
  }'
javascript
const res = await fetch('https://api.rikaii.com/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: 'Ping' }],
    max_tokens: 16,
  }),
})

if (!res.ok) {
  const err = await res.text()
  throw new Error(`HTTP ${res.status}: ${err}`)
}

const data = await res.json()
console.log(data.choices[0]?.message?.content)

Rikaii API documentation