Models read subword chunks, not whole words. Byte-pair encoding learns them by repeatedly merging the most frequent adjacent pair into a new token.
A language model can't read raw text — it reads tokens, integer IDs from a fixed vocabulary. The hard question is what a token should be. Whole words give a vocabulary in the millions and choke on anything unseen; single characters keep the vocabulary tiny but make sequences painfully long. Byte-pair encoding (BPE) meets in the middle with subwords.
BPE starts from the smallest units — characters (or raw bytes) — and greedily grows a vocabulary. It counts every adjacent pair across the training text, merges the single most frequent pair into one new token, and repeats. Each merge adds exactly one token to the vocabulary, and you stop once you hit the target size.
corpus (with end-of-word marks): low low lower newest newest
start as characters: l o w _ | l o w _ | l o w e r _ | ...
most frequent pair is (e, s) in 'newest' x2 -> merge -> 'es'
then (es, t) -> 'est'
then (l, o) -> 'lo' , (lo, w) -> 'low'
result: 'low' and 'est' are now single tokens;
a rare word like 'zyzzyva' still splits into pieces.ing, low, and est become single tokens, so frequent text stays short. Rare words fall back to smaller pieces. This caps the vocabulary size while keeping sequence length reasonable — the two costs BPE trades off against each other.