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/completionsModels 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
{
"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
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model key configured in the dashboard |
messages | array | Yes | Array of message objects |
temperature | number | No | Sampling temperature (0-2) |
max_tokens | number | No | Maximum tokens in response |
max_completion_tokens | number | No | Alternative to max_tokens |
stream | boolean | No | Enable SSE streaming (default: false) |
request_id | string | No | Client-provided correlation ID |
reasoning_effort | string | No | none, minimal, low, medium, high, xhigh or max. Used only when the model has no Model Hub default; see Reasoning |
reasoning | object | No | Same as reasoning_effort in the Responses shape, { "effort": "high" }. The object wins when both are sent |
include_reasoning | boolean | No | false leaves the reasoning text out of this response. It can only narrow what the model allows |
Message Format
{ "role": "system" | "user" | "assistant" | "tool", "content": "..." }Response (Non-Streaming)
{
"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:
| Field | Meaning |
|---|---|
choices[0].message.reasoning_content | The 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_tokens | The thinking tokens, a subset of completion_tokens. Present when the provider reports them, also when the text is hidden |
{
"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, andfile_pathannotations ({ type, file_id, filename }) onchoices[0].message.annotations; - streaming: the same list as
fileson the closing chunk (the one withfinish_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:
{
"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
| Status | Description |
|---|---|
| 400 | A body that is not a JSON object (invalid_json: malformed, empty, an array or a string), missing model or messages, guardrail block |
| 400 | invalid_version: on an agent route, version is not a positive integer, a numeric string or "draft" (an unknown version is 404 version_not_found) |
| 400 | attachment_unreachable: on an agent route, a remote file_url / image_url did not arrive within the outbound timeout |
| 400 | model_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) |
| 422 | output_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 |
| 429 | On 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 |
| 503 | On an agent route: agent_run_interrupted, the run the call waited on was ended elsewhere (server shutdown, lost worker); retry it |
| 401 | Invalid API token |
| 500 | Model not found |
| 500 | Model is not configured for chat completions |
| 429 | Rate limit or quota exceeded |
| 500 | Provider error (circuit breaker / upstream) |
Examples
cURL
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
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
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();
