Tokenization is where models learn your language
Ask a model to count the letters in "strawberry" and it may stumble - not because counting is hard, but because it never saw the word. It saw tokens. This article builds byte-pair encoding from scratch to show why.
The compression idea
Byte-pair encoding starts from raw bytes and repeatedly merges the most frequent adjacent pair into a new token:
- Count every adjacent pair in the corpus.
- Merge the most frequent pair into one symbol.
- Repeat until the vocabulary reaches its target size.
The result is a vocabulary that is statistically honest about the training corpus - common words become single tokens, rare words decompose into pieces.
Why this matters downstream
- Arithmetic suffers: numbers split into uneven tokens, so digit alignment is lost before the model even starts.
- Non-English text pays more: the same sentence can cost several times more tokens than its English equivalent.
- Spelling tasks are unfair: the model must reconstruct characters it was never shown individually.
Each of these is a tokenization artifact, not a reasoning failure - and knowing the difference changes how you evaluate model behavior.