Anthropic Messages API
/v1/messages serves the Anthropic Messages API format for every model the gateway knows. Applications and tools built on the Anthropic SDK, including coding agents, keep their request and response format while the gateway routes the call to Anthropic, Bedrock, Vertex AI or any other provider, translating as needed. For Anthropic models the request is forwarded in its native form, so features such as extended thinking, tool use, prompt caching and the skills and files betas work unchanged.
Scenario
A development team uses a coding agent that speaks the Anthropic Messages format. The platform admin wants every call to go through the gateway for budgets and logging, and wants to be able to serve the agent from Bedrock during an Anthropic outage. The team points the agent's base URL at the gateway and uses a virtual key as the API key. The admin puts an Anthropic deployment and a Bedrock deployment behind the same public name with a fallback, and the agent never notices which one answered.
Steps
-
Register the model. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints), click Add Model, pick the Provider, choose the model, set the Public Model Name toclaude-sonnet-5-5and set Mode to Anthropic Messages - /v1/messages so health checks use this format, then click Add Model. -
Create a key. Open Virtual Keys (
/ui/api-keys), click + Create New Key, name it, pick the model under Models and click Create Key. -
Send a message. The key can be sent as
x-api-keyor asAuthorization: Bearer.curl -X POST http://localhost:4000/v1/messages \-H "x-api-key: $SPANWAY_API_KEY" \-H "anthropic-version: 2023-06-01" \-H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5","max_tokens": 512,"system": "You are a careful reviewer.","messages": [{"role": "user", "content": "Review this function for bugs: def add(a, b): return a - b"}]}' -
Point your tool at the gateway. For the Anthropic SDK set
base_urlto the gateway root without/v1, because the SDK adds/v1/messagesitself, and setapi_keyto the virtual key. Coding agents that readANTHROPIC_BASE_URLtake the same root.ANTHROPIC_API_KEYsends the key asx-api-keyandANTHROPIC_AUTH_TOKENsends it asAuthorization: Bearer, and the gateway accepts both. For Claude Code,spanwayctl configure claudewrites these settings for you, see coding agent setup and connect any client. -
Try it in the browser. Open Playground (
/ui/playground), set Endpoint Type to/v1/messages, pick the model and send a message. Then open Logs (/ui/logs) to see the request with its cost.
Expected result
{
"id": "msg_01Xf7c2d4e1f",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-5-5",
"content": [{"type": "text", "text": "The function subtracts instead of adding."}],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {"input_tokens": 42, "output_tokens": 31}
}
Streaming returns Anthropic style server-sent events (message_start, content_block_delta, message_delta and so on), which the gateway also produces for non-Anthropic models.
Request fields
model is a public model name from the gateway. Supported fields include messages, system, max_tokens, temperature, top_p, top_k, stop_sequences, stream, tools, tool_choice, thinking, metadata and cache_control on content blocks. Beta features are requested with the anthropic-beta header, which the gateway forwards to Anthropic.
Other providers behind the Anthropic format
import os
import anthropic
client = anthropic.Anthropic(base_url="http://localhost:4000", api_key=os.environ["SPANWAY_API_KEY"])
message = client.messages.create(
model="gpt-6.1-sol",
max_tokens=256,
messages=[{"role": "user", "content": "Hello from an Anthropic client"}],
)
print(message.content[0].text)
Here an OpenAI model answers an Anthropic format request. Tool definitions, tool results, images and documents are translated between formats.
Gateway features
Keys, budgets, rate limits, routing, guardrails, caching and cost tracking apply to /v1/messages exactly as to chat completions. The response includes the x-spanway-* headers described in calling the gateway. Native Anthropic endpoints beyond messages are available through the Anthropic pass-through.
Errors
Every error on this route, and on /claude_code_gateway/v1/messages that Claude Code calls after gateway sign-in, comes back in the Anthropic shape, {"type": "error", "error": {"type": "...", "message": "..."}}, so the Anthropic SDKs read it like an answer from Anthropic itself. That includes the errors the gateway raises while it checks the key, such as a missing or wrong key, a model the key may not use and a used-up budget, as well as errors raised later, such as an unknown model name or a provider error. When the request carries an x-request-id header, the body repeats it as request_id.
| Status | Type | What it means | Fix |
|---|---|---|---|
| 400 | invalid_request_error | max_tokens is missing, a field is malformed, or no deployment carries the model name (anthropic_messages: Invalid model name passed in model=<name>). | Send max_tokens with every request, and call GET /v1/models to see the names the key may use. |
| 401 | authentication_error | The key is missing, wrong or expired. The message reads Authentication Error, No api key passed in. or names the key problem. | Send a live key in x-api-key or Authorization: Bearer. |
| 403 | permission_error | The key may not call this model. The message reads The requested model '<model>' is not available for this API key, or the model name is invalid. | Add the model to the key. A guardrail block is a 400 invalid_request_error, read its result in Logs. |
| 422 | api_error | The key, team or user has spent its budget. The message starts with Budget has been exceeded! | Raise the budget or wait for it to reset. |
| 429 | rate_limit_error | The key or team is over its rate limit, or the provider is throttling. | Raise the limit or add a fallback deployment. |
| 529 | overloaded_error | Anthropic is overloaded. | Retries and fallbacks in reliability cover this. |
The client error reference maps each status to the exception class of the Anthropic, OpenAI and Google SDKs.
Logs
Each call is one row on the Logs page (/ui/logs) with its key, team, model group, deployment, tokens, cost, latency and status, failed calls included. Prompt and response text is kept only when the gateway is set to store it, see what the gateway stores. Operator log lines for routing, retries and fallbacks are in the log reference.
Related
- Count tokens for
/v1/messages/count_tokens. - Chat completions for the OpenAI format of the same models.
- Claude Code through the gateway and coding agent setup.
- Anthropic pass-through for other Anthropic routes.
Providers
The gateway's endpoint support matrix marks 129 provider integrations as supporting this endpoint:
AI21, AI21 Chat, AIHubMix (aihubmix), AI/ML API, Amazon Nova, Anthropic, Anthropic Text, AssemblyAI (assemblyai), Auto Router (auto_router), Azure, Azure AI, Azure AI Foundry Agents, Azure Text, Baseten, AWS - Bedrock, Bedrock Mantle, Bytez, Cerebras, Charity Engine (charity_engine), ChatGPT Subscription, Clarifai, Cloudflare AI Workers, Codestral, Cognition, Cohere, Cohere Chat, CometAPI, CompactifAI, Cortecs, Crusoe (crusoe), Cursor BYOK, Custom, Custom OpenAI (custom_openai), Dashscope, Databricks, DataRobot, Deepgram, DeepInfra, Deepseek, Docker Model Runner, Eden AI, ElevenLabs, Empower, Fal AI, Featherless AI, Fireworks AI, FriendliAI, Galadriel, Google AI Studio - Gemini, GigaChat, GitHub Models, GitHub Copilot, GMI Cloud, GradientAI, Groq AI, Helicone, Heroku, Hosted VLLM, Huggingface, Hyperbolic, Inception (inception), Lambda AI, LangGraph, Lemonade, LibertAI, Llamafile, LlamaGate, LM Studio, Manus, Maritalk, Meta Model API (meta), Meta - Llama API, Minimax, Mistral AI API, ModelScope, Moonshot, Morph, Nebius AI Studio, NLP Cloud, Novita AI (novita), Nscale, Nvidia NIM, OCI, Ollama, Ollama Chat, Oobabooga (oobabooga), OpenAI, OpenRouter, OVHCloud AI Endpoints, Perplexity AI, Petals, Pinstripes, Predibase, Prism, PublicAI, Qianwen AI Platform, QwenCloud, RAGFlow, Reka, Replicate, AWS - Sagemaker, Sagemaker Chat, Sail, Sambanova, SAP Generative AI Hub, Sarvam, Scaleway, Snowflake, Spanway Proxy, Synthetic (synthetic), Tencent TokenHub (tencent), Tensormesh (tensormesh), Text Completion Codestral, Text Completion OpenAI, Together AI, Triton, V0, Venice.ai, Vercel AI Gateway, Google - Vertex AI, Vertex AI Agent Engine, VLLM, Volcengine, WandB Inference, IBM - Watsonx.ai, Watsonx Text, xAI, Xiaomi Mimo, Z.AI (Zhipu AI).