Anthropic Messages API

SwarmLLM provides a full Anthropic Messages API at POST /v1/messages, enabling it to serve as a drop-in backend for Claude Code and other Anthropic-compatible clients.

Claude Code Integration

Use SwarmLLM as your Claude Code backend to access all models (local, network, and cloud) through a single endpoint:

ANTHROPIC_BASE_URL=http://localhost:8800 claude --model qwen2.5-coder-7b

Environment Variables

VariableDescription
ANTHROPIC_BASE_URLPoint to your SwarmLLM node (e.g., http://localhost:8800)
ANTHROPIC_AUTH_TOKENYour node's API key (from Settings or /api/admin/api-key)
ANTHROPIC_MODELDefault model to use

POST /v1/messages

Request Body

FieldTypeRequiredDescription
modelstringyesModel name (local GGUF, network model, or cloud model like gpt-4o)
messagesarrayyesChat messages with role + content
max_tokensintegeryesMaximum tokens to generate (clamped to 1–32768)
systemstring or arraynoSystem prompt (supports cache_control blocks)
streambooleannoEnable SSE streaming
temperaturefloatnoSampling temperature
top_pfloatnoNucleus sampling
stop_sequencesarraynoStop sequences, 1–256 chars each, max 16
toolsarraynoTool definitions for function calling
tool_choiceobjectnoTool selection strategy
metadataobjectnoRequest metadata
thinkingobjectnoExtended thinking configuration

Content Block Types

Messages can contain these content block types:

// Text
{"type": "text", "text": "Hello, world!"}

// Image (base64)
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "..."}}

// Tool use (assistant response)
{"type": "tool_use", "id": "toolu_123", "name": "get_weather", "input": {"location": "NYC"}}

// Tool result (user message)
{"type": "tool_result", "tool_use_id": "toolu_123", "content": "72F, sunny"}

// Thinking (extended thinking)
{"type": "thinking", "thinking": "Let me reason about this..."}

// Redacted thinking
{"type": "redacted_thinking", "data": "..."}

Response

{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "model": "qwen2.5-coder-7b",
  "content": [
    {"type": "text", "text": "Here's my response..."}
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 25,
    "output_tokens": 150
  }
}

Model Routing

Requests are routed based on the model name:

Model PatternRouteDetails
Local GGUF modelLocal inferenceTool calls and thinking blocks converted to text
claude-*Anthropic APIFull pass-through (all fields preserved including tools and thinking)
gpt-*, o1-*, o3-*, o4-*OpenAIAnthropic→OpenAI format translation
deepseek-*DeepSeekAnthropic→OpenAI format translation
mistral-*, codestral-*, pixtral-*MistralAnthropic→OpenAI format translation
llama-*, groq-*GroqAnthropic→OpenAI format translation
nim-*NVIDIA NIMAnthropic→OpenAI format translation
cerebras-*CerebrasAnthropic→OpenAI format translation
samba-*SambaNovaAnthropic→OpenAI format translation
fireworks-*, accounts/fireworks/*Fireworks AIAnthropic→OpenAI format translation
together-*Together AIAnthropic→OpenAI format translation
deepinfra-*DeepInfraAnthropic→OpenAI format translation
moonshot-*, kimi-*Moonshot/KimiAnthropic→OpenAI format translation
Network modelDistributed inferenceRouted through swarm P2P network

All 12 cloud providers are supported. Configure API keys via the dashboard Settings page or by placing a .env file in the data directory (~/.local/share/swarmllm/.env) with standard variable names (e.g., OPENAI_API_KEY, DEEPSEEK_API_KEY).

System Blocks with Cache Control

Anthropic-compatible prompt caching:

{
  "system": [
    {"type": "text", "text": "You are a helpful assistant.", "cache_control": {"type": "ephemeral"}}
  ]
}

Streaming (SSE)

When stream: true, responses arrive as Server-Sent Events following the Anthropic streaming format:

event: message_start
data: {"type":"message_start","message":{"id":"msg_123","type":"message","role":"assistant","model":"qwen2.5-coder-7b","content":[]}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

event: message_stop
data: {"type":"message_stop"}