codai docs
API referenceGateway

Chat & Completions

The three wire formats that reach `codai`: OpenAI Chat Completions, Anthropic Messages, OpenAI Responses.

All three endpoints route to the same model chain and share auth, rate limits, prompt caching and usage recording. Pick the one your SDK already speaks — the wire formats guide explains the differences and the headers guide covers every x-codai-* extension you see below.

POST
/v1/chat/completions

Authorization

bearerAuth
headerAuthorizationBearer <codai_…>

A codai API key (codai_ prefix). Ephemeral tokens from POST /v1/tokens are accepted only by the media surfaces matching their scope (/v1/realtime, /v1/audio/*, /v1/embeddings).

Header Parameters

x-request-id?string

Client correlation id; echoed back as x-codai-trace-id and persisted on the usage row.

Lengthlength <= 128
x-codai-session-id?string

Groups requests of one conversation for receipts, stickiness and prompt caching.

Lengthlength <= 128
x-codai-effort?string

Test-time-compute effort tier. Wins over the reasoning_effort body field. max is a codai extension above OpenAI's high; none is accepted as an alias of minimal.

Value in"minimal""low""medium""high""max"
x-codai-thinking?string

1/true enables extended thinking on models that support it (budget from x-codai-thinking-budget, default 16384 tokens).

x-codai-thinking-budget?string

Positive integer thinking-token budget used when x-codai-thinking is on. Also marks the request as caller-tuned so the effort planner does not override it.

x-codai-thinking-pin?string

1 keeps the full thinking budget on tool-continuation turns instead of the dampened budget the gateway applies by default.

x-codai-cache?string

0/false disables prompt caching for a DIRECT model id. Ignored (caching forced ON) for the codai / codai-* router aliases.

x-codai-no-task?string

1/true opts this request out of task counting. Only valid on utility aliases; on codai the request is rejected with 400.

x-codai-task-id?string

Client-chosen task correlation id. Requests sharing it are attributed to one task; echoed back as the x-codai-task-id response header when known within 150 ms.

x-codai-task-outcome?string

pass or fail — self-reported outcome that closes the attached task. Honoured only for desktop/CLI surfaces (x-codai-client) and only together with a machine x-codai-task-evidence.

Value in"pass""fail"
x-codai-task-evidence?string

Evidence kind backing x-codai-task-outcome; only exec_verdict and reexec close a task.

Value in"exec_verdict""reexec"
x-codai-client?string

Explicit client surface tag used for task attribution (desktop/<ver>, phone-android/<ver>, codai-cli/<ver>, hub/<ver>). Anything else is classified as proxy.

x-codai-incognito?string

1 makes the turn fully stateless: no memory read, nothing persisted, no sticky routing.

x-codai-no-recall?string

1 skips reading prior memory but still persists this turn.

x-codai-proven-only?string

1 restricts memory recall to entries promoted to proven.

x-codai-new-session?string

1 rotates to a fresh server-minted session namespace for this request, ignoring x-codai-session-id.

x-codai-repo?string

Repository scope hint (e.g. owner/name) used to select the session playbook; most IDE proxies cannot send it, so the gateway also infers scope from the transcript.

x-codai-agent-id?string

Caller-supplied agent identifier persisted with the usage event for per-agent analytics.

x-codai-disable-subagents?string

1 forbids delegate_to_model sub-agent fan-out and forces the native streaming path.

x-codai-mode?"agent"

agent requests agent mode (server-side agent prompt + server tools). 403 when the key/plan is not entitled.

Value in"agent"
x-codai-server-tools?string

1/true enables gateway-side tools without full agent mode (agent mode implies it).

x-codai-orchestrate?string

1/true opts into multi-model orchestration (fan-out to sub-tasks). Requires at least two models in the key allowlist.

x-codai-cascade?string

verify opts into the FrugalGPT cascade (cheap draft, frontier verify). Result reported in x-codai-cascade-status.

x-codai-best-of?string

Best-of-N sampling count (e.g. 3); 0/off explicitly disables the automatic best-of. Result in x-codai-best-of-status.

x-codai-best-of-depth?string

Judge depth for best-of-N.

x-codai-reflect?string

Opt-in/out of the self-verification reflect pass (1/0). Result in x-codai-reflect-status.

x-codai-step-verify?string

Opt-in/out of step-level process verification (1/0). Result in x-codai-step-verify-status.

x-codai-plan?string

Opt-in/out of the long-horizon plan check (1/0). Result in x-codai-plan-status.

x-codai-consensus?string

1 runs iterative consensus across all tiers (non-stream, router alias only). Result in x-codai-consensus-status.

x-codai-compact?string

Opt-in server-side transcript compaction (1). Savings reported in x-codai-compacted / x-codai-compact-saved-chars.

x-codai-retrieval?string

1/on enables relevance retrieval over the transcript before dispatch (x-codai-retrieval-reduced).

x-codai-heuristics?string

Opt-in/out of the learned-heuristics prompt injection (1/0).

x-codai-identity?string

Identity-preamble mode override (on / off / shadow).

x-codai-playbook?string

Per-request playbook mode override; x-codai-no-playbook: 1 is the hard kill-switch.

x-codai-no-playbook?string

1 disables session playbook injection for this request.

x-codai-debug?string

1 adds the full router decision trace as extra x-codai-router-* response headers.

Request Body

application/json
  1. body

OpenAI Chat Completions body. Unknown fields are accepted (passthrough) and forwarded to OpenAI-compatible upstreams; Anthropic upstreams ignore them.

model?string

Logical model. codai is the smart router and the only publicly addressable model; any unrecognised name is coerced to codai.

Default"codai"
messages*array<>

Conversation so far. Above 2 000 messages the gateway trims the middle (system head + recent tail kept); above 20 000 the request is rejected with 400.

Items1 <= items <= 20000
stream?boolean

true returns text/event-stream chunks. The final usage chunk is always emitted.

Defaultfalse
stream_options?

Accepted for OpenAI compatibility; include_usage is effectively always on.

max_tokens?integer

Maximum completion tokens (legacy name).

Range1 <= value
max_completion_tokens?integer

Maximum completion tokens (current OpenAI name). Either name is honoured.

Range1 <= value
temperature?number
top_p?number
top_k?integer

codai extension forwarded to Anthropic upstreams.

stop?|array<string>

One stop string or an array of stop strings.

tools?array<|>

Function tools. Presence switches the request to the multi-turn loop path (streams are buffered and replayed). A codai_sandbox item lets the gateway run bash and Python server-side in an isolated sandbox (see the Sandboxes tag).

tool_choice?|

auto | none | required | {type: function, function: {name}}. required and named choices are downgraded to auto on models that reject forced tool use (Fable 5.x); none suppresses tools entirely.

response_format?

Structured-output control. json_object / json_schema inject a system instruction and validate the output at serve time; json_schema also carries the schema.

reasoning_effort?string

Test-time-compute effort tier (OpenAI vocabulary plus codai max; none accepted as minimal). The x-codai-effort header wins when both are present.

Value in"minimal""low""medium""high""max"
n?integer

Accepted for compatibility and forwarded only to OpenAI-compatible upstreams; codai always returns a single choice.

user?string

End-user identifier. When no x-codai-session-id header is present a non-empty user under 256 chars is used as the session id.

Lengthlength <= 255
metadata?

Accepted for compatibility (passthrough); not interpreted by the gateway.

[key: string]?any

Response Body

Completion. JSON (chat.completion) when stream is false or absent; text/event-stream of chat.completion.chunk objects when stream: true, always ending with a usage chunk and data: [DONE]. Response headers below are set on both forms; cost headers are x-codai-cost-micro-usd when the cost is known at response time and x-codai-cost-estimate: pending on native streams (the usage row is written after the stream closes).

  1. response
id*string
object*"chat.completion"
Value in"chat.completion"
created*integer

Unix seconds.

model*string

Upstream model that served the request.

choices*array<>

Always exactly one choice.

usage*
Example Request
curl -X POST 'https://ai.codai.ro/v1/chat/completions' \  -H 'Content-Type: application/json' \  -d '{  "messages": [    {      "role": "system"    }  ]}'
Example Response
{  "id": "chatcmpl_9f1c2b7e4d",  "object": "chat.completion",  "created": 1790108310,  "model": "claude-sonnet-5",  "choices": [    {      "index": 0,      "message": {        "role": "system",        "content": "string",        "name": "string",        "tool_call_id": "string",        "tool_calls": [          {            "id": "toolu_01Xy9k3mQ",            "type": "function",            "function": {              "name": "string",              "arguments": "{\"path\":\"src/index.ts\"}"            }          }        ]      },      "finish_reason": "stop"    }  ],  "usage": {    "prompt_tokens": 0,    "completion_tokens": 0,    "total_tokens": 0,    "prompt_tokens_details": {      "cached_tokens": 0    },    "cache_creation_input_tokens": 0  }}
POST
/v1/messages

Authorization

bearerAuth
headerAuthorizationBearer <codai_…>

A codai API key (codai_ prefix). Ephemeral tokens from POST /v1/tokens are accepted only by the media surfaces matching their scope (/v1/realtime, /v1/audio/*, /v1/embeddings).

Header Parameters

x-request-id?string

Client correlation id; echoed back as x-codai-trace-id and persisted on the usage row.

Lengthlength <= 128
x-codai-session-id?string

Groups requests of one conversation for receipts, stickiness and prompt caching.

Lengthlength <= 128
x-api-key?string

Anthropic-style API key header. Alternative to Authorization: Bearer; ignored when a Bearer header is present.

x-codai-effort?string

Test-time-compute effort tier. Wins over the body reasoning_effort. none is normalised to minimal; unknown values are ignored.

Value in"minimal""low""medium""high""max"
x-codai-thinking?string

Enable extended thinking upstream (1 or true). Default budget 16384 tokens.

Value in"1""true""0""false"
x-codai-thinking-budget?string

Thinking budget in tokens (positive integer). On adaptive-thinking models it is mapped to an effort level. Setting it also tells the compute allocator not to inject its own budget.

x-codai-cache?string

Prompt-caching opt-out (0 / false). Only honoured on direct model calls — caching is forced ON for the codai router alias.

Value in"0""false""1""true"
x-codai-compact?string

Force (1) or suppress (0) server-side conversation compaction for this request.

Value in"0""1"
x-codai-disable-subagents?"1"

1 disables sub-agent delegation for this request. Combined with stream: true and no tools this unlocks the native pass-through streaming path on sub-agent-capable tiers.

Value in"1"
x-codai-no-task?"1"

1 opts out of task counting. Only honoured for eligible utility aliases; on a full model the request is rejected with 400.

Value in"1"
x-codai-best-of?string

Best-of-N sampling on the buffered path (tier-gated; ignored when not entitled).

x-codai-best-of-depth?string

Sub-agent depth at which best-of applies on the buffered path.

x-codai-client?string

Free-form client identifier recorded on the task opened for this turn.

Request Body

application/json
  1. body

Public Anthropic Messages request. Unknown top-level fields pass validation and are ignored unless listed here as a codai extension.

model*string

Model or alias. Use codai for the smart router. Names that are not codai aliases are coerced to codai.

messages*array<>

Conversation turns, alternating user / assistant. Content is a string or an array of blocks. Over-long transcripts are trimmed from the middle rather than rejected.

Items1 <= items
max_tokens*integer

Maximum output tokens. Required.

Range1 <= value
system?|array<>

System prompt as a plain string or an array of text blocks (joined with blank lines).

temperature?number

Sampling temperature. Dropped on upstream models that reject sampling parameters.

top_p?number

Nucleus sampling. Dropped on upstream models that reject sampling parameters.

top_k?number

Top-k sampling. Dropped on upstream models that reject sampling parameters.

stop_sequences?array<string>

Custom stop sequences.

stream?boolean

true returns text/event-stream (see the operation description for the event list).

Defaultfalse
tools?array<>

Tool definitions the model may call. Any non-empty list forces the buffered execution path.

tool_choice?|

auto lets the model decide, any forces some tool call, tool forces a named tool. On Fable 5 / 5.1 upstreams any and tool are downgraded to auto because those models reject forced tool use.

metadata?

Only user_id is used; it is forwarded as the upstream user field.

thinking?|

Anthropic extended-thinking config. Accepted for SDK compatibility but NOT forwarded — the gateway decides the thinking shape per upstream model (enabled + budget_tokens on legacy models, adaptive on newer ones). Use the x-codai-thinking / x-codai-thinking-budget headers instead.

reasoning_effort?string

codai extension (OpenAI-standard body param). Effort tier for the compute allocator; the x-codai-effort header takes precedence.

Value in"minimal""low""medium""high""max"

Response Body

The assistant message. JSON when stream is false or absent; an SSE event sequence (see the operation description) when stream is true.

  1. response

Non-streaming response. model echoes the requested model or alias, not the upstream that answered (see x-codai-routed-to).

id*string
type*"message"
Value in"message"
role*"assistant"
Value in"assistant"
model*string
content*array<|>

Always at least one block; an empty reply is a single text block with text: ''.

stop_reason*string

Mapped from the upstream finish reason (content_filter becomes end_turn).

Value in"end_turn""max_tokens""stop_sequence""tool_use"
stop_sequence*string

Always null on the buffered path.

usage*
Example Request
curl -X POST 'https://ai.codai.ro/v1/messages' \  -H 'Content-Type: application/json' \  -d '{  "model": "codai",  "messages": [    {      "role": "user",      "content": "string"    }  ],  "max_tokens": 4096}'
Example Response
{  "id": "msg_1f0d5b7a-4c2e-4a9b-9c1d-2e3f4a5b6c7d",  "type": "message",  "role": "assistant",  "model": "codai",  "content": [    {      "type": "text",      "text": "string"    }  ],  "stop_reason": "end_turn",  "stop_sequence": "string",  "usage": {    "input_tokens": 0,    "output_tokens": 0,    "cache_read_input_tokens": 0,    "cache_creation_input_tokens": 0  }}
POST
/v1/responses

Authorization

bearerAuth
headerAuthorizationBearer <codai_…>

A codai API key (codai_ prefix). Ephemeral tokens from POST /v1/tokens are accepted only by the media surfaces matching their scope (/v1/realtime, /v1/audio/*, /v1/embeddings).

Header Parameters

x-request-id?string

Client correlation id; echoed back as x-codai-trace-id and persisted on the usage row.

Lengthlength <= 128
x-codai-session-id?string

Groups requests of one conversation for receipts, stickiness and prompt caching.

Lengthlength <= 128
x-codai-effort?string

Effort tier; when present it wins and reasoning.effort from the body is not mapped.

Value in"minimal""low""medium""high""max"
x-codai-cache?string

0/false disables prompt caching for a direct model id (ignored for the codai alias). Forwarded to the inner chat-completions call.

x-codai-no-task?string

Forwarded to the inner call; only valid on utility aliases (400 on codai).

x-codai-incognito?string

Forwarded to the inner call — stateless turn, nothing persisted.

x-codai-client?string

Forwarded to the inner call — client surface tag for task attribution.

Request Body

application/json
  1. body
model?string
Default"codai"
input?|array<>

Prompt as a single user string or as an array of input items.

instructions?string

Becomes the leading system message.

stream?boolean
Defaultfalse
max_output_tokens?integer

Mapped to max_tokens.

Range1 <= value
temperature?number
top_p?number
tools?array<>
tool_choice?|

Passed through to chat completions unchanged.

reasoning?

Only effort is mapped (xhigh → max, none → minimal, unknown values dropped). Ignored when x-codai-effort is sent.

previous_response_id?string

NOT supported — any value is rejected with 400. Send the full input context instead.

background?boolean

NOT supported — true is rejected with 400.

[key: string]?any

Response Body

A response object when stream is false; otherwise a text/event-stream of response.* events (see description). Non-stream responses carry the same x-codai-* headers as /v1/chat/completions (routing, trace, cost, task).

  1. response
id*string
object*"response"
Value in"response"
created_at*integer

Unix seconds.

status*"completed"
Value in"completed"
model*string

Upstream model that served the inner call.

output*array<|>

Function-call items first, then at most one assistant message.

output_text*string

Convenience copy of the assistant text (empty string when none).

usage*
Example Request
curl -X POST 'https://ai.codai.ro/v1/responses' \  -H 'Content-Type: application/json' \  -d '{}'
Example Response
{  "id": "resp_chatcmpl_9f1c2b7e4d",  "object": "response",  "created_at": 0,  "status": "completed",  "model": "string",  "output": [    {      "type": "function_call",      "id": "fc_toolu_01Xy9k3mQ",      "call_id": "string",      "name": "string",      "arguments": "string",      "status": "completed"    }  ],  "output_text": "string",  "usage": {    "input_tokens": 0,    "input_tokens_details": {      "cached_tokens": 0    },    "output_tokens": 0,    "total_tokens": 0  }}

Last updated: