EZAT RouterDocs
API 参考

Count tokens

POST
/v1/messages/count_tokens

Counts the input tokens a Messages request would use, without running the model. It is free and isn't billed or listed in your usage, but it counts toward your key's requests per minute.

The body is the same as for creating a message. model and a non-empty messages are required. max_tokens and stream are accepted and ignored. The anthropic-beta header is not passed on.

Authorization

headerx-api-key<token>

Your EZAT Router key (ezat_sk_…) in the x-api-key header, as the Anthropic SDKs send it. Accepted on every /v1 and /openai/v1 endpoint.

Header Parameters

anthropic-version?string

Accepted for compatibility with Anthropic's SDKs, which send it, but not used. You don't need to send it.

x-request-id?string

Your own id for this request, echoed in the x-request-id response header (and as request_id in Anthropic-format error bodies). Used only if it is 1 to 128 characters of letters, digits and ._:-; otherwise the gateway makes a new UUID.

Match^[A-Za-z0-9._:-]{1,128}$

Request Body

application/json
  1. body

The same body as for creating a message. model and messages are required; max_tokens and stream are ignored.

model*ModelName

A canonical model id (anthropic/claude-sonnet-5-5) or an alias (claude-sonnet-5-5, claude-sonnet-5.5), in any letter case. Surrounding spaces are ignored.

Length1 <= length <= 128
messages*array<>
Items1 <= items
system?|array<>

The system prompt, as a string or an array of text blocks.

tools?array<>
thinking?
[key: string]?any

Response Body

The number of input tokens.

application/json
  1. response
input_tokens*integer

Tokens in the system prompt, tools and messages.

Range0 <= value
Example Request
curl -X POST 'https://api.router.ezat.io/v1/messages/count_tokens' \  -H "x-api-key: $EZAT_API_KEY" \  -H 'Content-Type: application/json' \  -d '{  "model": "anthropic/claude-sonnet-5-5",  "system": "You are a concise assistant.",  "messages": [    {      "role": "user",      "content": "Hello, what can you do?"    }  ]}'
Example Response
{  "input_tokens": 23}

Create a message POST

Sends a conversation to a model and returns its reply, in Anthropic's Messages format. Set `"stream": true` to receive the reply as Server-Sent Events. The gateway checks `model`, `max_tokens`, `messages` and `stream`. Every other field (`system`, `tools`, `thinking`, `output_config`, `cache_control` and the rest) is passed to the model unchanged, and the model checks it as Anthropic's API would. Two exceptions: - The gateway removes prompt-cache breakpoints (`cache_control`) for models whose `capabilities.promptCaching` is `false` in `GET /api/models`, because their cache is never read back. Those requests are billed as plain input instead of a cache write. - The `anthropic-version` header is accepted but not used. The response's `model` (and `message.model` in `message_start`) is the name you sent, not the canonical id.

Create a chat completion POST

Sends a conversation to a model and returns its reply, in OpenAI's Chat Completions format. Set `"stream": true` to receive `chat.completion.chunk` events. The request is translated to Anthropic's Messages format and runs through the same gateway as `POST /v1/messages`: the same limits, routing, failover and prices. - **Messages.** `system` and `developer` messages are joined into the system prompt, wherever they appear. Assistant `tool_calls` and `tool` messages become tool calls and tool results, and consecutive messages from the same side are merged. - **Images and PDFs.** `image_url` parts accept base64 data URLs and `http(s)` URLs, which the gateway downloads (JPEG, PNG, GIF or WebP; public hosts only; up to 10 MB per image, 32 MB and 20 URLs per request; 15 seconds and 3 redirects per download). PDFs go in `file` parts as a base64 `file_data` data URL. - **Prompt caching is automatic.** When the model supports caching and the request marks no breakpoints of its own, the gateway adds up to two: one at the end of the system prompt (or of the tools, when there is no system prompt) and one on the newest message, which the next turn reads back. Send `x-ezat-cache: off` to opt out. To place breakpoints yourself, add Anthropic's `cache_control` to content parts or tools. - **Refused with `400`**, because they would change the result and have no equivalent: `n` other than 1, `logprobs`, `top_logprobs`, a non-empty `logit_bias`, audio (`modalities` other than text, `audio`, `input_audio` parts), `web_search_options`, the legacy `functions` and `function_call`, the `function` role, `response_format` of type `json_object`, and `file_id`. - **Ignored:** every field not listed in the request schema, such as `user`, `seed`, `presence_penalty`, `frequency_penalty`, `store`, `metadata` and `service_tier`. - **Not returned:** the model's thinking text. Thinking tokens are counted in `usage`.