BPE learned 50,000 merge rules during training. What is a merge rule, and where does it live at inference?
A merge rule is one entry in BPE's ordered learned list: 't' + 'h' = 'th'. Encoders replay merges greedily in learned order. Storage: merges.txt, tokenizer.json, or the tiktoken binary.
Imagine a cookbook where every recipe just says 'combine X with Y to make Z'. The cookbook has hundreds of recipes in a specific order. You start with a kitchen full of plain ingredients. To cook anything, you flip through the recipes in order and every time you see an ingredient pair you have in the kitchen, you combine them as the recipe says. Then you keep flipping. By the end, your kitchen has a few big assembled dishes instead of many small ingredients. BPE merges work the same way. The training process produced an ordered cookbook of pair-combine rules. The encoder reads the cookbook in order and combines every applicable pair in the input. Different tokenizer libraries print the cookbook in different file formats, but the cookbook itself is the same kind of object.
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.
A merge rule is the load-bearing unit of BPE tokenization. BPE training produces a vocabulary and an ordered list of merge rules; BPE encoding replays that list deterministically on every new input. Understanding what a merge rule is, where it is stored, and why the order matters is one of the cleanest ways to demonstrate that you understand what BPE actually does at inference time.
This deep dive walks through the training-time origin of merges, the encoding-time replay, the storage formats across the major 2026 tokenizer libraries, and the senior nuances that separate a careful answer from a superficial one.
Where merges come from: BPE training
BPE training is a simple loop that produces an ordered list of merge rules. The setup is:
- Choose a base alphabet. For classical BPE this is the set of Unicode characters in the training corpus. For byte-level BPE (the GPT-2 variant used by tiktoken, Llama 3, and most modern generative LLMs) it is the 256 possible byte values.
- Tokenize the training corpus into the base alphabet.
- Repeat until the vocabulary reaches the target size: count every adjacent pair of pieces across the corpus, pick the pair with the highest frequency, merge every occurrence into a new piece, add the new piece to the vocabulary, and record the merge as a rule.
The output of training is two things. The vocabulary is the base alphabet plus every merged piece, mapped to integer ids. The merge list is an ordered sequence of pair-token rules: '(A, B) becomes AB'. The list is in the order the merges were learned, which is also (roughly) from most frequent to least frequent on the training corpus.
A typical merge list contains tens of thousands of entries. For a 100K-vocabulary byte-level BPE tokenizer like cl100k_base, the list has roughly 99,744 entries (100K total vocab minus 256 base bytes). For a 200K vocabulary like o200k_base it has roughly 199,744 entries.
The training process is corpus-dependent. The same algorithm on different corpora produces different vocabularies and different merge lists. The same algorithm on the same corpus with different vocabulary-size budgets produces lists of different lengths but the early merges are identical because they are picked by frequency before any cutoff.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- GPT-2's original tokenizer ships with merges.txt and vocab.json files in the model directory; you can open merges.txt and read the BPE training history line by line.
- Llama 3's tokenizer ships as tokenizer.json in the model repository on Hugging Face; the merges are nested inside the model.merges field of the JSON.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the order of the merge list critical and what would happen if you shuffled it?
Earlier merges create pieces that later merges combine. If 't' + 'h' becomes 'th' is at position 5 and 'th' + 'e' becomes 'the' is at position 12, shuffling so that 'the' merge comes first means it will never fire because 'th' does not yet exist. Random shuffling produces a tokenizer that mostly fails to merge anything beyond the simplest pieces, drastically inflating sequence length.
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.
Thinking BPE encoding picks the highest-scoring merge at each step at inference time. Encoding is not re-running BPE training; it is replaying the learned merges in their training order, greedily, until no more apply.
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.