Skip to content

Chat completions API

Call chat models with POST /v1/chat/completions in the OpenAI Chat Completions format. Stream replies, billed per input and output token.

Send POST /v1/chat/completions with your API key. Requests and responses use the OpenAI Chat Completions format. List the available models with GET /v1/models and pass a chat model's ID as model.

curl --fail --show-error https://api.sawtakarabi.ai/v1/chat/completions \
  -H "Authorization: Bearer $SAWTAK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<MODEL_ID>",
    "messages": [{"role": "user", "content": "مرحباً"}]
  }'

The OpenAI SDKs work when you set the base URL:

import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.sawtakarabi.ai/v1",
    api_key=os.environ["SAWTAK_API_KEY"],
)
reply = client.chat.completions.create(
    model="<MODEL_ID>",
    messages=[{"role": "user", "content": "مرحباً"}],
)
print(reply.choices[0].message.content)

Streaming

Set "stream": true to receive the reply as server-sent events. The stream ends with data: [DONE].

Add "stream_options": {"include_usage": true} to receive a final chunk with usage and an empty choices list.

Billing

Each request is charged for its input and output tokens at the model's prices per 1M tokens, rounded up to the nearest micro-dollar.

  • A request is charged only once the reply, or any part of a stream, reaches you.
  • If you stop a stream early, you pay for the tokens generated up to that point.
  • If the model rejects your input as invalid (a 4xx error), the request isn't charged.

If max_tokens or max_completion_tokens is larger than your balance covers at the model's output price, it is lowered to what your balance covers.

Limits

Each request has a 10-minute deadline and a request body of up to 4 MiB. Requests are subject to your account's concurrency limit.

Errors

Statuserror.codeWhat to do
400model_kind_mismatchThe model isn't a chat model. Choose one from GET /v1/models.
402insufficient_balanceAdd credit in Billing.
404model_not_foundCheck the model ID.
429concurrency_limitedWait for one of your requests to finish, then retry.
429capacity_fullRetry with backoff.
502backend_errorThe model couldn't complete the request. Retry with backoff.
503no_backendCapacity is starting. Retry shortly.

See Errors for the error format.