Tokenization in LLMs
/AI/4 min read
A model never sees text, only token IDs, and the split decides what it costs and what it can do. At 1.3 tokens per word a 128k window holds 100,824 words; at 4 tokens per word — common for languages the tokenizer under-trained on — it holds 32,768, at 3.08× the price.
A language model does not read characters or words. Before any text reaches it, a tokenizer cuts the text into pieces from a fixed vocabulary and replaces each piece with an integer. The model reads integers and writes integers; the tokenizer turns them back into text at the end.
"Tokenization is surprisingly important." might become something like:
["Token", "ization", " is", " surprisingly", " important", "."] → [3404, 2065, 374, 29392, 3062, 13]
Common words are single tokens. Rarer words are split into familiar pieces. Spaces are usually attached to the following word. Everything the model knows, it knows in terms of these pieces.
Why not characters, or words?
Characters give a tiny vocabulary and can spell anything, but sequences become five times longer. Attention cost grows with the square of length, and the model has to learn to assemble meaning from letters before it can do anything else.
Words give short sequences, but the vocabulary is unbounded — every name, typo, number, and inflection is a new word — and anything not in the list cannot be represented at all.
Subwords sit between: a vocabulary of tens of thousands of pieces that covers common words whole and builds rare ones from parts. Algorithms like byte-pair encoding learn the pieces from data by repeatedly merging the most frequent adjacent pair. Working at the level of bytes means any string at all can be encoded, even if inefficiently.
Tokens are the unit of everything
Context limits, prices and speeds are all quoted in tokens, so how many tokens a piece of text becomes matters directly.
For English, a typical modern tokenizer produces about 1.3 tokens per word. For a language that was rare in the tokenizer's training data, the same sentence can take several times as many tokens, because words are broken into small, generic fragments.
| tokens per word | words in a 131,072-token window | relative cost |
|---|---|---|
| 1.3 | 100,824 | 1.00× |
| 2.0 | 65,536 | 1.54× |
| 4.0 | 32,768 | 3.08× |
The same model, the same advertised context and the same per-token price give a user writing in an under-represented language a third of the context at three times the cost. They also get slower output — generation speed is tokens per second — and often worse quality, since meaning is spread across more, less informative pieces. Larger, more multilingual vocabularies in newer tokenizers are largely a response to this.
The vocabulary-size trade
A bigger vocabulary means fewer tokens per text, but every entry needs a row in the input embedding matrix and a row in the output layer. At a model width of 4,096:
| vocabulary | embedding + output parameters |
|---|---|
| 32,000 | 262M |
| 128,000 | 1,049M |
| 256,000 | 2,097M |
For a 70B model, a billion parameters of vocabulary is a small price for shorter sequences across many languages. For a 1B model it would be most of the model. Vocabulary size is chosen with the model size in mind.
The odd failures tokenization explains
Several well-known LLM weaknesses come from the model never seeing letters.
Spelling and counting letters. Asked how many r's are in "strawberry", the model sees perhaps ["str", "aw", "berry"]. The letters are inside the tokens, not visible as separate symbols. It has to have memorised the spelling of each token.
Arithmetic. Depending on the tokenizer, "1234567" might be split as "123" "456" "7" or into single digits. If the chunks do not line up with place value, carrying digits across chunk boundaries is hard. Tokenizers that split numbers into individual digits make arithmetic noticeably more reliable.
Leading spaces. " hello" and "hello" are usually different tokens. A prompt ending in a trailing space can push the model toward an unusual continuation, because in training text a space was almost always attached to the word after it.
Code indentation. Runs of spaces can be single tokens or many, depending on whether the tokenizer learned them. Tokenizers trained with lots of code handle indentation compactly; others spend tokens on whitespace.
What to take away
Tokenization turns text into integers from a fixed vocabulary of subword pieces, and the model only ever sees those integers. Every limit and price is in tokens, so the tokens-per-word rate is effectively an exchange rate: at 1.3 a 128k window holds about 100,824 words, at 4.0 it holds 32,768, at 3.08× the cost.
Many puzzling behaviours — miscounting letters, shaky arithmetic, sensitivity to trailing spaces — are a model reasoning about text it has only seen in chunks.