Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

ParameterTypeDefaultDescription
modelstringrequiredModel identifier from the model catalog
messagesarrayrequiredArray of message objects
max_tokensintegerrequiredMaximum tokens in the response
systemstring—System prompt
temperaturefloat—Sampling temperature
streambooleanfalseEnable SSE streaming (this endpoint defaults to non-streaming; the OpenAI leg defaults to true)
stop_sequencesstring[]—Custom stop sequences
top_pfloat—Nucleus sampling
top_kinteger—Top-k sampling
toolsarray—Tool definitions (see Tool Use)
tool_choicestring—Tool selection: auto, any, none
thinkingobject—Extended thinking config {type: "enabled", budget_tokens: 2048}
response_formatobject—Structured-output request, forwarded on the provider wire
metadataobject—Metadata with user_id
stream_optionsobject{"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):

CodeDescriptionBody
400Invalid request body, missing model/messages, or malformed tool blocks{"type":"error","error":{"type":"invalid_request_error","message":"..."}}
401Missing or invalid authentication{"type":"error","error":{"type":"authentication_error","message":"..."}}
403Insufficient permissions{"type":"error","error":{"type":"permission_error","message":"..."}}
404Model not found or unavailable{"type":"error","error":{"type":"not_found_error","message":"..."}}
409Task cancelled before completion (non-streaming waiter){"type":"error","error":{"type":"invalid_request_error","message":"..."}}
429Provider key exhausted or SSE budget full{"type":"error","error":{"type":"rate_limit_error","message":"..."}}
500The task itself failed/crashed (non-streaming waiter){"type":"error","error":{"type":"api_error","message":"..."}}
502Upstream connect failure{"type":"error","error":{"type":"api_error","message":"..."}}
503All circuits open / provider unavailable{"type":"error","error":{"type":"overloaded_error","message":"all circuits open"}}
504Upstream 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"}]
  }'