Why not just letters or words?
You could give every letter its own number. Then the vocabulary is small, but texts become very long, and the model has to learn to put letters together into words by itself. You could also give every word its own number. Then texts are short, but the vocabulary becomes huge, and any word that was not included from the start cannot be read at all.
Modern tokenizers take a middle path: subword tokens. Common words such as “and” and “the” become one token, while rare words are split into smaller pieces the model knows. A new word can always be written with those pieces, so nothing is completely unknown.
How does a tokenizer learn its pieces?
The most widely used method is byte pair encoding, BPE. You start from single characters, count which two neighbouring characters appear together most often, merge them into a new token, and repeat. The slider in the demo sets how many of those merges the tokenizer has learned. At 0 it is a pure character tokenizer. The more merges, the larger the vocabulary and the fewer tokens per text. See BPE step by step.
A tokenizer is trained before the model is trained, and it does not change afterwards. The model learns a vector for every token number in the vocabulary.
Unknown characters and bytes
The demo’s tokenizer starts from the alphabet, the digits and the characters in its training text. Type anything else, such as an emoji, and it becomes [UNK], “unknown”. Older tokenizers had that problem. Most newer tokenizers for language models work on bytes instead: they start from the 256 possible bytes, so any text can be written, including emojis and rare scripts.
Why does it matter to you?
- Cost and length: language models count in tokens. Both their context window and the price at many providers are measured in tokens, not words.
- Other languages often cost more: a tokenizer trained mostly on English often splits words in other languages into more pieces. The same meaning can take more tokens in, say, Danish.
- Spelling and numbers: the model sees pieces, not letters. That is one reason language models can struggle to count the letters in a word or to calculate with long numbers.
Try real tokenizers on your own text
In the free Tokenization lab you see how real language models’ tokenizers split your text, compare them, and train BPE yourself right in the browser.