Skip to content

Chat Completions API ​

OpenAI-compatible chat completion endpoint with optional streaming. Prefer Anthropic's wire format? See Messages — both run through the same model routing, guardrails, and provider gateway.

Endpoint ​

POST /api/client/v1/chat/completions

Models and agents ​

model takes a model key, or agent:<key> to run an agent instead (a bare agent key works when no model has the same key). The same endpoint therefore serves both, and the same token needs the models service for a model and the agents service for an agent. GET /api/client/v1/models lists the project's models and its active agents (as agent:<key>), so OpenAI clients find both; it admits a token with the models or the agents service, and a token with agents only lists the agents alone. Agent calls stream, accept response_format, service_tier: "flex", metadata and variables, and may answer 202 when the turn outlasts the wait window; see the Agents API and Calling agents from other systems.

Request ​

json
{
  "model": "gpt-4",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "What is the capital of France?" }
  ],
  "temperature": 0.7,
  "max_tokens": 1000,
  "stream": false,
  "request_id": "optional-correlation-id"
}

Parameters ​

FieldTypeRequiredDescription
modelstringYesModel key configured in the dashboard
messagesarrayYesArray of message objects
temperaturenumberNoSampling temperature (0-2)
max_tokensnumberNoMaximum tokens in response
max_completion_tokensnumberNoAlternative to max_tokens
streambooleanNoEnable SSE streaming (default: false)
request_idstringNoClient-provided correlation ID
reasoning_effortstringNonone, minimal, low, medium, high, xhigh or max. Used only when the model has no Model Hub default; see Reasoning
reasoningobjectNoSame as reasoning_effort in the Responses shape, { "effort": "high" }. The object wins when both are sent
include_reasoningbooleanNofalse leaves the reasoning text out of this response. It can only narrow what the model allows

Message Format ​

json
{ "role": "system" | "user" | "assistant" | "tool", "content": "..." }

Response (Non-Streaming) ​

json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "model": "gpt-4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 8,
    "total_tokens": 33
  },
  "request_id": "req_abc123"
}

Reasoning ​

Available from the next Cognipeer release

include_reasoning, reasoning_content and the Model Hub reasoning default arrive with the next Cognipeer release.

For a reasoning model, the effort is decided in this order: the Model Hub default of the model, then reasoning_effort / reasoning in the request, then nothing (the provider's own default). A caller's effort only reaches models that accept one. What the response contains:

FieldMeaning
choices[0].message.reasoning_contentThe model's reasoning (usually a summary), present when the model returns it, the Model Hub Return reasoning to callers is on and the request did not send include_reasoning: false. In a stream it arrives as delta.reasoning_content
usage.completion_tokens_details.reasoning_tokensThe thinking tokens, a subset of completion_tokens. Present when the provider reports them, also when the text is hidden
json
{
  "model": "claude-sonnet",
  "messages": [{ "role": "user", "content": "Plan the migration in three steps." }],
  "reasoning_effort": "high",
  "include_reasoning": false
}

The call above thinks at high (unless the Model Hub sets another default) and returns no reasoning_content; reasoning_tokens still shows what the thinking cost. Details, including Claude's thinking forms: Reasoning.

Response (Streaming) ​

When stream: true, the response is a Server-Sent Events stream:

HTTP/1.1 200 OK
Content-Type: text/event-stream
X-Request-Id: req_abc123

data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]}

data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":"The"},"index":0}]}

data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":" capital"},"index":0}]}

data: [DONE]

Files the agent produced ​

When model is an agent and the turn produced files (left in its sandbox's /outputs/ folder, or written with session_files_create), the reply also carries them. This is additive and absent otherwise (see Session files):

  • non-streaming: output_files: [{ id, name, mime_type, size, download_url }] on the body, and file_path annotations ({ type, file_id, filename }) on choices[0].message.annotations;
  • streaming: the same list as files on the closing chunk (the one with finish_reason).

download_url is relative to the server origin and already includes /api; fetch it with the same credentials.

Guardrail Blocking ​

If a guardrail blocks the request:

json
{
  "error": {
    "message": "Content blocked by guardrail",
    "type": "guardrail_block",
    "guardrail_key": "pii-checker",
    "action": "block",
    "findings": [
      { "category": "email", "message": "PII detected" }
    ]
  }
}

Status code: 400

Errors ​

StatusDescription
400A body that is not a JSON object (invalid_json: malformed, empty, an array or a string), missing model or messages, guardrail block
400invalid_version: on an agent route, version is not a positive integer, a numeric string or "draft" (an unknown version is 404 version_not_found)
400attachment_unreachable: on an agent route, a remote file_url / image_url did not arrive within the outbound timeout
400model_not_allowed: on an agent route, the requested model is not offered by the agent, the requested model is not offered by the agent (see Agent models)
422output_token_limit_exceeded: the model hit its output token limit before it could answer (param: max_completion_tokens). Raise max_completion_tokens or the model's maximum output tokens, or lower the reasoning effort. Agents soften this into a notice, see Agent models
429On an agent route: agent_run_concurrency_limit (Retry-After: 30) or agent_quota_exceeded (Retry-After: 60). A stream is checked before it opens, so this is a plain JSON 429, not an error chunk
503On an agent route: agent_run_interrupted, the run the call waited on was ended elsewhere (server shutdown, lost worker); retry it
401Invalid API token
500Model not found
500Model is not configured for chat completions
429Rate limit or quota exceeded
500Provider error (circuit breaker / upstream)

Examples ​

cURL ​

bash
curl -X POST https://gateway.example.com/api/client/v1/chat/completions \
  -H "Authorization: Bearer cpeer_your_token" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Python ​

python
import requests

response = requests.post(
    "https://gateway.example.com/api/client/v1/chat/completions",
    headers={"Authorization": "Bearer cpeer_your_token"},
    json={
        "model": "gpt-4",
        "messages": [{"role": "user", "content": "Hello"}]
    }
)
print(response.json())

Node.js ​

typescript
const response = await fetch('https://gateway.example.com/api/client/v1/chat/completions', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer cpeer_your_token',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'gpt-4',
    messages: [{ role: 'user', content: 'Hello' }],
  }),
});
const data = await response.json();

Studio · Pulse · Console · Agent SDK and more — the Cognipeer documentation hub