Skip to content

估算 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

鉴权

以下方案任选其一:

  • AnthropicApiKey
  • BearerAuth

请求

请求体:application/json

json
{"$ref":"#/components/schemas/AnthropicCountTokensRequest"}

响应

状态说明Schema
200成功{"$ref":"#/components/schemas/AnthropicCountTokensResponse"}
400本地请求错误或供应商同状态响应{"$ref":"#/components/schemas/AnthropicError"}
401API Key 无效或供应商同状态响应{"$ref":"#/components/schemas/AnthropicError"}
500平台内部错误或供应商同状态响应{"$ref":"#/components/schemas/AnthropicError"}

流式行为

non-streaming

错误

  • invalid_api_key
  • invalid_request_error
  • internal_error

限制

  • 仅受全局请求体上限约束
  • 本地粗估只用于客户端兼容且不计费

兼容性

  • inventory:contract-anthropic-count-tokens-general
  • protocol: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\":\"你好\"}]}"

Token Factory · 国产合规 AI Gateway