Why does a chatbot that writes fluent essays sometimes miscount the r’s in “strawberry”? Why is a context window measured in “tokens” rather than words, and why does the same paragraph cost more in te reo Māori than in English?
The answer to all three is the step that happens before a language model sees anything at all. Your text is cut into pieces called tokens, and each piece is swapped for a number. The model only ever sees that list of numbers. It never sees letters.
On this page you can run two real tokenisers on your own text, train a small one from scratch, and see how the choices baked into a tokeniser show up as cost, fairness and odd behaviour. Everything is computed in your browser. Nothing you type leaves the page.
Live tokeniser
The macron in ā takes two bytes (see the Bytes view). cl100k_base gives “ā” a token of its own; o200k_base has learnt “Nā” and “ nā”, and “ iwi” as one token.
| Encoding and vocabulary | Tokens | Characters per token | Bytes per token |
|---|---|---|---|
| UTF-8 bytes256 | 50 | 0.96 | 1.00 |
| cl100k_base… | … | … | … |
| o200k_base… | … | … | … |
48 characters, 50 UTF-8 bytes
Text is bytes first
Computers store text as bytes, and almost all text on the web uses an encoding called UTF-8. Plain English letters take one byte each. A macron vowel such as ā takes two (C4 81), most Asian scripts take three per character, and an emoji like 🥝 takes four (F0 9F A5 9D). Switch the live tokeniser to Bytes to see this directly.
You could feed a model raw bytes. The vocabulary would be tiny, just 256 possible values, and nothing would ever be unknown. The problem is length. The English preset is 109 bytes but only 22 tokens. A model pays for every position it reads: attention compares each position with every earlier one, and the memory needed grows with the number of positions. Five times as many positions is a much slower, hungrier model.
Modern tokenisers sit in between. They start from bytes, so any text at all can be encoded, and then learn tens of thousands of longer pieces for the byte sequences that appear often. The two you are comparing above are OpenAI’s published vocabularies: cl100k_base, used by GPT-4 and GPT-3.5, has 100,256 ordinary tokens, and o200k_base, used by the GPT-4o family, has 199,998. Both also reserve a handful of special tokens, such as an end-of-text marker.
How a tokeniser learns its vocabulary
Those long pieces are found by an algorithm called byte pair encoding, or BPE. It was a data compression trick before Sennrich, Haddow and Birch adapted it for translation models in 2016, and GPT-2 made the byte-level version standard.
Training is a loop you can do by hand:
- Write the training text as bytes. Each byte is a token with an ID from 0 to 255.
- Count every pair of neighbouring tokens across the whole text.
- Take the most frequent pair, give it the next unused ID (256, then 257, and so on), and replace every occurrence of the pair with the new token.
- Repeat until the vocabulary is the size you want.
Each merge removes one token for every place the pair occurs, so the text shrinks by exactly the pair’s count (a run like “aaa” holds only one non-overlapping “aa”):
Before counting, real tokenisers split the text into chunks with a pattern (words with their leading space, runs of digits, punctuation, whitespace) and never merge across chunks. That is why “ the” with its space is a token, but “e t” spanning two words never is. Our trainer uses GPT-2’s pattern.
Train your own BPE
Six well-known whakataukī. The macron vowel ā starts as two bytes, so one early merge does nothing but rebuild that letter.
| # | Pair | New token | Count |
|---|---|---|---|
| 10 | C4 + 81 | ā | 5 |
| 9 | u + r | ur | 5 |
| 8 | t + a | ta | 5 |
| 7 | i + a | ia | 5 |
| 6 | g + a | ga | 5 |
| 5 | · + m | ·m | 5 |
| 4 | · + k | ·k | 6 |
| 3 | a + n | an | 7 |
| 2 | ·t + e | ·te | 9 |
| 1 | · + t | ·t | 18 |
The tokeniser behind a real model is trained the same way, only on a much larger and more varied sample of text, until it reaches its target size. After that, the vocabulary is frozen. The model is then trained on text that has been cut up by that fixed tokeniser, which is why a model and its tokeniser always travel together.
Same meaning, different cost
BPE rewards whatever is frequent in its training text. Most large training sets are dominated by English and a few other widely published languages, and te reo Māori is a very small share of the web. So English words become whole tokens early, while Māori words are more often stitched together from fragments. The figures below use OpenAI’s vocabularies; the tokeniser in your local model will give different numbers.
Same meaning, different cost
There are two separate causes here. The first is training data share: a word the tokeniser saw rarely stays in pieces. The second is encoding: ā, ē, ī, ō and ū each need two UTF-8 bytes, and if the tokeniser has not learnt merges for those byte pairs in context, a single letter can cost a token of its own, as “ā” does in “Tāmaki” with cl100k_base.
Researchers have measured this across many languages. Petrov and colleagues (2023) found the same text can take up to 15 times as many tokens in one language as in another, and Ahia and colleagues (2023) showed that speakers of many languages pay more per request and get worse results from commercial models.
This matters in practice. Many services charge per token, so the same request costs more in te reo. Context windows are counted in tokens, so fewer whakataukī, letters or documents fit at once. And a model that sees a language in smaller pieces has to spend more of its capacity reassembling words, which is one reason models tend to write less well in languages they saw less of. For Aotearoa, this is part of whether AI tools serve everyone equally. But the answer is not simply to collect more te reo: whose words are used, with what consent and under whose control matters as much. Māori-led groups such as Te Mana Raraunga, the Māori Data Sovereignty Network, and Te Hiku Media, which builds te reo speech tools and keeps its recordings under its own Kaitiakitanga licence, set out how that can be done.
Quirks that come from tokens
Once you know the model sees IDs, several famous failures stop being mysterious.
Tokeniser quirks
Letter counting. The model receives one number for “ strawberry”. Any knowledge of the letters inside it has to be learnt indirectly, from places where the word happened to be spelled out. Capitalisation makes it worse: “ STRAWBERRY” is four different tokens again.
Arithmetic. Both vocabularies split digit runs into groups of up to three, starting from the left. So 12345 is “123” then “45”, and 67890 is “678” then “90”. To add them, the model has to work out that the “45” and “90” are the units columns from their position, with no explicit place value in the IDs. Models do learn this, but it is a needless obstacle; some tokenisers split numbers into single digits for this reason.
Whitespace in code. GPT-2’s tokeniser spent a token on every space of indentation; newer vocabularies have tokens for runs of spaces, which is part of why they handle code better (try the Python preset above).
Rare and glitch tokens. A token can end up in the vocabulary even if it is rare in the text the model is later trained on. Its embedding, the row of numbers the model looks up for that ID, then barely gets trained. The best documented case is “ SolidGoldMagikarp”, a single token in the GPT-2 and GPT-3 vocabulary that made GPT-3 models behave erratically when asked to repeat it. In both newer vocabularies here it is five ordinary tokens.
Vocabulary size is a trade-off. A bigger vocabulary means shorter sequences, as you saw with o200k_base and te reo. But every token needs a row in the model’s embedding table and a slot in its output layer, and each prediction is a choice over the whole vocabulary. Rare tokens also see fewer training examples each. Doubling the vocabulary does not halve the token count; the BPE curve above shows why.
What this means for local AI
When you run a model on your own computer, the tokeniser ships with it (inside a GGUF file, or as tokeniser files beside the weights), and different families use different ones. Llama 2 used a 32,000-token SentencePiece vocabulary. Llama 3 switched to a tiktoken-style BPE with 128,000 ordinary tokens (about 100,000 taken from OpenAI’s tiktoken vocabulary plus about 28,000 added for other languages) and 256 reserved special tokens, for 128,256 in all. The same prompt therefore has a different token count in every model family. Counts from this page are only a guide for Llama 3; for exact numbers, use the model’s own tokeniser.
Token counts matter on your own hardware in three ways.
- Context length is counted in tokens. A “128K context” means 131,072 tokens, not words. If your language or your code tokenises badly, you fit less.
- Memory grows with every token. Every token in your prompt costs memory while the model works: about 128 KiB per token for Llama 3.1 8B at f16, held in what is called the KV cache. How attention decides what matters shows why the cache exists, and Will it run on my machine? sizes it for your hardware.
- Speed scales with tokens. Your prompt has to be processed before the first word appears, and each new token is another pass through the whole model. Fewer tokens for the same text means a faster answer.
The vocabulary itself also costs memory. In Llama 3.1 8B the input embedding table alone is 128,256 tokens × 4,096 numbers, about 525 million parameters, and the output layer is the same size again. That is roughly an eighth of the model spent on the vocabulary.
What we simplified
- The trainer is tiny. Real tokenisers are trained on gigabytes of text, and use faster incremental pair counting. The algorithm is the same; the scale is not.
- Tie-breaking. When two pairs are equally frequent, our trainer picks the one with the lowest IDs so the result is reproducible. Other implementations break ties differently and get slightly different merges.
- Pre-tokenisation. Our trainer uses GPT-2’s splitting pattern.
cl100k_baseando200k_baseuse newer patterns (for example, digits in groups of up to three, and case-aware rules ino200k_base), which you see in the live tokeniser. - Characters are counted as Unicode code points. Some things that look like one character, such as flags or accented letters typed as two code points, count as more.
- Other tokenisers. Many models use SentencePiece or WordPiece rather than byte-level BPE. They differ in details, such as how they mark spaces, but the cost and fairness effects are the same in kind.
- Translations. Whakataukī are poetic and their English renderings are interpretive, so the language comparison is indicative, not a precise measure.
Next: a token ID is just a row number. The model’s first step is to look up that row in its embedding table, a list of numbers that places each token in a space where related tokens sit close together. Meaning as a map shows what that space looks like.
After that comes the other end of the model. Tokens are also what a model writes: at every step it gives a score, called a logit, to every token in its vocabulary, which for cl100k_base means 100,256 scores. How a model picks the next token shows how one of them is chosen.
Inspired by, and worth your time
- Tiktokenizer by David Duong
- Let's build the GPT Tokenizer by Andrej Karpathy
- tiktoken by OpenAI