TechCompare LogoTechCompare

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

Use the client-side token counter for pre-request planning and prompt optimization. Trust the API response for billing and production monitoring. For OpenAI and Llama, they should match. For Claude and Gemini, treat the client-side count as an estimate within roughly 10%.

There are two ways to know how many tokens your prompt uses: count them before sending with a client-side tokenizer, or check the usage field in the API response after the request completes. The client-side count is an estimate of the input tokens. The API response is the ground truth, including both input and output tokens. In practice, they should match exactly for input tokens (assuming you're using the same tokenizer the API uses).

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

For OpenAI models, the tiktoken library or gpt-tokenizer npm package gives exact pre-request counts that match the API response. For Llama models, llama3-tokenizer-js gives exact counts. For Claude and Gemini, client-side tokenizers don't have access to the real tokenizer, so any pre-request count is an estimate. The API response always includes the exact count. For cost estimation, use client-side tools for budgeting before you send. For billing and monitoring, use the API response values. If you see a mismatch between your client-side count and the API response for OpenAI models, check that you're using the correct tokenizer for the model (o200k_base for GPT-5 and GPT-4o, cl100k_base for older models).

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 all match the API response exactly because they ship the same tokenizer. Claude and Gemini keep their tokenizers private, so any pre-request estimate lands within about 10%. For budgeting before you send, run the client-side counter. For billing and monitoring, the API response usage field is ground truth.

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. Anthropic and Google don't publish their tokenizers, so every client-side estimate for Claude and Gemini is a ratio-based guess within roughly 10%. For budgeting, that's fine. For anything exact, the per-request usage data in the API response is the only true count for those two providers.