Skip to content

Docs / Anthropic-compatible API

Anthropic-compatible API

POST /v1/messages and /v1/messages/count_tokens accept the Anthropic request body. Use the Anthropic SDK or Claude Code with any model.

Get an API key

POST /v1/messages takes the Anthropic Messages request body and answers in the Anthropic shape, errors included. POST /v1/messages/count_tokens counts a prompt’s tokens. The Anthropic Python SDK runs against the API in our test suite on every change.

Every model is callable here, not only Claude models. Claude Code and other Anthropic-format tools can use any model in the catalogue.

Base URL: https://api.yunshurouter.com

No /v1 at the end. The Anthropic SDK and Claude Code add /v1/messages themselves. With /v1 in the base URL, requests go to /v1/v1/messages and fail with 404.

Example

curl https://api.yunshurouter.com/v1/messages \
  -H "x-api-key: $YUNSHU_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-pro",
    "max_tokens": 512,
    "system": "You answer in one sentence.",
    "messages": [{"role": "user", "content": "What is a context window?"}]
  }'
import anthropic

client = anthropic.Anthropic(base_url="https://api.yunshurouter.com", api_key="tq-...")

message = client.messages.create(
    model="deepseek-v4-pro",
    max_tokens=512,
    system="You answer in one sentence.",
    messages=[{"role": "user", "content": "What is a context window?"}],
)
print(message.content[0].text)
print(message.usage)
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ baseURL: "https://api.yunshurouter.com", apiKey: "tq-..." });

const message = await client.messages.create({
  model: "deepseek-v4-pro",
  max_tokens: 512,
  system: "You answer in one sentence.",
  messages: [{ role: "user", content: "What is a context window?" }],
});
console.log(message.content);

Request fields

Field Handling
model Required. A model ID, a combo (combo/<name>), or a model with :floor / :nitro. Up to 256 characters
messages Required. At least 1, at most 4,096. tool_use and tool_result blocks are kept
max_tokens 1 to 1,000,000, lowered to the model’s maximum output. Default when omitted: 1,024 (Anthropic’s own API requires it; we do not)
system A string or a list of text blocks. A list is joined into one string
temperature 0 to 2
stream true for Anthropic server-sent events. See Streaming
tools, tool_choice Up to 512 tools. See Tool calling
thinking Turns reasoning on or off. See Reasoning
fallbacks Up to 3 backup models, as [{"model": "..."}]. See Fallbacks and combos
top_p, top_k, stop_sequences and other fields Passed to the model as sent

Limits are the same as the OpenAI format: 2 MiB body, 4,096 messages, 512 tools. See OpenAI-compatible API.

Content and system blocks

  • system given as blocks is sent as plain text. Block-level options such as cache_control on system blocks are not kept. See Prompt caching.
  • Image blocks are passed to the model.

Response

The body is an Anthropic message object: type: "message", content blocks, stop_reason and usage (input_tokens, output_tokens, and cache fields when the model reports them). model is the model that answered.

If a model returns only thinking and no text, the thinking is also returned as a text block, so clients that read text see an answer.

Response headers are the same as the OpenAI format: x-yunshu-request-id, x-yunshu-request-ref, x-yunshu-model, x-yunshu-cache. See OpenAI-compatible API.

raw = client.messages.with_raw_response.create(
    model="deepseek-v4-pro", max_tokens=256,
    messages=[{"role": "user", "content": "Hi"}],
)
print(raw.headers.get("x-yunshu-request-id"))
message = raw.parse()

Counting tokens

POST /v1/messages/count_tokens takes the same body as /v1/messages and returns the input token count. It is free and nothing is sent to a model.

curl https://api.yunshurouter.com/v1/messages/count_tokens \
  -H "x-api-key: $YUNSHU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-pro",
    "messages": [{"role": "user", "content": "How many tokens is this?"}]
  }'
{"input_tokens": 14}
count = client.messages.count_tokens(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "How many tokens is this?"}],
)
print(count.input_tokens)

What is counted: messages, the system prompt, tool definitions, tool results and images.

The count is our own estimate, not the model’s tokenizer. It errs on the high side, so use it to plan context, not to predict a bill. Each call counts as one request against your rate limit.

Errors

Errors on /v1/messages and /v1/messages/count_tokens use the Anthropic shape, so the Anthropic SDK raises its usual exception classes:

{
  "type": "error",
  "error": {"type": "rate_limit_error", "message": "Request rate limit exceeded for this key.", "code": "rate_limited"},
  "request_id": "..."
}

error.code carries our own code. Other endpoints (/v1/models, /v1/generation) answer in the OpenAI shape even when you call them from the Anthropic SDK. See Errors.