# Messages

Use the Messages interface for Claude Code, text and local function tools, with native thinking and prompt-cache support.

`POST /anthropic/v1/messages`

`POST /anthropic/v1/messages/count_tokens`

`GET /anthropic/v1/models`

`GET /anthropic/v1/models/{model_id}`

## Send a message

Set the SDK base URL to the gateway’s /anthropic path. max_tokens is required and must be 1–131,072 within the model’s limits. Use user and assistant messages. Pass initial system instructions separately; mid-conversation system messages are also accepted.

### Python · Anthropic SDK

```text
import os
from anthropic import Anthropic

client = Anthropic(
    api_key=os.environ["UROUTER_API_KEY"],
    base_url="https://urouter.net/gateway/anthropic",
    max_retries=0,
)
message = client.messages.create(
    model="YOUR_MODEL_ID",
    max_tokens=256,
    messages=[{"role": "user", "content": "Explain DNS in one sentence."}],
)
for block in message.content:
    if block.type == "text":
        print(block.text)
```

## Compatibility boundaries

Send model, messages and max_tokens. Optional fields include system, temperature, top_p, top_k, stream, tools, tool_choice, metadata, thinking, output_config, stop_sequences, cache_control and context_management. Authenticate with x-api-key or Bearer. Send up to 1,024 messages within the model's context limit and a 2 MiB limit on serialized messages excluding image URLs. File IDs, documents, hosted tools and structured output formats are unsupported.

- Define up to 128 local tools with name and input_schema. type may be omitted or set to custom. description can be omitted or null. Optional fields include cache_control, strict, defer_loading, up to 20 object input_examples and allowed_callers set to ["direct"]. Serialized tools must fit 512 KiB. tool_choice supports auto, any, none or a named tool, plus disable_parallel_tool_use.
- Return user tool_result blocks matching every assistant tool_use ID. Tool-result content may contain text or images, or be omitted for an empty result. Preserve thinking and redacted_thinking blocks unchanged when returning assistant history.
- Responses preserve text, function calls and signed thinking blocks. Streams finish with message_stop after a complete response. When reported, usage.cache_read_input_tokens and usage.cache_creation_input_tokens are separate from usage.input_tokens. All three input categories count toward recorded usage. Use request status for the confirmed charge.
- If available credit cannot cover the request, both interfaces return HTTP 402. The Messages interface uses billing_error with details.error_code set to insufficient_balance. Keep the request ID when troubleshooting.

## Images, caching and thinking

Supported vision models accept base64 PNG, JPEG, GIF and WebP images or HTTPS image URLs in user messages and tool results. Each request allows up to 16 images and 12 MiB of image URL or data text. Image requests reserve credit for the model's full context, which can require more available credit than the final charge.

- cache_control may be null or an ephemeral object with optional ttl of 5m or 1h. Use it at the request or message level, or on supported content blocks and tools. metadata.user_id may be null or a string of up to 1,024 characters.
- output_config.effort supports low, medium, high, xhigh or max. You can omit it or set it to null at the request level, or send a system message containing only output_config with an explicit effort. context_management accepts null or one clear_thinking_20251015 edit with keep set to all. Other context edits and compaction are unsupported.
- With thinking.type set to enabled, budget_tokens must be an integer from 1,024 to one less than max_tokens. Omit budget_tokens for adaptive or disabled. The optional display setting can be summarized or omitted.

## Models and estimated token counts

GET /anthropic/v1/models supports limit from 1 to 1,000 (default 100), either after_id or before_id, and optional beta=true. Individual models can be read by ID. POST /anthropic/v1/messages/count_tokens accepts model, messages, system, tools, tool_choice, metadata, thinking, output_config, cache_control and context_management. Omit max_tokens on this endpoint.

- The token-count response reports input_tokens and estimated: true. It uses a heuristic that accounts for message structure, tool schemas and images. Use a reservation quote or request status for billing information.
- Use the main /v1/models catalog for pricing, required credit and capabilities.
