Lesson 2.6 · 6 of 6 · 6 min read · Reviewed 24 Nov 2026
Lab: compare token counts across tokenizers
By the end, you can
- Measure the same texts with several tokenizers and compare tokens per word.
- Turn the measurements into a decision about budgets and cost estimates.
Lesson 2.5 explained why tokenizersTokenizer: The component that splits text into tokens and maps each token to an id the model can process. differ. This lab-style lesson turns that into numbers you can act on. The hands-on lab below has you run real tokenizers on your own machine; this page explains what to measure and how to read the results.
What to measure
Pick four or five short samples that look like your real traffic:
| Sample | Why include it |
|---|---|
| A paragraph of plain English | The baseline every rule of thumb is based on |
| A block of code | Symbols, indentation and names split differently |
| A table of numbers or ids | Long digit strings often split into many pieces |
| The same paragraph in another language you serve | Shows the language gap |
| A JSON tool result | What agents actually feed the model |
For each sample, record words, characters and the tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. count from each tokenizer. Then compute tokens per word (or per character for languages without spaces).
Where the numbers come from
- Open tokenizers you can run locally: for example
tiktoken, or any model’s tokenizer through the Hugging Facetokenizersandtransformerslibraries. - Hosted models whose tokenizer is not published: use the provider’s token countingToken counting: Measuring how many tokens a request will use before sending it, for example with a token-counting endpoint. endpoint, or read
usage.input_tokensfrom a real response.
Never compare a local estimate for one model with a hosted count for another and call it the same thing. Label every column with the tokenizer it came from.
A worked example
Illustrative results for four samples and three tokenizers (your numbers will differ):
| Sample (words) | Tokenizer A | Tokenizer B | Tokenizer C |
|---|---|---|---|
| English paragraph (100) | 130 | 128 | 140 |
| Python function (60) | 150 | 120 | 175 |
| Order ids table (40) | 160 | 95 | 170 |
| Same paragraph in Greek (95) | 330 | 260 | 410 |
How to read it:
- On English the three agree within about 10%. Rules of thumb are fine here.
- On code and ids the spread is much bigger. Tokenizer B has longer digit and symbol pieces.
- On Greek, tokenizer C needs over 4 tokens per word. Context budgetsToken budget: A planned limit on how many tokens each part of the context may use. and costs for Greek users are roughly three times the English figures.
Turning it into decisions
- Budgets. Set chunkChunking: Splitting documents into smaller passages before embedding and indexing them. sizes and history limits in the target model’s tokens, with margin for the worst sample type you saw.
- Cost estimates. Multiply by the measured tokens per word for each traffic type, not one global ratio.
- Model switches. Re-run the measurement before migratingModel migration: Moving a production system from one model version to another.. A switch can quietly push long prompts over the window.
Common mistakes
- Measuring only English and applying the result to everything.
- Counting with one model’s tokenizer and budgeting for another.
- Forgetting that the system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation., tools and JSON wrappers are tokens too (chapter 3).
Key points
- Different tokenizers give different counts for the same text. The gap is small for plain English and large for code, numbers and many other languages.
- Measure on a sample of your real inputs, not on a rule of thumb. Report tokens per word or per character for each tokenizer.
- Budgets, chunk sizes and cost estimates belong to one model's tokenizer. Re-measure when you switch models.
Compare token counts across tokenizers
Measure the same texts with several real tokenizers and turn the spread into a budgeting decision.
You will have: A table of token counts and tokens per word for four samples across three tokenizers, with a one-paragraph recommendation.
Steps
- Create a project environment (see lesson 1.3) and install tiktoken and transformers.
- Prepare four samples: an English paragraph, a code block, a table of ids and the same paragraph in another language you serve.
- Count tokens with two tiktoken encodings and one Hugging Face tokenizer, such as a WordPiece or SentencePiece model.
- Record words, tokens and tokens per word for every sample and tokenizer.
- Write a short recommendation: which sample type needs the biggest margin in your budgets.
Done when
- The table has a count for every sample and tokenizer, each labelled with the tokenizer's name.
- Tokens per word is computed for each cell.
- The recommendation names the worst-case sample type and the margin to allow.
Docs: tiktoken (GitHub) · Summary of the tokenizers (Hugging Face)
Finished the chapter? Take the chapter check.