Count tokens for a message
POST /v1/messages/count_tokens on BigModel.io counts the input tokens of an Anthropic Messages request without generating a reply. Anthropic groups only.
POST
https://bigmodel.io/v1/messages/count_tokensCounts the input tokens an Anthropic Messages body would use, without generating a reply and without recording usage. Only Anthropic groups support it; OpenAI groups and chat-only groups (DeepSeek, GLM platforms) return 404.
#Request
http
POST https://bigmodel.io/v1/messages/count_tokens#Authentication
| Header | Value |
|---|---|
Authorization |
Bearer $BIGMODEL_API_KEY |
x-api-key |
$BIGMODEL_API_KEY |
Send one of these.
#Body
An Anthropic Messages body; max_tokens is not needed.
| Field | Type | Required | Description |
|---|---|---|---|
model |
string | Yes | Model ID. |
messages |
array<object> | Yes | The messages to count. |
system |
string | array<object> | ||
tools |
array<object> |
#Response
| Status | Meaning |
|---|---|
200 |
Token count, as returned by the upstream. |
400 |
api_key_in_query_deprecated (key sent in the query string), or invalid_request_error: empty or invalid JSON, missing model, a non-boolean stream, or another malformed field. |
401 |
API_KEY_REQUIRED, INVALID_API_KEY, API_KEY_DISABLED, USER_NOT_FOUND, USER_INACTIVE. |
403 |
INSUFFICIENT_BALANCE, API_KEY_EXPIRED, ACCESS_DENIED (IP rule), SUBSCRIPTION_NOT_FOUND, SUBSCRIPTION_INVALID, or a permission_error (key without a group, endpoint not allowed for the group). |
404 |
Token counting is not supported for this key group. |
413 |
Request body too large. |
429 |
API_KEY_QUOTA_EXHAUSTED, USAGE_LIMIT_EXCEEDED (plan limit), or rate_limit_exceeded / rate_limit_error (key 5h/1d/7d limit, requests per minute, concurrency, upstream). RPM limits send Retry-After. |
503 |
api_error and similar: no capacity for this model in your group right now, or the model is not served by your group (No available accounts). |
#Response fields
| Field | Type | Description |
|---|---|---|
input_tokens |
integer | Input tokens the request would use. |
#Examples
cURL
curl https://bigmodel.io/v1/messages/count_tokens \
-H "Authorization: Bearer $BIGMODEL_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3",
"system": "You are a concise assistant.",
"messages": [{"role": "user", "content": "Explain HTTP 429 in one sentence."}]
}'Python
import os
from anthropic import Anthropic
client = Anthropic(base_url="https://bigmodel.io", api_key=os.environ["BIGMODEL_API_KEY"])
count = client.messages.count_tokens(
model="glm-5.3",
system="You are a concise assistant.",
messages=[{"role": "user", "content": "Explain HTTP 429 in one sentence."}],
)
print(count.input_tokens)Node.js
import Anthropic from '@anthropic-ai/sdk'
const client = new Anthropic({baseURL: 'https://bigmodel.io', apiKey: process.env.BIGMODEL_API_KEY})
const count = await client.messages.countTokens({
model: 'glm-5.3',
system: 'You are a concise assistant.',
messages: [{role: 'user', content: 'Explain HTTP 429 in one sentence.'}],
})
console.log(count.input_tokens)Example response (the count is illustrative):
json
{"input_tokens": 21}#Notes
- Not available on every group. OpenAI groups and chat-only groups return
404with{"type":"error","error":{"type":"not_found_error","message":"Token counting is not supported for this platform"}}. For those groups, readusagefrom the actual response instead. - The count comes from the upstream, so it reflects that model's tokenizer.
- Billing checks still apply. Counting is not charged, but like other text endpoints it needs a positive balance or an active plan (
403INSUFFICIENT_BALANCEotherwise). - An empty or invalid body returns
400invalid_request_error; an oversized one413. Full list: Errors.
#Related
Was this page helpful?
Updated · View as Markdown