TechCompare LogoTechCompare

Byte Pair Encoding: how LLM tokenizers learn to split text into tokens

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, building a vocabulary from the bottom up. After training on a large text corpus, common words become single tokens. Rare words stay split into subword pieces.

By TechCompare · Updated

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

The algorithm starts with every unique character as a token, counts adjacent pairs across the corpus, and merges the most frequent pair into a new token, repeating until the vocabulary hits its target size. That bottom-up training is why a word never seen during production 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 might cost 55 tokens on Llama 4 or 48 on Claude.
View details ➜
Token limits by model
Context window size is the maximum number of tokens a model can process in a single request, including both input and output.
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 (Gemini among them), 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.