TechCompare LogoTechCompare

Client-side token counter vs API-reported counts: which should you trust?

Use this counter for rough planning, provider preflight endpoints for exact model-specific input counts, and provider usage for billing. Even an exact plain-text count can exclude request formatting and other inputs. Llama 4, Claude, and Gemini estimates have no fixed accuracy guarantee.

There are several ways to know how many tokens a prompt uses: use a matching client-side tokenizer where one is available, call a provider's preflight count endpoint, or check usage after the request completes. Client-side estimates can miss chat templates, tools, images, documents, and other request material. Anthropic's /v1/messages/count_tokens and Google's models.countTokens provide provider-side input counts before generation, while the API response remains the billing record for the complete request, including output tokens.

By TechCompare · Updated

Knowledge area
Best Practices
Practical token optimization advice
Topic focus
Token counter vs API
token-counter-vs-api

How this is calculated

Use a matching tokenizer for a pre-request text count. This counter loads supported OpenAI encodings and llama3-tokenizer-js for Llama 3 only. Llama 4, Claude, and Gemini are estimated. Full requests can add chat-template, tool, image, and other tokens. Provider usage is the source for billing.

Verdict

The two values answer different questions. tiktoken and gpt-tokenizer (o200k_base for GPT-5, cl100k_base for older models) and llama3-tokenizer-js can match their APIs when the request format and tokenizer are the same. Claude and Gemini keep their tokenizers private for offline use, but Anthropic's /v1/messages/count_tokens and Google's models.countTokens can provide provider-side input counts before generation. For billing and monitoring, reconcile against the API response usage field.

More Tokens scenarios

Frequently asked questions

Should I trust a token counter tool or the API's usage numbers?
Both, for different purposes. A client-side counter tells you the cost before you send, which is what you need for prompt optimization and budgeting. The API response usage field is the billing ground truth, including output tokens a pre-request count can't know. Reconcile them the first time you wire a model in, then trust the tool for planning and the API for invoicing.
Why does my token counter disagree with OpenAI's API count?
Tokenizer mismatch, almost always. GPT-5 and GPT-4o use o200k_base while older models use cl100k_base, and counting with the wrong one shifts the total. Make sure your library (tiktoken, gpt-tokenizer) is set to the tokenizer that matches the model you're actually calling, and the pre-request count will match the API exactly.
Can I count Claude or Gemini tokens client-side?
Only approximately unless you have the provider's own tokenizer and request format. For exact input preflight counts, Anthropic provides /v1/messages/count_tokens and Google provides models.countTokens. Use those endpoints before generation when available, then use the per-request usage data in the API response as the billing record, including output tokens that cannot be known in advance.