Byte-Pair Encoding (BPE) is a subword tokenization algorithm that constructs a vocabulary by iteratively merging the most frequent adjacent pairs of symbols in a training corpus. Starting from a base vocabulary of individual characters, it counts all adjacent symbol pairs, identifies the most frequent pair, and merges them into a new token. This process repeats for a predefined number of merge operations, creating a vocabulary of common words, subwords, and rare character sequences. The resulting merge table enables the tokenizer to segment any unseen word into known subword units, effectively eliminating out-of-vocabulary problems.
Glossary
Byte-Pair Encoding (BPE)

What is Byte-Pair Encoding (BPE)?
Byte-Pair Encoding (BPE) is a data compression algorithm repurposed for tokenization that iteratively merges the most frequent adjacent pairs of bytes or characters to build a subword vocabulary from a training corpus.
During tokenization, a trained BPE model applies its learned merge rules in order to an input sequence. The algorithm first splits the text into its base characters, then iteratively applies the highest-priority merge rule that matches an adjacent pair. This greedy application continues until no more merges are possible. For example, the word "lower" might be segmented into low and er if that merge was frequent in training, while "lowering" becomes low, er, and ing. This approach balances vocabulary size with representational power, making BPE the foundational tokenization strategy for models like GPT and RoBERTa.
Key Characteristics of BPE
Byte-Pair Encoding (BPE) is a data compression algorithm repurposed for tokenization. It iteratively merges the most frequent adjacent pairs of bytes or characters to build a subword vocabulary, balancing vocabulary size with the ability to represent rare and unseen words.
Iterative Merge Algorithm
BPE starts with a base vocabulary of individual characters or bytes. It then scans the training corpus to find the most frequent adjacent pair of tokens and merges them into a single new token. This process repeats for a pre-defined number of merge operations.
- Initial Vocabulary: All unique characters in the corpus.
- Merge Rule: Selects the pair (A, B) that maximizes frequency.
- Termination: Stops after
Nmerges, whereNis a hyperparameter controlling the final vocabulary size.
Open-Vocabulary Capability
A defining advantage of BPE is its ability to encode any unseen word by decomposing it into known subword units. A word like 'unfriendliness' can be segmented into ['un', 'friend', 'li', 'ness'] if those subwords exist in the vocabulary.
- No OOV Tokens: Eliminates the need for a dedicated unknown token for rare words.
- Morphological Awareness: Learns common prefixes, suffixes, and stems.
- Graceful Degradation: Falls back to individual characters for completely novel sequences.
Vocabulary Size Hyperparameter
The final vocabulary size is a critical hyperparameter chosen before training. It directly controls the granularity of the segmentation and the model's embedding matrix size.
- Small Vocab (e.g., 8k): Results in shorter sequences but more aggressive segmentation, often splitting words into characters.
- Large Vocab (e.g., 50k): Encodes most common words as single tokens, reducing sequence length but increasing the embedding layer's memory footprint.
- Trade-off: Balances encoding efficiency against model parameter count.
Byte-Level BPE
A variant of BPE that operates on a raw byte sequence (UTF-8) rather than Unicode characters. This guarantees a base vocabulary of exactly 256 tokens and completely eliminates the concept of an unknown token.
- Universal Encoding: Can tokenize any text in any language without special handling.
- Robust to Noise: Handles typos, emojis, and rare symbols gracefully by falling back to byte-level segmentation.
- GPT-2 & GPT-3: This approach was popularized by OpenAI's GPT series.
Greedy Deterministic Encoding
During inference, BPE tokenization is a deterministic, greedy process. The algorithm applies the learned merge rules in the exact order they were created during training.
- Reproducibility: The same input text always produces the identical token sequence.
- No Probabilistic Choice: Unlike Unigram LM, BPE does not sample from a probability distribution during standard encoding.
- Subword Regularization: A technique called BPE-Dropout can be applied during training to stochastically skip merges, improving robustness.
Training Corpus Dependency
The resulting subword vocabulary is entirely dependent on the training corpus. A model trained on English Wikipedia will learn different merges than one trained on a multilingual code repository.
- Domain Adaptation: A BPE tokenizer trained on medical text will learn merges like '##oma' and '##itis'.
- Language Bias: A corpus with 90% English text will produce a vocabulary optimized for English morphology, potentially segmenting other languages less efficiently.
- Pre-tokenization: Often combined with a regex-based pre-tokenizer to prevent merges across word boundaries.
BPE vs. Other Subword Tokenization Algorithms
A feature-level comparison of Byte-Pair Encoding against WordPiece and Unigram Language Model tokenization strategies.
| Feature | Byte-Pair Encoding (BPE) | WordPiece | Unigram Language Model |
|---|---|---|---|
Core Principle | Iteratively merges the most frequent adjacent pair of tokens | Merges pairs that maximize training data likelihood | Starts with large vocabulary; prunes tokens that least increase overall likelihood |
Training Direction | Bottom-up (starts with characters) | Bottom-up (starts with characters) | Top-down (starts with large seed vocabulary) |
Selection Criterion | Raw co-occurrence frequency | Mutual information gain (likelihood delta) | Loss contribution (token removal impact) |
Out-of-Vocabulary Handling | ✅ | ✅ | ✅ |
Lossless Decoding | |||
Probabilistic Segmentation | |||
Primary Model Association | GPT, RoBERTa, LLaMA | BERT, DistilBERT | XLNet, ALBERT, T5 |
Subword Regularization Support |
Frequently Asked Questions
Clear, technically precise answers to the most common questions about the BPE subword tokenization algorithm, its mechanics, and its role in modern language models.
Byte-Pair Encoding (BPE) is a subword tokenization algorithm that constructs a vocabulary by iteratively merging the most frequent adjacent pairs of bytes or characters in a training corpus. Originally a data compression algorithm, BPE was repurposed for NLP to balance vocabulary size against the ability to represent rare and unseen words. The process begins with a base vocabulary of all unique characters. In each iteration, the algorithm scans the corpus, counts all adjacent symbol pairs, and merges the most frequent pair into a new token. This merge is added to the vocabulary, and the process repeats for a predefined number of merge operations. The result is a deterministic set of merge rules that can segment any new word into known subwords. For example, the word 'lower' might be segmented into low and er if that pair was frequently observed during training. This mechanism elegantly solves the out-of-vocabulary (OOV) problem by ensuring any sequence can be represented as a combination of known subword units, including individual characters as a fallback.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Byte-Pair Encoding is one of several subword algorithms that form the backbone of modern NLP tokenization. Understanding these related concepts is essential for mastering vocabulary construction.
Vocabulary Size
A critical hyperparameter defining the total number of unique tokens a model recognizes. This represents a fundamental trade-off:
- Larger vocabularies (50k-100k): shorter sequences, faster inference, but larger embedding matrices consuming more GPU memory
- Smaller vocabularies (8k-32k): more compact models, but longer token sequences requiring more compute per example
- Typical BPE vocabularies range from 30,000 to 50,000 tokens for English-dominant models

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us