Docs menu

Docs / API

Chat Completions

POST /v1/chat/completions in the OpenAI format, with streaming, tools and JSON mode.

Last updated


Your setup

Fills in YOUR_API_KEY in the code on this page. The key stays in this tab: it is never sent and never saved.

Endpoint

POST https://api.cent-ai.dev/v1/chat/completions
Authorization: Bearer cai_<secret>
Content-Type: application/json

The endpoint follows the OpenAI Chat Completions format. Point any OpenAI SDK or tool at the base URL https://api.cent-ai.dev/v1.

Request

FieldRequiredWhat Cent AI does with it
modelyesPicks the model and its prices. An unknown name returns 404 model_not_found
messagesyesSent to the model as is
streamnotrue returns server-sent events, see Long streaming
stream_options.include_usagenotrue adds the final usage chunk, with usage.cost, to the stream
max_tokens, max_completion_tokensnoSent as is
tools, tool_choice, response_format, reasoning_effort and othersnoSent as is

Parameters are passed to the model unchanged. A parameter the model does not support comes back as the model's error, with the HTTP status it returned.

Example

curl https://api.cent-ai.dev/v1/chat/completions \
  -H "Authorization: Bearer $CENT_AI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"glm-5.3","messages":[{"role":"user","content":"Hello"}]}'

Response

{
  "id": "chatcmpl-9f2c1a",
  "object": "chat.completion",
  "created": 1792051200,
  "model": "glm-5.3",
  "choices": [{
    "index": 0,
    "message": {"role": "assistant", "content": "Hello!"},
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 10000,
    "prompt_tokens_details": {"cached_tokens": 9000},
    "completion_tokens": 800,
    "total_tokens": 10800,
    "cost": "0.000363000"
  }
}

prompt_tokens includes cached tokens; cached_tokens is the part read from the cache and charged at the cache read rate. usage.cost is the amount charged, see Request cost.

Streaming

With stream: true, the response is text/event-stream. Chunks follow the OpenAI format and the stream ends with data: [DONE]:

: keepalive

data: {"id":"chatcmpl-9f2c1a","object":"chat.completion.chunk","created":1792051200,"model":"glm-5.3","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-9f2c1a","object":"chat.completion.chunk","created":1792051200,"model":"glm-5.3","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-9f2c1a","object":"chat.completion.chunk","created":1792051200,"model":"glm-5.3","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-9f2c1a","object":"chat.completion.chunk","created":1792051200,"model":"glm-5.3","choices":[],"usage":{"prompt_tokens":10000,"prompt_tokens_details":{"cached_tokens":9000},"completion_tokens":800,"total_tokens":10800,"cost":"0.000363000"}}

data: [DONE]

The usage chunk is sent only when stream_options.include_usage is true.

Status codes

StatusWhen
200Success, streamed or not
400The request body is not valid (invalid_request)
401, 402, 403, 404, 413, 429See 4xx errors
503The model failed before the first byte was sent, see 5xx errors

Contract


POST/v1/chat/completions

Chat completions (OpenAI format)

Auth · API key (Bearer or x-api-key)

Request body

  • max_completion_tokensintegeroptional

  • max_tokensintegeroptional

  • messagesobject[]required

  • modelstringrequired

  • streambooleanoptional

  • stream_optionsobjectoptional

  • stream_options.include_usagebooleanoptional

    Sends the final usage chunk, with usage.cost, to the client.

Responses

  • 200Completion, or an SSE stream of chat.completion.chunk frames ending with data: [DONE].
  • 400Error in the OpenAI shape.
  • 401Error in the OpenAI shape.
  • 402Error in the OpenAI shape.
  • 403Error in the OpenAI shape.
  • 404Error in the OpenAI shape.
  • 413Error in the OpenAI shape.
  • 429Error in the OpenAI shape that may be retried after Retry-After.retry
  • 500Error in the OpenAI shape.
  • 503Error in the OpenAI shape that may be retried after Retry-After.retry

Response headers

Retry-Afterx-ratelimit-limitx-ratelimit-remainingx-ratelimit-resetx-request-id

Generated from the OpenAPI contract.