切换外观
估算 Anthropic 输入 Token
仅校验 API Key,不进入消息计费链。
POST/v1/messages/count_tokens- 鉴权与权益
- api-key-only
- 入口语境
- general-balance
POST /v1/messages/count_tokens 接口
方法:
POST路径:
/v1/messages/count_tokens入口家族:
general-balance计费语境:
balance调用前置:
api-key-only
鉴权
以下方案任选其一:
AnthropicApiKeyBearerAuth
请求
请求体:application/json
json
{"$ref":"#/components/schemas/AnthropicCountTokensRequest"}响应
| 状态 | 说明 | Schema |
|---|---|---|
| 200 | 成功 | {"$ref":"#/components/schemas/AnthropicCountTokensResponse"} |
| 400 | 本地请求错误或供应商同状态响应 | {"$ref":"#/components/schemas/AnthropicError"} |
| 401 | API Key 无效或供应商同状态响应 | {"$ref":"#/components/schemas/AnthropicError"} |
| 500 | 平台内部错误或供应商同状态响应 | {"$ref":"#/components/schemas/AnthropicError"} |
流式行为
non-streaming
错误
invalid_api_keyinvalid_request_errorinternal_error
限制
仅受全局请求体上限约束本地粗估只用于客户端兼容且不计费
兼容性
inventory:contract-anthropic-count-tokens-generalprotocol:anthropic-compatible
可执行示例
cURL
sh
curl --request POST "${TOKENFACTORY_API_BASE_URL}/v1/messages/count_tokens" --silent --show-error --fail-with-body --max-time 60 --header "x-api-key: ${TOKENFACTORY_API_KEY}" --header "anthropic-version: 2023-06-01" --header "Content-Type: application/json" --data "{\"model\":\"${TOKENFACTORY_MODEL_ID}\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}"