How Byte Pair Encoding teaches LLM tokenizers to split text

By TechCompare · Updated · How we check

BPE is the dominant tokenization algorithm for a reason: it handles any text, any language, and any script without special cases. Understanding BPE helps you predict how a model will split your text and why some prompts cost more tokens than you'd expect based on character count.

Byte Pair Encoding (BPE) is the algorithm behind most modern LLM tokenizers, including OpenAI's tiktoken and Meta's Llama tokenizer. BPE starts with individual characters as tokens, then repeatedly merges the most frequent pair of adjacent tokens into a new token. After training on a large text corpus, common words become single tokens. Rare words stay split into subword pieces.

Knowledge area
Fundamentals
How tokens and tokenization work
Topic focus
BPE tokenization
bpe

How this is calculated

BPE training works like this: start with every unique character in the training data as a token. Count all adjacent token pairs. Merge the most frequent pair into a new token. Repeat thousands of times until the desired vocabulary size is reached (e.g. 100K or 200K tokens).

The resulting vocabulary handles any text, even words never seen during training, because any word can be broken into known subword tokens. This is why misspellings, code, and invented words still produce reasonable tokenization.

BPE's main competitor is Unigram (used by SentencePiece in some configurations), which starts with a large vocabulary and prunes it. BPE is more common in the latest generation of models (GPT-5, Llama 4) because it handles whitespace and code more naturally.

Verdict

BPE handles any text because it builds a vocabulary from characters upward. A word never seen during training can still tokenize cleanly into known subwords, which covers misspellings, invented terms, and code.

Unigram, the SentencePiece alternative, prunes a large vocabulary instead and shows up less in the current generation of models.

More Tokens scenarios

What are tokens
A token is the atomic unit of text that a language model processes.
View details
OpenAI vs Llama tokenizer
A sentence that costs 50 tokens on GPT-5 can land close to that on Llama 4 but cost more on Llama 3 or Claude, especially outside English.
View details
Token limits by model
A model context limit is a token budget, but providers may publish separate input and output limits.
View details

Frequently asked questions

What is Byte Pair Encoding in simple terms?
BPE builds a vocabulary from the bottom up. It starts with every character as its own token, then repeatedly merges the most frequent adjacent pair ('t' plus 'h' becomes 'th', and so on) until the vocabulary reaches its target size, such as 100K or 200K tokens. Common words end up as single tokens, and rare ones stay split into pieces.
How does BPE handle words it has never seen?
By falling back to subword pieces. Because the vocabulary grew from characters upward, any string can be decomposed into known fragments, so misspellings, invented product names, and code identifiers tokenize without special cases. That's the property that made BPE the default for models like GPT-5 and Llama 4.
What's the difference between BPE and Unigram tokenization?
Opposite construction directions. BPE starts with characters and merges the most frequent pairs upward. Unigram, used in some SentencePiece setups, starts from a large candidate vocabulary and prunes pieces by likelihood. Both produce subword tokenizers. BPE currently dominates the newest model families, while Unigram appears more in multilingual pipelines.