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 text corpus. Originally a data compression technique, BPE bridges the gap between word-level and character-level tokenization, enabling language models to represent an open vocabulary with a finite set of subword units.
Glossary
Byte-Pair Encoding (BPE)

What is Byte-Pair Encoding (BPE)?
A data compression algorithm adapted for NLP that iteratively merges the most frequent pairs of bytes or characters to build a subword vocabulary, effectively handling rare and out-of-vocabulary words.
The algorithm begins with a base vocabulary of individual characters and repeatedly identifies the most common bigram, merging it into a new token until a predefined vocabulary size is reached. This process ensures that frequent words remain intact as single tokens, while rare and out-of-vocabulary words are decomposed into known subword fragments, eliminating the <UNK> problem in neural machine translation and large language models.
Key Characteristics of BPE
Byte-Pair Encoding (BPE) is a data compression algorithm adapted for NLP that iteratively merges the most frequent pairs of bytes or characters to build a subword vocabulary. This hybrid approach bridges the gap between word-level and character-level tokenization, effectively handling rare and out-of-vocabulary words.
Iterative Merge Algorithm
BPE starts with a base vocabulary of individual characters and iteratively merges the most frequent adjacent pair of symbols. This process builds a vocabulary of subword units, where common words remain intact and rare words are decomposed into meaningful fragments.
- Initialization: Vocabulary contains every unique character in the corpus.
- Merge Step: The pair
(A, B)with the highest co-occurrence count is merged into a new tokenAB. - Termination: The process stops after a predefined number of merges or when a target vocabulary size is reached.
Open-Vocabulary Handling
A core strength of BPE is its ability to encode any unseen word by decomposing it into known subword units. This eliminates the problem of out-of-vocabulary (OOV) tokens that plague word-level models.
- A novel word like 'unfriendliness' can be segmented into
['un', 'friend', 'li', 'ness']if those subwords exist in the vocabulary. - This is critical for handling morphologically rich languages, technical jargon, and user-generated content with typos.
Frequency-Driven Compression
BPE naturally learns a vocabulary that reflects the statistical structure of the training corpus. High-frequency words and morphemes are preserved as single tokens, while rare sequences are broken down.
- Efficiency: Common words like 'the' or 'ing' become single tokens, maximizing the information per token.
- Granularity: Rare or complex words are split into smaller, reusable parts, preventing vocabulary explosion.
- This balances the context window length with the need for granular semantic representation.
Reversible and Lossless Encoding
The BPE tokenization process is fully deterministic and reversible. Given the merge rules learned during training, a tokenized sequence can be perfectly reconstructed back into the original text.
- Encoding: Apply the learned merge operations greedily to split a word into subword tokens.
- Decoding: Simply concatenate all tokens and replace the special whitespace character to recover the original string.
- This lossless property ensures no information is discarded during the preprocessing step.
BPE vs. WordPiece vs. Unigram
BPE is one of several subword tokenization algorithms, each with distinct optimization criteria.
- BPE: Merges the most frequent pair. A deterministic, bottom-up approach.
- WordPiece: Merges the pair that maximizes the likelihood of the training data. Used by BERT.
- Unigram LM: Starts with a large vocabulary and iteratively prunes tokens that least impact the overall likelihood. A probabilistic, top-down approach used by T5 and Llama.
- All three achieve open-vocabulary encoding but differ in their training objectives and resulting vocabulary composition.
Frequently Asked Questions
Clear, technical answers to the most common questions about the subword tokenization algorithm that powers modern language models.
Byte-Pair Encoding (BPE) is a data compression algorithm adapted for NLP that builds a subword vocabulary by iteratively merging the most frequent adjacent pairs of bytes or characters in a text corpus. The process begins with a base vocabulary of individual characters. In each iteration, the algorithm scans the corpus, identifies the most frequently co-occurring pair of adjacent tokens, and creates a new token by merging that pair. This merge is added to the vocabulary, and all occurrences in the corpus are replaced. The process repeats for a predefined number of merge operations. The result is a vocabulary where common words remain intact (e.g., 'the', 'and'), while rare or unseen words are decomposed into meaningful subword units (e.g., 'low' + 'er' = 'lower'). This elegantly solves the out-of-vocabulary (OOV) problem, as any word can be represented as a sequence of known subwords.
BPE vs. Other Tokenization Methods
A feature-level comparison of Byte-Pair Encoding against word-level, character-level, and unigram-based tokenization strategies for NLP pipelines.
| Feature | Byte-Pair Encoding | Word-Level | Character-Level | Unigram LM |
|---|---|---|---|---|
Granularity | Subword | Word | Character | Subword |
Handles OOV Words | ||||
Vocabulary Size | Moderate (8k-50k) | Very Large (100k+) | Tiny (50-256) | Moderate (8k-50k) |
Encodes Rare Words Efficiently | ||||
Preserves Morphology | ||||
Reversible (Lossless) | ||||
Training Algorithm | Iterative Merge | Frequency Threshold | None (Fixed) | Likelihood Maximization |
Typical OOV Rate | < 0.1% | 2-5% | 0% | < 0.1% |
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 a cornerstone of modern tokenization. These related concepts form the vocabulary and retrieval backbone of answer engine architectures.
Tokenization
The foundational text processing step of segmenting a string into discrete units called tokens. BPE is a specific subword tokenization algorithm. The choice of tokenizer directly impacts a model's vocabulary size and its ability to handle out-of-vocabulary (OOV) words. Other methods include WordPiece and Unigram.
Named Entity Recognition (NER)
A fundamental information extraction task that locates and classifies named entities in text into pre-defined categories like person names, organizations, and locations. BPE tokenization helps NER models handle rare or misspelled entity names by breaking them into known subword units.
Query Expansion
A set of techniques for augmenting the original search query with additional, related terms to improve recall. This bridges the vocabulary gap between user language and indexed documents. BPE ensures that even if a query and document use different morphological forms of a word, they can share common subword tokens.
Spelling Correction
The automated detection and rectification of typographical errors in a search query before retrieval. BPE provides a robust fallback: even if a word is misspelled, its constituent character n-grams can still match the correct subword tokens in the index, preventing a total retrieval failure.
Dense Retrieval
A modern retrieval paradigm that encodes queries and documents into dense, fixed-length vector embeddings. BPE tokenization is the first step in this pipeline, converting raw text into the token IDs that a transformer-based encoder model can process to produce the semantic embedding.
Word Sense Disambiguation (WSD)
The computational task of identifying which meaning of a polysemous word is intended in context (e.g., 'bank' as a financial institution vs. a river edge). BPE does not solve WSD, but its subword units can help models recognize shared morphological roots across different senses, aiding in generalization.

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