Byte-pair encoding (BPE)
Byte-pair encoding is a method for building a tokenizer's vocabulary by repeatedly merging the most frequent pair of neighbouring symbols in a body of text, so common words become single tokens and rare words split into pieces.
Language models do not read letters or whole words. They read tokens, and byte-pair encoding is a common way of deciding what those tokens are. Training a BPE tokenizer starts with the individual characters (or bytes) in a large sample of text. It counts every pair of neighbouring symbols, merges the most frequent pair into a new symbol, records that merge as a rule, and repeats until the vocabulary reaches a chosen size. To tokenize new text, the learned merges are applied in the order they were learned.
Hugging Face's course on tokenizers gives a small worked example. In a toy corpus made of the words hug, pug, pun, bun and hugs, the most frequent pair is u followed by g, so the first merge creates the token ug. The next merges create un and then hug. With those rules, the unseen word bug becomes b plus ug. Repeat this over billions of words and frequent words such as 'the' end up as single tokens, while a rare name is assembled from several smaller pieces.
The algorithm began as data compression. Philip Gage described it in 1994 as a way to shrink files by replacing the most frequent pair of bytes with a byte that was not otherwise used. In 2015 Rico Sennrich, Barry Haddow and Alexandra Birch adapted it for neural machine translation, splitting rare words into subword units so that a system with a fixed vocabulary could still handle names, compound words and loanwords. That paper is the root of its use in today's language models.
Many large language models use byte-level BPE, as GPT-2 did. Text is first turned into raw bytes, so the starting vocabulary is just 256 symbols and any text in any script can be encoded without an unknown token. OpenAI's tiktoken library describes BPE as reversible and lossless, with each token covering about four bytes of text on average. Vocabularies have grown: GPT-4's tokenizer has roughly 100,000 merged tokens, and Meta said in April 2024 that Llama 3 uses a vocabulary of 128,000 tokens. Meta's original LLaMA, from February 2023, used BPE but split every number into individual digits.
Tokenization explains several odd behaviours. A model receives a word as one or a few tokens, not as a string of letters, which is a frequently cited reason for chatbots stumbling over spelling and letter counts, such as the number of r's in strawberry. A December 2024 study found a more specific pattern: the models it tested could identify the letters in a word but struggled to count them, and errors were most common when a letter appeared more than once. Token counts also set what a request costs and how much fits in a context window, and the same sentence can come to a different number of tokens under different models' tokenizers.
It is a way to build a vocabulary of word pieces. Starting from single characters, it keeps joining the pair of pieces that appears together most often in a large sample of text, until it has as many pieces as it needs. Common words end up as one token and rare words are spelled out from several. A vocabulary of whole words cannot cover every name, typo or new word, while single characters make sequences very long and slow to process. BPE sits in between: frequent text is compact, and anything unusual can still be built from smaller pieces. That trade-off is why it spread from machine translation to most language models. Byte-level BPE runs the merging process over the raw bytes of text rather than over characters. Because there are only 256 possible bytes, every piece of text in any language or script can be represented, and the tokenizer never has to fall back to an unknown token. GPT-2 and RoBERTa use this approach. It is part of the story, because the model receives tokens rather than letters. A December 2024 study found that the models it tested could recognise which letters were in a word but struggled to count them, especially letters that appear more than once. So tokenization makes the task harder, but counting itself is also a weak point.Common questions
What is byte-pair encoding in simple terms?
Why do language models use BPE instead of whole words or single characters?
What is byte-level BPE?
Is tokenization why chatbots miscount the letters in strawberry?
Search for "byte-pair encoding tokenizer", "subword tokenization" or "byte-level BPE" for explanations, and try an interactive tokenizer viewer to see how a sentence is split. Trustworthy sources include the original 2015 subword paper by Sennrich and colleagues on arXiv, the tokenizer chapters of Hugging Face's course, and the documentation of open tokenizer libraries such as tiktoken. Model technical reports usually state the tokenizer and vocabulary size in their architecture section.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.