Completions API
Canonical reference: docs/api-reference.md (generated endpoint directory) — this page is a task-oriented summary.
ZERG exposes an OpenAI Chat Completions compatible endpoint. This allows any OpenAI SDK or tool to target ZERG as a drop-in replacement.
Overview
The /v1/chat/completions endpoint mirrors the OpenAI wire format. ZERG translates the request internally, dispatches to the configured provider (Anthropic, OpenAI, z.ai, Ollama, etc.), and returns OpenAI-formatted responses. Streaming is supported via SSE.
Authentication
Requires Authorization: Bearer <token> header. The token is issued via POST /api/v1/auth/login (the Mango login proxy). The route additionally requires the inference:use permission.
Endpoints
Create Chat Completion
POST /v1/chat/completions
Request Body:
{
"model": "claude-sonnet-4-20250514",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Explain quicksort" }
],
"max_tokens": 1024,
"temperature": 0.7,
"stream": false
}
Response (non-streaming):
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1745600000,
"model": "claude-sonnet-4-20250514",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Quicksort is a divide-and-conquer algorithm..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 42,
"completion_tokens": 156,
"total_tokens": 198
}
}
Streaming Mode (stream: true):
Returns SSE events with data: {"choices": [{"delta": {"content": "..."}}]} fragments. Final event contains finish_reason and usage.
Supported Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | string | required | Model identifier from the model catalog (prefix matching supported) |
messages | array | required | Array of message objects (system, user, assistant, tool) |
max_tokens | integer | model default | Maximum tokens in the response |
temperature | float | template default | Sampling temperature |
stream | boolean | true | Enable SSE streaming (the OpenAI leg defaults to streaming) |
stop | string[] | — | Stop sequences |
stop_sequences | string[] | — | Stop sequences (alias) |
tools / tool_choice | array / string | — | Tool definitions and selection (translated per provider wire) |
top_p | float | template default | Nucleus sampling parameter |
timeout | integer | 300000 | Request timeout in ms (clamped to client_timeout_max_ms, default 600 s) |
Provider Translation:
The ZERG gateway translates OpenAI messages to the target provider’s wire format. Tool calls are converted bidirectionally between OpenAI tool_calls and Anthropic tool_use blocks. Mid-conversation provider switching preserves context.
Error Codes (every error carries the OpenAI {"error":{...}} envelope so SDK clients can parse error.message; the 413 oversized-body arm is the one exception — it answers the Zerg envelope):
| Code | Description | Body |
|---|---|---|
| 400 | Invalid request body or missing model/messages | {"error":{"message":"...","type":"invalid_request_error","param":null,"code":null}} |
| 401 | Missing or invalid authentication token | {"error":{"message":"authentication required","type":"invalid_request_error",...}} |
| 403 | Insufficient permissions (inference:use missing) | {"error":{"message":"...","type":"invalid_request_error",...}} |
| 413 | Oversized body | {"ok":false,"error":{"code":"body_too_large","message":"..."}} (Zerg envelope) |
| 429 | Model at capacity, provider key cooling down, or SSE budget full | {"error":{"message":"...","type":"rate_limit_error",...}} + Retry-After: 1 |
| 502 | Upstream connect failure (connect_failed, connection_down) | {"error":{"message":"...","type":"api_error",...}} |
| 503 | Model unavailable (pre-stream), or resolver circuits open | Pre-stream: {"error":{"message":"model unavailable","type":"server_error",...}}; circuit-open arms currently answer the Zerg envelope {"ok":false,"error":{"code":"all circuits open",...}} (family leak — see the note below) |
| 504 | Upstream timeout family (timeout, first_token_timeout, idle_timeout) | {"error":{"message":"...","type":"server_error",...}} |
Interrupted streams: the final SSE chunk carries
"finish_reason": "interrupted" followed by the regular data: [DONE]
framing; an error frame emitted after content tokens carries
"truncated": true (partial answer delivered vs failed-before-output).
Family note: the resolver’s circuit-open arms on this route answer the Zerg envelope instead of the OpenAI one (a known family leak in the handler — the pre-stream capacity/down arms and the auth middleware do speak the OpenAI envelope). The SSE-budget refusal on all completion routes answers 429
sse_limit_reachedwithRetry-After: 1in the flat{"ok":false,"error":"sse_limit_reached"}form.
Example:
curl http://127.0.0.1:21434/v1/chat/completions \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 100
}'
Streaming variant:
curl -N http://127.0.0.1:21434/v1/chat/completions \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [{"role": "user", "content": "Count to 10"}],
"stream": true
}'