Messages API
Canonical reference: docs/api-reference.md (generated endpoint directory) — this page is a task-oriented summary.
ZERG exposes an Anthropic Messages API compatible endpoint. This enables Anthropic SDK clients and tooling to target ZERG directly.
Overview
The /v1/messages endpoint mirrors the Anthropic wire format. ZERG translates the request to the configured provider and returns Anthropic-formatted responses. Supports streaming via SSE for token-by-token output.
Authentication
Requires Authorization: Bearer <token> (cookie auth also accepted for browser sessions). Tokens are issued by Mango. x-api-key is also accepted as a bearer-equivalent credential. Unauthenticated calls return 401.
Endpoints
Create Message
POST /v1/messages
Request Body:
{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "Explain quicksort" }
],
"system": "You are a helpful assistant.",
"temperature": 0.7,
"stream": false
}
Response (non-streaming):
{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "Quicksort is a divide-and-conquer algorithm..." }
],
"model": "claude-sonnet-4-20250514",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 42,
"output_tokens": 156
}
}
Supported Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | string | required | Model identifier from the model catalog |
messages | array | required | Array of message objects |
max_tokens | integer | required | Maximum tokens in the response |
system | string | — | System prompt |
temperature | float | — | Sampling temperature |
stream | boolean | false | Enable SSE streaming (this endpoint defaults to non-streaming; the OpenAI leg defaults to true) |
stop_sequences | string[] | — | Custom stop sequences |
top_p | float | — | Nucleus sampling |
top_k | integer | — | Top-k sampling |
tools | array | — | Tool definitions (see Tool Use) |
tool_choice | string | — | Tool selection: auto, any, none |
thinking | object | — | Extended thinking config {type: "enabled", budget_tokens: 2048} |
response_format | object | — | Structured-output request, forwarded on the provider wire |
metadata | object | — | Metadata with user_id |
stream_options | object | {"include_usage": true} | Stream options on the OpenAI wire |
Streaming (stream: true):
Returns SSE events following the Anthropic streaming format:
event: message_start
data: {"type": "message_start", "message": {...}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Quicksort"}}
event: message_stop
data: {"type": "message_stop"}
Tool Use:
ZERG supports tool definition and execution through the Anthropic tool schema. Tools defined in the request are executed by the Luna agent runtime.
{
"tools": [{
"name": "web_search",
"description": "Search the web",
"input_schema": {
"type": "object",
"properties": {
"query": { "type": "string" }
}
}
}]
}
Error Codes (every HTTP error speaks the Anthropic {"type":"error",...} envelope — the flat {"error":"..."} shape is gone):
| Code | Description | Body |
|---|---|---|
| 400 | Invalid request body, missing model/messages, or malformed tool blocks | {"type":"error","error":{"type":"invalid_request_error","message":"..."}} |
| 401 | Missing or invalid authentication | {"type":"error","error":{"type":"authentication_error","message":"..."}} |
| 403 | Insufficient permissions | {"type":"error","error":{"type":"permission_error","message":"..."}} |
| 404 | Model not found or unavailable | {"type":"error","error":{"type":"not_found_error","message":"..."}} |
| 409 | Task cancelled before completion (non-streaming waiter) | {"type":"error","error":{"type":"invalid_request_error","message":"..."}} |
| 429 | Provider key exhausted or SSE budget full | {"type":"error","error":{"type":"rate_limit_error","message":"..."}} |
| 500 | The task itself failed/crashed (non-streaming waiter) | {"type":"error","error":{"type":"api_error","message":"..."}} |
| 502 | Upstream connect failure | {"type":"error","error":{"type":"api_error","message":"..."}} |
| 503 | All circuits open / provider unavailable | {"type":"error","error":{"type":"overloaded_error","message":"all circuits open"}} |
| 504 | Upstream timeout family (timeout, first_token_timeout, idle_timeout) | {"type":"error","error":{"type":"overloaded_error","message":"..."}} |
Usage-absence semantics: usage is reported only when the provider
actually emitted it — output tokens are never fabricated.
Example:
curl http://127.0.0.1:21434/v1/messages \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-20250514",
"max_tokens": 500,
"messages": [{"role": "user", "content": "Write a poem"}]
}'