Should you trust a client-side token count or the one the API reports?

By TechCompare · Updated · How we check

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 and Claude 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.

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 the OpenAI encodings, the Gemma tokenizers Google uses for Gemini, and llama3-tokenizer-js for Llama 3. Llama 4 and Claude are estimated. Full requests can add chat-template, tool, image, and other tokens. Provider usage is the source for billing.

Verdict

Use the API response usage field for billing and monitoring, and a matching client-side tokenizer for planning. 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 keeps its tokenizer private, while Gemini text can be counted offline with the open Gemma tokenizers.

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 text count should line up with the API's count for that text.
Can I count Claude or Gemini tokens client-side?
Gemini text, yes: Google's SDK counts it offline with the open Gemma tokenizers, and this counter runs them too. Claude only approximately, since Anthropic doesn't publish its tokenizer. 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.