Token 计数
POST /v1/messages/count_tokens
compatible
语义操作 generate
契约修订 1
状态语义:stateless; served natively against one direct Anthropic connection
已声明的偏差
Section titled “已声明的偏差”逐条来自契约,不是补充说明:
- only the direct Anthropic Messages provider profile serves this endpoint; Bedrock Mantle shares the Messages wire format but its count_tokens surface is not established
- the request body reaches the provider verbatim with only the upstream model identifier substituted, so there is no portable execution mode to select
- Anthropic does not bill count_tokens, and Halro settles it at zero cost — but it is a real provider call on the operator’s credential, so it takes a ledger attempt and appears in the audit trail like any other
- the reported count is Anthropic’s, for the upstream model behind the alias; it is not re-derived by Halro
在真正发出请求之前估算 token 数。这个端点不支持流式(stream 会被拒),
也不接受 Halro-Route-Mode: portable。
curl https://halro.example.com/v1/messages/count_tokens \ -H "x-api-key: $HALRO_GATEWAY_KEY" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "claude", "messages": [{"role": "user", "content": "你好"}] }'import osfrom anthropic import Anthropic
client = Anthropic(base_url="https://halro.example.com", api_key=os.environ["HALRO_GATEWAY_KEY"], timeout=60.0, max_retries=0)count = client.messages.count_tokens( model="claude", messages=[{"role": "user", "content": "你好"}],)print(count.input_tokens)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://halro.example.com", apiKey: process.env.HALRO_GATEWAY_KEY, timeout: 60_000, maxRetries: 0,});const count = await client.messages.countTokens({ model: "claude", messages: [{ role: "user", content: "你好" }],});console.log(count.input_tokens);x-api-key or Authorizationanthropic-versionanthropic-betaContent-Type
| 字段 | 不支持该字段的 Provider Profile | 组合级限制 |
|---|---|---|
model | — | — |
messages | — | — |
messages[].role | — | — |
messages[].content | — | — |
system | — | — |
tools | — | — |
tool_choice | — | — |
thinking | — | — |
output_config | — | — |
门面层直接拒绝的字段
Section titled “门面层直接拒绝的字段”这些字段在到达任何 Provider 之前就会被拒。照搬 OpenAI 文档最容易撞上的就是它们:
streamHalro-Route-Mode: portableunknown top-level fields
input_tokens
可路由的 Provider Profile 及各自成熟度
Section titled “可路由的 Provider Profile 及各自成熟度”| Provider Profile | 成熟度 | 不支持的请求字段 |
|---|---|---|
anthropic.messages.2023-06-01 | compatible | 未声明 |
已声明的变换
Section titled “已声明的变换”anthropic.messages.2023-06-01
- only the model identifier is substituted; every other member is forwarded unchanged
已验证的证据:gateway_contract、provider_transport_fixture、sdk_blackbox
SDK 黑盒协议桩矩阵
Section titled “SDK 黑盒协议桩矩阵”anthropic-go、anthropic-typescript、anthropic-python
本页的字段表、覆盖矩阵与偏差列表由 Halro 7117fdc38491 的兼容性契约生成
(scripts/generate-api-pages.mjs,契约摘要 9f8a5c57f76ae870…)。
字段的类型、是否必填与语义说明尚未进入契约,因此本页只列字段名。