How Byte Pair Encoding Translates Raw Text into Model Input Tokens
Byte Pair Encoding bridges the gap between human language and machine computation by breaking words into frequent subword chunks. This statistical merging process allows large language models to process any text without requiring an infinite vocabulary.
By Ishani Patel
Share this story
In short
- Byte Pair Encoding (BPE) translates human text into the mathematical tokens required by neural networks.
- Originally a 1994 data compression algorithm, BPE was adapted for natural language processing in 2016.
- The algorithm iteratively merges the most frequent adjacent characters into single subword units.
Before a neural network can predict the next word, it must first convert human language into a sequence of numbers it can mathematically process. This translation step dictates the model's vocabulary, its memory footprint, and its ability to handle typos or rare terminology. If a model attempts to memorize every word in the English language, its embedding layer becomes too massive to compute. If it reads text letter by letter, the sequences become too long for its context window to retain.[5]
The dominant mechanism for this translation in modern large language models is Byte Pair Encoding (BPE). Originally conceived by Philip Gage in 1994, the algorithm was not designed for artificial intelligence. According to Wikipedia, Gage's invention was "a simple form of data compression in which the most common pair of consecutive bytes of data is replaced with a byte that does not occur within that data."[1]
In 2016, researchers Rico Sennrich, Barry Haddow, and Alexandra Birch adapted Gage's compression technique for natural language processing. Their paper, published in the ACL Anthology, sought to solve a specific bottleneck in neural machine translation: the out-of-vocabulary problem.[4]
"We show that subword models improve over a back-off dictionary baseline for the WMT 15 translation tasks English-German and English-Russian by up to 1.1 and 1.3 BLEU, respectively," Sennrich and his colleagues wrote. By breaking unknown words into known subword units, the model could guess the meaning of a novel term based on its fragments.[4]
The BPE algorithm operates through a statistical merging process. It begins by splitting the entire training corpus into individual characters, or bytes. As the Hugging Face documentation explains, "Byte-Pair Encoding (BPE) was initially a data compression algorithm... it was later adapted to NLP."[2]
From this base vocabulary of roughly 256 individual byte characters, the algorithm scans the text to find the most frequently adjacent pair of symbols. If the letters "e" and "s" appear next to each other more often than any other combination, BPE merges them into a single new token: "es".[2]
This counting and merging cycle repeats iteratively. The new "es" token might subsequently merge with "t" to form "est". The process continues until the vocabulary reaches a pre-defined target size, typically between 30,000 and 50,000 tokens for modern language models.[2][5]
This statistical approach creates a highly efficient, variable-length representation of language. Common words like "the" or "apple" are merged into single tokens because their character sequences appear together constantly. Rare words, however, remain split into multiple subword tokens.
My Written Word notes that this dynamic scaling is what makes BPE so effective for large language models. A model does not need to memorize every possible word in the English language; it only needs to learn the most frequent chunks and how they combine.
The mathematical properties of this tokenization method are still being actively researched. A 2023 paper published on arXiv, titled "Formalizing BPE Tokenization," attempted to rigorously define the algorithm's behavior.[3]
"Despite its widespread use, the theoretical properties of BPE are not well understood," the authors of the arXiv paper noted, highlighting that the greedy nature of the merging algorithm does not always guarantee the optimal tokenization for a given sequence.[3]
One significant limitation of BPE is its language bias. Because the algorithm merges tokens based on frequency within the training data, languages overrepresented in the corpus—primarily English—receive highly optimized, single-token representations for most words.[5]
Conversely, morphologically rich languages or those with non-Latin scripts often fracture into multiple tokens per word. This fragmentation forces the model to expend more of its context window to process the same amount of semantic information, effectively making the model slower and more computationally expensive for non-English users.[5]
The reliance on BPE also introduces specific failure modes known as glitch tokens. If a particular string of characters appears frequently in the training data but lacks semantic meaning—such as a repeated string of code or a specific Reddit username—the BPE algorithm will dutifully merge it into a single token.[5]
When a user prompts the model with one of these glitch tokens, the neural network often hallucinates or outputs nonsensical text, because the token exists in its vocabulary but lacks robust connections to other concepts in its neural weights.[5]
The exact vocabulary size acts as a critical hyperparameter. A smaller vocabulary forces the model to use more tokens to represent a text, shrinking the effective context window. A larger vocabulary reduces the sequence length but exponentially increases the size of the model's embedding layer, requiring more memory.[2][5]
The balance struck by BPE—compressing sequence lengths by roughly a factor of three compared to pure character-level encoding, while avoiding the infinite scaling of word-level models—remains the industry standard. The algorithm's statistical elegance ensures that as language evolves, the tokenization process adapts automatically to the frequencies of the new data.[5]
Definitions
- Token
- The fundamental unit of data processed by a large language model, which can be a whole word, a syllable, or a single character.
- Out-of-vocabulary (OOV)
- A problem where a model encounters a word it has never seen before and cannot process it.
- Context Window
- The maximum number of tokens a language model can process and remember at one time during a single interaction.
- Embedding Layer
- The part of a neural network that translates discrete tokens into continuous mathematical vectors.
Questions & answers
What does Byte Pair Encoding actually do?
It breaks down raw text into smaller chunks called tokens based on how frequently characters appear together. Common words become single tokens, while rare words are split into multiple subword pieces.
Why don't models just use whole words?
Using whole words would require a massive, nearly infinite vocabulary to account for every possible word, typo, and conjugation, which would overwhelm the model's memory.
How does BPE affect non-English languages?
Because BPE is trained on datasets that are predominantly English, it often fragments non-English words into many small tokens, making the model slower and more expensive to run for those languages.
What is a glitch token?
A glitch token is a string of characters that appears frequently in the training data (like a repeated username) but has no real meaning, which can cause the model to output nonsensical text when prompted with it.
Analysis by camp
Subword Efficiency Advocates
Researchers who view BPE as the optimal mathematical compromise between vocabulary size and sequence length.
Proponents of subword tokenization emphasize that it elegantly solves the out-of-vocabulary problem that plagued early natural language processing models. By breaking rare or novel words into recognizable chunks, the model can infer meaning without requiring a dictionary update. This camp points out that BPE allows models to handle typos, novel slang, and complex coding languages with a fixed memory footprint, making large-scale AI training computationally feasible.
Multilingual Equity Critics
Linguists and developers who argue that frequency-based tokenization inherently disadvantages non-English languages.
Critics of the current tokenization paradigm argue that BPE acts as a hidden tax on non-English users. Because the algorithm merges characters based on their frequency in the training data—which is overwhelmingly English—words in morphologically rich languages or non-Latin scripts are often shattered into many small tokens. This forces models to use more compute to process the same amount of information, leading to slower generation times and higher API costs for users outside the Anglosphere.
Algorithmic Formalists
Computer scientists focused on the theoretical limitations and greedy nature of the BPE algorithm.
Researchers analyzing the mathematical foundations of BPE note that it is a 'greedy' algorithm, meaning it makes the locally optimal choice at each step of the merging process. This camp highlights that this approach does not guarantee a globally optimal tokenization for any given text. They advocate for exploring alternative tokenization strategies, such as Unigram language modeling, which approaches the problem probabilistically rather than strictly through frequency counts.
- Subword Efficiency Advocates
- Researchers who view BPE as the optimal mathematical compromise between vocabulary size and sequence length.
- Multilingual Equity Critics
- Linguists and developers who argue that frequency-based tokenization inherently disadvantages non-English languages.
- Algorithmic Formalists
- Computer scientists focused on the theoretical limitations and greedy nature of the BPE algorithm.
Perspectives this story doesn't cover
- Hardware architects optimizing token lookup tables
Sources
[1]WikipediaByte-pair encoding
Read on Wikipedia →
[2]Hugging FaceSubword Efficiency AdvocatesByte-Pair Encoding tokenization
Read on Hugging Face →
[3]arXivAlgorithmic Formalists[2309.08715] Formalizing BPE Tokenization
Read on arXiv →
[4]ACL AnthologySubword Efficiency AdvocatesNeural Machine Translation of Rare Words with Subword Units
Read on ACL Anthology →
[5]Factlen Editorial TeamMultilingual Equity CriticsSynthesis by Factlen editorial team
Read on Factlen Editorial Team →
More in Artificial Intelligence
See all →AI Safety Frameworks
AI Researchers from OpenAI, Anthropic, Meta, and Microsoft Co-Author Joint Paper Calling for Autonomous System Oversight
6 sources
AI Hallucinations
The Three Primary Causes of Hallucination in Large Language Models
6 sources
Loss Functions
The Equivalence of Minimizing Cross-Entropy Loss and Maximizing Likelihood in Neural Networks
7 sources
Model Training
How Adam's Adaptive Learning Rates Solve the Deep Learning Convergence Bottleneck
6 sources
Comments
Every angle. Every day.
Get Artificial Intelligence stories with full source coverage and perspective breakdowns, free every day.




