Token 计数
POST /v1/messages/count_tokens
compatible
语义操作 generate
契约修订 1
状态语义:stateless; served natively against one direct Anthropic connection
查看原始契约偏差(英文)
这些是源码契约中的逐条证据。日常接入先看本页示例、字段表和 Profile 覆盖矩阵;排查兼容性边界时 再展开核对原文。
- 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
- 操作结论:原生模式只替换模型标识,其余字段原样转发;调用方必须确保正文完全符合所选 Provider 的原生协议。 原始契约: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 08af8c8b0b10 的兼容性契约生成
(scripts/generate-api-pages.mjs,当前副本摘要 db249a88ce62e5b7…)。
字段的类型、是否必填与语义说明尚未进入契约,因此本页只列字段名。