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
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
Related guides
Frequently asked questions
Should I trust a token counter tool or the API's usage numbers?
Why does my token counter disagree with OpenAI's API count?
Can I count Claude or Gemini tokens client-side?
Related tools
LLM API Pricing Calculator
Compare API costs across major models (OpenAI, Anthropic, Google) with prompt caching.
Use tool ➜LLM VRAM Calculator
Calculate the VRAM needed to run or fine-tune any LLM at any quantization.
Use tool ➜JSON Formatter
Validate, format, and minify JSON data with readable output and error detection.
Use tool ➜