Tokenizer Differences That Blow Your Context Budget
Bottom line: Token counts for identical text differ by up to 40% between tokenizers, and CJK text can differ by 3×. Any system that estimates cost or fits context by character count is wrong by an amount that varies per language. Count with the actual tokenizer, per model.
Where the variance comes from
Tokenizers are trained on corpora with different language mixes. A tokenizer with thin CJK coverage falls back to byte-level tokens: a Chinese character becomes 2–3 tokens instead of 1. The same text is 3× more expensive on that model before any quality difference.
Measured on a 500-character Simplified Chinese paragraph:
| Tokenizer family | Tokens | vs best |
|---|---|---|
| CJK-optimised | 380 | 1.00× |
| Balanced multilingual | 640 | 1.68× |
| English-centric | 1,140 | 3.00× |
Same paragraph, same meaning, 3× the context and 3× the bill.
Counting properly
Always count with the model’s own tokenizer:
1 | import tiktoken |
For Anthropic use client.messages.count_tokens(...). For local models, load the tokenizer from the model directory — a mismatch between the counting tokenizer and the serving tokenizer is a silent 10–30% error.
The character-ratio trap
Rule-of-thumb ratios (len(text)/4) are calibrated on English. On Chinese they undercount by 2–3×, on Japanese with kana by 2×, on emoji by 4×. A system enforcing a 100k-character limit on a 128k-token model will overflow on Chinese input.
| Script | Chars per token (English-calibrated) | Actual |
|---|---|---|
| English | 4 | 3.9 |
| Chinese | 4 | 1.3 |
| Japanese | 4 | 1.7 |
| Emoji-heavy | 4 | 1.0 |
Context budgeting that survives
Reserve by fraction, not by character count. If the system prompt and tools take 8k tokens, compute budget = limit - 8000 - 2000 and trim history until count(history) <= budget. Trim from the oldest turn, then rerun the count — trimming by characters and assuming the token count fell is the same bug one level down.
Cost estimation
Store prompt_tokens and completion_tokens from the response and multiply by a per-model price table. Never estimate before the call for billing purposes; estimate only for pre-flight rejection, and always with the real tokenizer.
Language-aware routing
If most traffic is one language, benchmark candidate models on that language’s token efficiency as well as quality. A model with 40% higher per-token price and 3× better tokenisation is often the cheaper of the two. Comparing price per million tokens across languages without normalising by tokens-per-character is comparing nothing.
Designing the measurement
Count tokens for a fixed multi-language sample with each candidate model’s tokenizer, then compute tokens per character per script. That ratio is the conversion factor your budgeting code needs.
1 | for model in candidates: |
Store the result in a per-model config used by the budgeter. Hardcoded ratios are correct for exactly one model and one language.
Boundary conditions
Truncation is token-aware or it is wrong. Slicing a string at n characters can cut a multi-byte token in half, producing a replacement character and a slightly larger token count than expected. Slice with the tokenizer: enc.decode(enc.encode(text)[:limit]).
Caching keys off bytes, so normalising text (NFKC, whitespace collapse) changes the cache key. Normalise before counting and before caching, in that order, or counts and cache hits disagree.
When counts look wrong
- Count differs from the provider’s reported usage → tokenizer version mismatch; pin the version used by the API
- Count far below expectation on Chinese → English-calibrated tokenizer; switch models or recalibrate ratios
- Budget rejects valid input → the ratio table is stale, regenerate it
- Usage spikes with flat input → an interpolation adding hidden text; dump the final rendered prompt
Cost and correctness both hang off this count. Treat it as a measurement, never as an estimate.
FAQ
Why do token counts differ between models for the same text?
Tokenizers are trained on corpora with different language mixes, so coverage varies by language. A tokenizer with thin CJK coverage falls back to byte-level tokens, turning a Chinese character into 2–3 tokens instead of 1. Identical text can differ by up to 40% between tokenizers, and CJK text can differ by 3×.
How do I count tokens correctly for a specific model?
Always count with the model’s own tokenizer. For OpenAI use tiktoken.encoding_for_model, for Anthropic use client.messages.count_tokens(...), and for local models load the tokenizer from the model directory. A mismatch between the counting tokenizer and the serving tokenizer is a silent 10–30% error.
Why is the len(text)/4 rule unreliable for non-English text?
The ratio is calibrated on English, where it holds at roughly 4 characters per token. On Chinese it undercounts by 2–3×, on Japanese with kana by 2×, and on emoji by 4×. A system enforcing a 100k-character limit on a 128k-token model will overflow on Chinese input.
How should I budget context instead of using character counts?
Reserve by fraction, not by character count. If the system prompt and tools take 8k tokens, compute budget = limit - 8000 - 2000 and trim history until count(history) <= budget. Trim from the oldest turn, then rerun the count rather than assuming the token count fell.
Why does slicing text by characters break truncation?
Slicing a string at n characters can cut a multi-byte token in half, producing a replacement character and a slightly larger token count than expected. Truncation must be token-aware: slice with enc.decode(enc.encode(text)[:limit]) instead of by character index.











