Skip to content
API referenceToken counting

Token counting

Free input token counting and its limits for the selected model.

Model protocol

POST /v1/messages/count_tokens counts full Claude input with and without a server prompt. When a prompt is applied, its tokens are already part of the returned input_tokens; the prompt text is not exposed.

bash
curl https://api.guardrelay.ai/v1/messages/count_tokens \
  -H "x-api-key: $GUARD_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5-5",
    "messages": [{"role": "user", "content": "How many tokens is this?"}]
  }'
json
{ "input_tokens": 56 }

Codex: exact pre-count

Kimi, Composer and Grok

POST /v1/messages/count_tokens works for Claude, Kimi, and GLM, and also for Gemini and Muse (for them it is a free approximate estimate). GLM also supports /glm/v1/messages/count_tokens; internal count-only fallback 5.2 is not a public model. Composer and Grok receive 404.

  • Kimi. Free counting is available at /v1/messages/count_tokens for all four IDs. It does not replace the generation reserve: set an explicit max_tokens, or the output reserve is computed at Kimi's default: 131,072 tokens for k3 and k3-256k, 32,768 for kimi-for-coding-highspeed, 32,000 for kimi-for-coding; if the balance cannot cover that reserve with room for 8 more regular requests, 32,000.
  • Composer and Grok. No pre-count. Actual consumption arrives in the response usage; the context window is 200K for Composer, 256K in Guard for Grok 4.7, and 500K for Grok 4.6.

Counting Kimi tokens

All four Kimi IDs support free POST /v1/messages/count_tokens. Send the model and messages; the response contains input_tokens. Generation usage is reported separately in the billed response.

bash
curl -s -X POST https://api.guardrelay.ai/v1/messages/count_tokens \
  -H "Authorization: Bearer $GUARD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "k3",
    "messages": [{"role": "user", "content": "How many tokens is this?"}]
  }'
json
{ "input_tokens": 14 }

Free GLM count_tokens

POST /v1/messages/count_tokens and /glm/v1/messages/count_tokens accept a Messages body and do not debit the balance.

bash
curl https://api.guardrelay.ai/glm/v1/messages/count_tokens \
  -H "Authorization: Bearer $GUARD_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"glm-5.3","messages":[{"role":"user","content":"Count this input"}]}'
json
{"input_tokens": 14, "model": "glm-5.3"}

Flash uses its native tokenizer. The messages content is checked against the same media matrix as generation.

Gemini token counting

POST /v1/messages/count_tokens works for Gemini free of charge. The count before an answer returns an approximate estimate of the whole input (system instructions, tools, and the server prompt when it applies), not an exact Google count. It is a reference number: the charge follows the actual usage, not the estimate.

Muse token counting

POST /v1/messages/count_tokens works for Muse as well. The free count before an answer returns an approximate estimate, not an exact Meta count. It is a reference number: the charge follows the actual usage, not the estimate.

Sections

On this page