Skip to content

Chat Completions

POST /v1/chat/completions: the chat endpoint in the OpenAI format, with streaming. GPT, Claude, Gemini and Grok models all work with it.

Chat Completions is the most commonly used endpoint: you send a list of conversation messages (messages) and the model returns the next reply. Requests and responses follow the official OpenAI format. Besides OpenAI's GPT models, Claude, Gemini and Grok models can also be called through this endpoint, and the gateway converts the request to the upstream format. Which models you can use depends on your key's group, and Model Square is the source of truth.

POST https://codaapi.com/v1/chat/completions

Request headers

Name Required Description
Authorization Yes Bearer sk-…, your API key
Content-Type Yes application/json

Request body

Parameter Type Required Description
model string Yes Model name, such as gpt-5.4-mini
messages array Yes Conversation messages, in chronological order
messages[].role string Yes system, developer, user, assistant or tool
messages[].content string or array Yes Message content. To send images or PDFs, use an array of content parts; see Vision and PDF files
stream boolean No When true, the response is streamed as SSE; see Streaming responses
stream_options.include_usage boolean No Whether a streamed response ends with usage. The gateway includes it by default; pass false to turn it off
max_completion_tokens integer No The most tokens to generate, including a reasoning model's reasoning tokens. The older max_tokens is also accepted
temperature number No Sampling temperature. Some reasoning models accept only the default
top_p number No Nucleus sampling
stop string or array No Stop sequences; generation stops when it produces one of them
reasoning_effort string No Reasoning effort, such as low, medium or high; the allowed values depend on the model
tools, tool_choice array, string or object No Function calling; see Tools and structured output
response_format object No Structured output; same page

The table lists only the common parameters. Other parameters from the official OpenAI docs (such as n, seed and logprobs) are also passed to OpenAI-format upstreams, while the gateway doesn't forward service_tier or safety_identifier by default. For Claude and Gemini, the gateway rewrites the parameters that have an equivalent in the upstream format and ignores the rest; see Claude via the OpenAI format and Gemini via the OpenAI format. Whether a parameter takes effect in the end depends on the model and the upstream.

Some models support only the Responses endpoint upstream. If a model errors with Chat Completions and says it doesn't support this endpoint, use Responses instead.

Request examples

curl

curl https://codaapi.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-mini",
    "messages": [
      {"role": "system", "content": "Answer in one sentence."},
      {"role": "user", "content": "What is an API gateway?"}
    ]
  }'

Python (pip install openai)

from openai import OpenAI

# With no arguments, the client reads OPENAI_BASE_URL and OPENAI_API_KEY
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[
        {"role": "system", "content": "Answer in one sentence."},
        {"role": "user", "content": "What is an API gateway?"},
    ],
)
print(response.choices[0].message.content)

Node.js (npm install openai)

import OpenAI from 'openai';

// With no arguments, the client reads OPENAI_BASE_URL and OPENAI_API_KEY
const client = new OpenAI();

const completion = await client.chat.completions.create({
  model: 'gpt-5.4-mini',
  messages: [
    { role: 'system', content: 'Answer in one sentence.' },
    { role: 'user', content: 'What is an API gateway?' },
  ],
});
console.log(completion.choices[0].message.content);

For a multi-turn conversation, put the earlier user messages and model replies (role: assistant) into messages in order. The gateway doesn't store conversations, so every request has to carry the full context.

Response

{
  "id": "chatcmpl-…",
  "object": "chat.completion",
  "created": 1764460800,
  "model": "gpt-5.4-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "An API gateway is the single entry point for API requests, handling authentication, routing and billing."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 21,
    "total_tokens": 46,
    "prompt_tokens_details": { "cached_tokens": 0 },
    "completion_tokens_details": { "reasoning_tokens": 0 }
  }
}
Field Description
choices[].message.content The model's reply
choices[].message.tool_calls Functions the model wants to call; see function calling
choices[].finish_reason Why generation stopped: stop finished normally, length hit the length limit, tool_calls wants to call a function, content_filter was blocked by moderation
usage.prompt_tokens Input tokens
usage.completion_tokens Output tokens, including reasoning tokens
usage.prompt_tokens_details.cached_tokens The part of the input served from the prompt cache
usage.completion_tokens_details.reasoning_tokens A reasoning model's reasoning tokens

Claude and Gemini models return this format too; the gateway converts it.

Streaming responses

Add "stream": true to the request and the response Content-Type is text/event-stream. Each event is a data: {…} line, the content increments are in choices[0].delta.content, and the last line is data: [DONE].

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1764460800,"model":"gpt-5.4-mini","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1764460800,"model":"gpt-5.4-mini","choices":[{"index":0,"delta":{"content":"An API gateway"},"finish_reason":null}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1764460800,"model":"gpt-5.4-mini","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1764460800,"model":"gpt-5.4-mini","choices":[],"usage":{"prompt_tokens":25,"completion_tokens":21,"total_tokens":46}}

data: [DONE]

Even when the request has no stream_options, the gateway sends one extra chunk with usage before [DONE] by default. Its choices is an empty array, so check it before reading choices[0]. If you don't want this chunk, pass "stream_options": {"include_usage": false}.

Python

stream = client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[{"role": "user", "content": "Write a four-line poem"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Node.js

const stream = await client.chat.completions.create({
  model: 'gpt-5.4-mini',
  messages: [{ role: 'user', content: 'Write a four-line poem' }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}

For long answers and reasoning models, the first token can take a while to arrive, so don't set your client's read timeout too short.

Errors

Errors come back with a 4xx or 5xx status code and an OpenAI-style error body, {"error": {"message": "…", "type": "…", "code": "…"}}, where message ends with the ID of the request (the request id). For common status codes and what to do about them, see Error codes.

Billing

Most models are billed by input and output tokens, and reasoning tokens count as output; models marked Per Request or Dynamic Pricing in Model Square follow the matching rules in Pricing and multipliers. For each model's price, see Model Square and the price list; for the rules, see Pricing and multipliers. The token counts and charge of every request are recorded in Usage Logs in the console (open it directly).

Updated