Skip to content

Anthropic Messages

Anthropic Messages requests, for Claude Code and the Anthropic SDKs.

POST https://api.aimodelsrouter.ai/v1/messages takes an Anthropic Messages API request and runs it on the same models, prices, holds, key limits and failover as Chat Completions. Use it for Claude Code and the Anthropic SDKs.

  • Base URL: https://api.aimodelsrouter.ai, without /v1: Anthropic clients add /v1/messages themselves.
  • Key: your sk-amr-v1-… key, as x-api-key or Authorization: Bearer.
  • Headers: anthropic-version and anthropic-beta are accepted and ignored.
Claude models are not available here. Set model to one of our model slugs; a claude-… id gets 404 not_found_error that names the setting to change.

Request body

FieldTypeDescription
modelstring, requiredOne of our model slugs. claude-* ids get 404 not_found_error: we don't serve Anthropic's models.
max_tokensinteger, requiredUpper bound on generated tokens. Also lowers the hold placed on your balance.
messagesarray, requireduser and assistant turns (system turns are accepted too). Consecutive turns with the same role are combined, as Anthropic does.
systemstring | text blocksSystem prompt, sent to the model first.
toolsarrayCustom tools with an input_schema. Tools Anthropic runs or defines (web_search_*, code_execution_*, bash_*, text_editor_*, computer_*…) get 400. A tool with defer_loading stays out of the model's context until a tool_reference in a tool_result names it.
tool_choiceobjectauto, any, tool (with name) or none; disable_parallel_tool_use is honoured.
stop_sequencesstring[]Stop sequences. stop_reason is stop_sequence when the model provider says which one matched.
temperature, top_p, top_knumberSampling parameters, passed through to the model.
thinkingobjectenabled or adaptive returns the model's reasoning as thinking blocks; without it, reasoning is not returned (it is still billed as output, as Anthropic bills thinking). display: omitted returns empty thinking text.
output_config.effort, thinking.budget_tokensSent as reasoning_effort to models with the reasoning capability (max is sent as high; a budget under 4,096 tokens as low, under 16,384 as medium).
output_config.formatobjectjson_schema structured output, for models with the structured_outputs capability.
streambooleanStream the response as server-sent events. Default false.
metadata, cache_control, service_tier, context_managementAccepted and ignored. Nothing is stored or passed to the model provider.

Content blocks

BlockNotes
textText.
imageA base64 or url source. Images inside a tool_result are shown to the model in a user turn right after the results.
tool_use / tool_resultTool calls and their results (text, images, documents, is_error).
thinkingReasoning we returned earlier. Send it back, as Claude Code does, and the model gets its own earlier reasoning again; some models need it on tool turns. redacted_thinking is ignored.
documentText sources are sent as text. A PDF or other file is replaced by a note telling the model it was attached but couldn't be read.
# pip install anthropic
import os

import anthropic

client = anthropic.Anthropic(base_url="https://api.aimodelsrouter.ai", api_key=os.environ["AMR_API_KEY"])

message = client.messages.create(
    model="deepseek/deepseek-v4-flash",
    max_tokens=512,
    system="You are concise.",
    messages=[{"role": "user", "content": "Explain idempotency keys in one paragraph."}],
)
print(message.content[0].text)
print(message.usage.model_extra["cost"])  # USD charged

Response

  • id is our generation id (gen-…), also in the X-Generation-Id header.
  • content: a thinking block when thinking is on and the model reasoned, the text, and a tool_use block per tool call.
  • stop_reason: end_turn, max_tokens, tool_use, stop_sequence or refusal.
  • usage: input_tokens (not counting cache reads, as Anthropic counts), cache_read_input_tokens, cache_creation_input_tokens (always 0), output_tokens and cost, the amount charged in USD. Prompt caching happens at the model provider when it supports it; cache_control is not needed.

Streaming

With "stream": true you get Anthropic's events: message_start, then per block content_block_start, content_block_delta (text_delta, thinking_delta, signature_delta or input_json_delta) and content_block_stop, then message_delta with the stop_reason and the final usage and cost, and message_stop. While the model is still thinking we send ping events.

message_start carries an estimate of input_tokens; the real count, as charged, is in message_delta.

Count tokens

POST https://api.aimodelsrouter.ai/v1/messages/count_tokens takes the same body without max_tokens and returns {"input_tokens": N}. It is an estimate (about four bytes of prompt per token, a typical figure per image), it is free, and nothing is sent to a model.

Errors

Errors use Anthropic's envelope and status codes:

{"type": "error", "error": {"type": "billing_error", "message": "Insufficient credits. This request can cost up to $0.004100 (prompt plus the maximum output for `deepseek/deepseek-v4-flash`). Add credits, or set a lower `max_tokens` to reduce the hold."}}
HTTPerror.typeWhen
400, 405invalid_request_errorMalformed request, an unsupported field or tool, or a parameter the model rejected.
401authentication_errorMissing, unknown, disabled, revoked or expired key.
402billing_errorInsufficient credits, a key spending limit (the message says which and when it resets), or a model available only after your first credit purchase (the message names one you can use now).
403permission_errorAccount suspended.
404not_found_errorUnknown or claude-* model; the message says which variable to set.
408timeout_errorThe model did not respond in time. Nothing was charged.
413request_too_largeBody over 8 MB.
429rate_limit_errorKey rate, concurrency or daily token limit, or the model is at capacity. Retry-After is set.
500api_errorSomething failed on our side. Nothing was charged.
503overloaded_errorEvery route for the model failed or is paused. Nothing was charged.

After a stream has started, a failure arrives as an error event (for example overloaded_error when the model provider failed). You pay only for the output already sent.