BPE picks the next merge by raw pair frequency; WordPiece picks the merge that maximally increases corpus likelihood, scored as freq(AB) over freq(A) times freq(B).
Picture a kid stacking blocks. Both kids start with a pile of letter blocks and a rule: each turn, glue two blocks together to make a bigger block, and add the new shape to your toolbox. The first kid (BPE) glues whichever two blocks happen to sit next to each other most often. Simple counting. The second kid (WordPiece) is fussier. It only glues two blocks together if the pair shows up much more often than you would expect from how often each block shows up on its own. That ratio rewards merges that are genuinely surprising. After many turns, both kids have a toolbox of bigger blocks they can use to build any word. They tend to pick similar blocks for common words but diverge on rarer combinations, which is why a text tokenized with BPE and the same text tokenized with WordPiece end up looking subtly different.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
BPE and WordPiece are the two best-known subword tokenizer training algorithms. They look almost identical from a distance: both grow a vocabulary by iteratively picking adjacent pairs and merging them into a new token. The distinction is small but consequential, and in 2026 it is mostly a historical landmark that explains why generative LLMs and BERT-family models tokenize text differently.
This deep dive unpacks the shared skeleton, names the precise difference in the merge-selection rule, derives where the WordPiece ratio comes from, and places both algorithms in the modern production landscape.
The shared training skeleton
Both algorithms start from a base alphabet. For classical BPE and WordPiece this is the set of Unicode characters in the corpus. For byte-level BPE (the GPT-2 variant most generative LLMs now use) it is the 256 possible byte values.
From that base, both algorithms run an iterative loop. On each iteration, they look at every adjacent pair of pieces across the entire training corpus, count or score those pairs, pick a winning pair, and merge every occurrence of that pair into a new piece that gets added to the vocabulary. Then they recount and pick again. The loop continues until the vocabulary reaches the target size, which is usually a hyperparameter chosen up front (32K, 50K, 128K, 200K, and so on).
The artifact produced is a learned, ordered list of merges. At encoding time the tokenizer reapplies that list greedily to any input string to produce a sequence of token ids. Both algorithms are deterministic at encoding time and produce one segmentation per input. The split between them is purely about how the winning pair is chosen during training.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- GPT-5.5 and o3 use BPE via tiktoken's o200k_base encoding with a 200K vocabulary; no WordPiece anywhere in the OpenAI stack in 2026.
- Llama 3 and Llama 4 use BPE with a 128K vocabulary, having switched away from the SentencePiece tokenizer used in Llama 2.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhere does the freq(AB) over freq(A) times freq(B) formula come from?
Start from the unigram language model likelihood of the corpus, write the change in log-likelihood from merging pair (A, B) into a new token AB, and show that maximizing that delta reduces to picking the pair with the largest score freq(AB) divided by freq(A) times freq(B). The ratio is the closed-form solution under the unigram independence assumption.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Saying BPE and WordPiece differ in how they segment at inference time. They differ in how they pick merges during training; both apply learned merges greedily at encoding time.
60 second bullets to scan on the way to the call.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.