Skip to content
API referenceToken counting

Token counting

Free preliminary token counting for Claude and Kimi.

Model protocol

POST /v1/messages/count_tokens counts Claude input with and without a server prompt. It uses the model tokenizer for the prepared request. The result is an input_tokens count.

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",
    "messages": [{"role": "user", "content": "How many tokens is this?"}]
  }'
json
{ "input_tokens": 56 }

Kimi, Composer and Grok

POST /v1/messages/count_tokens works for Claude and Kimi models and returns the input token count. A request with a Composer or Grok id gets a 404: this endpoint does not know them.

  • 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 uses 32,000 tokens.
  • Composer and Grok. No pre-count. Actual consumption arrives in the response usage; the context window is 200K for Composer and 500K for Grok.

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 }

Documentation sections

On this page