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.
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
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 toolLLM VRAM Calculator
Calculate the VRAM needed to run or fine-tune any LLM at any quantization.
Use toolJSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use tool