Create Anthropic Messages
OpenAI-compatible gateway endpoint for Anthropic Messages API payloads.
/v1/anthropic/messages and /v1/anthropic/messages/count_tokens are deprecated and will be removed on September 3, 2026. Responses on those paths carry Deprecation and Sunset headers. Migration is a base-URL change only: point your Anthropic SDK at https://api.therouter.ai β the SDK appends /v1/messages itself.Request Parameters
| Name | Type | Required | Description |
|---|---|---|---|
model | string | Required | Anthropic model ID, for example anthropic/claude-sonnet-4.5. |
messages | array | Required | Anthropic-formatted message list. |
max_tokens | integer | Required | Maximum tokens to generate. |
system | string | array | System prompt string or array of system content blocks. | |
temperature | number | Sampling temperature between 0 and 1. | |
top_p | number | Nucleus sampling threshold. | |
top_k | integer | Top-K sampling: only consider the K most likely tokens. | |
stop_sequences | array | Sequences that will cause the model to stop generating. | |
stream | boolean | If true, returns a streaming SSE response. Defaults to false. | |
metadata | object | Arbitrary metadata object passed through with the request. | |
tools | array | Tool definitions available to the model (Anthropic tool format). | |
tool_choice | object | How the model selects tools: {"type": "auto"}, {"type": "any"}, or {"type": "tool", "name": "..."}. | |
thinking | object | Extended thinking controls, e.g. {"type": "enabled", "budget_tokens": 4096}. This endpoint takes the native Anthropic thinking block β the normalized reasoning object is an OpenAI-compatible-endpoint parameter (/v1/chat/completions) and is not translated here. On Claude 4.6+ models thinking is adaptive β the model self-selects its reasoning depth and budget_tokens does not apply. |
Authentication
This endpoint accepts both Authorization: Bearer and x-api-key headers. Use x-api-key for drop-in compatibility with the native Anthropic SDK.
Request Example
curl -X POST https://api.therouter.ai/v1/messages \
-H "x-api-key: $THEROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.5",
"max_tokens": 256,
"messages": [{"role": "user", "content": "Explain retrieval augmented generation."}]
}'Response
{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "RAG combines..."}],
"model": "anthropic/claude-sonnet-4.5",
"stop_reason": "end_turn",
"usage": {"input_tokens": 22, "output_tokens": 81}
}Streaming
Set stream: true to receive the response as server-sent events (SSE). The gateway supports two routing paths, both producing Anthropic-format SSE events:
| Name | Type | Required | Description |
|---|---|---|---|
Native passthrough | When the resolved provider is Anthropic, events are streamed directly from the upstream API. | ||
Translation path | When routing to a non-Anthropic provider, the gateway translates OpenAI SSE chunks into Anthropic-format events in real time. |
Events follow the standard Anthropic streaming format:
event: message_start
event: content_block_start
event: content_block_delta # repeated for each text chunk
event: content_block_stop
event: message_delta # final usage and stop_reason
event: message_stopThe stream is opened as soon as the request has been routed, and until the first upstream event the gateway writes Anthropic's event: ping every 15 seconds (the first one immediately), so intermediaries do not close an idle connection while a reasoning model is thinking. The Anthropic SDKs already ignore ping. An error after the stream is open arrives as a single event: error.
stream: true request to /v1/messages that fails before it is sent upstream β unknown model, a guardrails block, insufficient balance or quota β returns an HTTP error in the Anthropic error envelope, exactly as the Anthropic API does, instead of a 200 followed by event: error. SDK clients already handle this; a hand-written SSE consumer that assumed every streaming response is a 200 should check the status code first. In the same release, a stream that is not delivered in full is charged for the prompt plus the output actually written to your connection, with the reason recorded in the request log as delivery_outcome (values are listed in the Chat Completions reference); non-streaming requests remain subject to the edge's 100-second first-byte limit β use streaming for reasoning models.https://api.therouter.ai (the SDK appends /v1/messages itself) and use your TheRouter API key.