Kolkata ·  
AI engineering
foundations
short · 3 min

Byte-pair encoding

The merge algorithm most tokenizers use to build their vocabulary.

updated 23 Sept 2026

Back in 1994 there was a simple way to shrink a file: find the pair of bytes that shows up most often, swap every copy for one spare byte, and do it again. In 2015 some translation researchers borrowed that idea to cut words into pieces, and a version of it now decides how most LLMs see text.

Tokenization explains why models read chunks of text rather than letters or whole words. BPE is how those chunks get chosen. Let's run it by hand.

Five words

Say the whole training text is five words, with these counts:

WordTimes it appears
hug10
pug5
pun12
bun4
hugs5

Split everything into letters. The starting vocabulary is every letter we saw: b g h n p s u. Seven tokens.

Now count each pair of neighbours, weighted by how often its word appears. h u shows up in "hug" and "hugs", 15 times. u n is in "pun" and "bun", 16 times. u g is in "hug", "pug" and "hugs", 20 times. So u g wins. We add ug to the vocabulary and write it as one symbol from now on. "hug" is now h ug.

Count again. u n at 16 is on top, so un becomes a token. Count again. h ug comes up 15 times, and we get hug, our first three-letter token.

That's all there is to it. Count pairs, merge the most common one, write the merge down, repeat. Early merges make two-letter pieces, later ones make longer ones, and very common words end up as a single token. You stop when the vocabulary is as big as you want, and that size is the one real choice: starting symbols plus one token per merge.

Now some new words

Training leaves us an ordered list of merges. To tokenize new text, split it into letters and apply the merges in the order they were learned.

"bug" was never in our five words. It becomes b + ug, no problem. That's why BPE caught on: rare words, typos and new names can always be built from smaller pieces. In the 2015 paper, this beat a dictionary-based fallback for translating English to German and Russian.

Now try "mug". We never saw an "m", so it becomes [UNK] + ug, with [UNK] meaning "unknown". That's a real hole. It's one reason older language models handled emojis so badly.

Start from bytes instead

The fix is to start from bytes rather than letters. All text is stored as bytes, and there are only 256 possible values. Put all 256 in the starting vocabulary and any character at all, emoji included, can be spelled, even if only as raw bytes. That's byte-level BPE, and GPT-2 used it. GPT-2's tokenizer has no unknown token, because nothing can be unknown.

One more difference from the 2015 version. That one marked the end of each word with a special symbol. GPT-2 glues the space onto the front of the next word, so it learns pieces like " is" and " the", space included.

You won't ever train BPE for an API model; the tokenizer comes with it. But knowing how it works lets you guess the splits: common words are one token, odd strings are several. And it tells you why every model family's tokenizer is its own. The merges depend on which text it was trained on and how big the vocabulary was set. Even ties matter: when two pairs are equally common, different implementations pick differently, so two "BPE" tokenizers trained on the same text can still end up with different lists.

Further reading