primitive
tokenization
How raw text becomes the integer IDs models actually see β BPE, WordPiece, SentencePiece β and why tokenizer quirks explain so many otherwise-baffling LLM failures.
A language model never sees text. It sees a sequence of integers, each naming an
entry in a fixed vocabulary, and everything downstream β embeddings,
attention, the output headβs prediction β operates on those IDs.
Tokenization is the deterministic preprocessing step that chops raw bytes into
that sequence, and it embodies a trade-off: characters give a tiny vocabulary but
absurdly long sequences; whole words give short sequences but an unbounded
vocabulary that canβt handle typos or new words. Subword tokenization splits the
difference β common words stay whole, rare words decompose into recognizable
pieces (tokenization β token + ization).
It is also the layer where many famous LLM failures actually live. A model that can write poetry but canβt count the letters in βstrawberryβ or reverse a string isnβt bad at reasoning β it literally cannot see characters, only opaque chunks.
How it works
Byte-pair encoding (BPE), the dominant algorithm, is learned from a corpus before model training ever starts:
- Start with a base vocabulary of single bytes (so anything is representable).
- Count which adjacent pair of tokens occurs most often; merge it into a new vocabulary entry.
- Repeat until the vocabulary reaches a target size (typically 32kβ200k entries).
Encoding new text replays those merges in order. The result is a frequency-driven vocabulary: common English words become single tokens, rare strings shatter into many. WordPiece (used by BERT) scores merges by likelihood instead of raw frequency, and SentencePiece runs directly on raw text so the same machinery works for languages without spaces.
The token IDs then index an embedding table β tokenization decides what the atoms are; embeddings decide what they mean.
Why tokenizer quirks matter
- Character-level blindness β spelling, rhyming, counting letters, and
arithmetic all suffer because the relevant structure is hidden inside tokens
(numbers split inconsistently:
2023might be one token,2024two). - Multilingual inequity β a vocabulary trained mostly on English spends few tokens per English sentence and many per sentence of an underrepresented language, making the same content slower, costlier, and effectively shrinking the context window.
- Glitch tokens β vocabulary entries whose training data was filtered out
(the infamous
SolidGoldMagikarp) leave embeddings essentially untrained, and prompting them produces erratic behavior. - Whitespace sensitivity β
helloandhelloare different tokens, which is why trailing spaces in prompts can measurably change completions.
Related concepts
- embeddings β the next stage: token IDs become vectors; tokenization defines the rows of that table
- large language models β predict the next token, not the next word; every context-window and pricing number is denominated in tokens
- BERT β uses WordPiece tokenization; its masked objective masks tokens, not words
- vision transformer β the same move for images: patches are visual tokens