Real tokenizers are learned, not written by hand. Start from single letters and keep merging the most common pair, and useful pieces like "low" appear by themselves.
AI · beginner · 10 min · 23 steps · free
What you'll learn
Explain how BPE builds a vocabulary of word pieces
Count pairs and merge the most common one
See why common words become one token and rare words several
bpe_tokenizer.py
# Real tokenizers learn word pieces. Byte-pair encoding (BPE): start from letters, keep merging the most common pair.
words = ["l o w", "l o w e r", "l o w e s t", "s l o w"]
for merge in range(3):
pairs = [a + " " + b for w in words for a, b in zip(w.split(" "), w.split(" ")[1:])]
options = list(dict.fromkeys(pairs))
counts = [pairs.count(p) for p in options]
best = options[counts.index(max(counts))]
words = [(" " + w + " ").replace(" " + best + " ", " " + best.replace(" ", "") + " ").strip() for w in words]
print("merge", merge, ":", best, "->", words)
Step by step
Four words, each split into single letters with spaces between the pieces. Every letter is a token to begin with.
Do three merges. Real tokenizers do tens of thousands.
Every pair of neighbouring pieces in every word: "l o", "o w", "w e" and so on.
Each different pair once.
How often each appears. "l o" and "o w" both appear 4 times, once in every word.
The most common pair wins. In a tie the first one found wins: "l o".
Merge it everywhere: "l o" becomes the single piece "lo". The spaces around the pair make sure only whole pieces are joined.
It prints merge 0 : l o -> ['lo w', 'lo w e r', 'lo w e s t', 's lo w'].
merge changes from 0 to 1.
pairs now points to a different list: ['lo w', 'lo w', 'w e', 'e r', 'lo w', 'w e', 'e s', 's t', …].
options now points to a different list: ['lo w', 'w e', 'e r', 'e s', 's t', 's lo'].
"lo w" appears 4 times.
best changes from 'l o' to 'lo w'.
Merged: "low" is now a single token, and "low" on its own is one token.
It prints merge 1 : lo w -> ['low', 'low e r', 'low e s t', 's low'].
merge changes from 1 to 2.
pairs now points to a different list: ['low e', 'e r', 'low e', 'e s', 's t', 's low'].
options now points to a different list: ['low e', 'e r', 'e s', 's t', 's low'].
"lowe" becomes a piece. Pieces do not have to be real words or syllables, just common.
It prints merge 2 : low e -> ['low', 'lowe r', 'lowe s t', 's low'].
After three merges: "low" is one token, "lowest" is three ("lowe", "s", "t"). Common words end up as single tokens and rare words as a few pieces. That is why "tokens" and "words" are different counts.
Common mistakes
Thinking tokens line up with syllables or meaning: they follow frequency
Assuming every language costs the same number of tokens per word
Best practices
Expect rare words, names and numbers to use more tokens than common words
Key takeaways
BPE starts from single characters (or bytes), so any text can be written.
It repeatedly merges the most frequent neighbouring pair into a new piece.
Common words become one token; rare words are spelled with several pieces.
The order of merges is saved and replayed to tokenize new text.
Check yourself
Pairs counted: "t h" 5 times, "h e" 4 times, "e r" 2 times. Which is merged?
Answer: "t h". BPE always merges the most frequent pair.
Why does a rare name often take several tokens?
Answer: Its pairs were too rare to be merged into one piece. Only common pairs get merged, so rare words are spelled out with smaller pieces.