What is BPE (Byte Pair Encoding)?
BPE is a tokenisation algorithm that builds a subword vocabulary by repeatedly merging the character or byte pairs that appear next to each other most often in a text corpus.
It is also written byte-pair encoding. It differs from Unigram tokenisation in direction: BPE starts from small pieces and grows by merging, while Unigram starts from a large candidate set and prunes it with a probability model. First proposed in 1994 for data compression, it was adapted to neural machine translation in 2016.
- Topic
- Core concepts
- Also known as
- Byte-pair encoding, byte-level BPE
- Origin
- 1994, Philip Gage, a data compression algorithm
How BPE (Byte Pair Encoding) works
- The initial vocabulary consists of single characters or, in byte-level BPE, the 256 byte values.
- At each step the most frequent adjacent symbol pair is merged into one new vocabulary symbol.
- Merging repeats until the target vocabulary size is reached; the order of merges is recorded.
- When new text is split the recorded merges are applied in the same order, so the same text always yields the same tokens.
In more detail
Pre-tokenisation and boundary rules
Before BPE merges are learned or applied, most modern LLM tokenisers run a regex-based pre-tokenisation pass that splits text on whitespace, punctuation and digit groups. This prevents frequent words from merging indiscriminately across spaces and keeps numbers segmented predictably, although digit-splitting choices still strongly affect arithmetic accuracy.
The vocabulary size trade-off
Modern open-weight models have shifted from 32K vocabularies to 100K, 256K byte-level BPE vocabularies to improve multilingual and code efficiency. Larger vocabularies compress Turkish and technical jargon into fewer tokens, accelerating generation and extending effective context, at the cost of a larger embedding and output projection matrix in GPU memory.
Special control tokens and security
Special tokens (such as BOS, EOS, padding and chat-role delimiters) sit outside the statistical BPE merge table and are matched directly by the tokeniser pipeline. Improper handling of raw special-token strings inside untrusted user text can enable delimiter smuggling and prompt-injection attacks.
Commonly confused terms
| Term | How it differs |
|---|---|
| WordPiece | BPE builds a vocabulary bottom-up by greedily merging the most frequent adjacent pairs and applies merges deterministically, whereas WordPiece chooses merges that maximise training-corpus likelihood and marks continuation subwords (e.g. ##). |
| SentencePiece | BPE is a deterministic subword tokenisation algorithm, whereas SentencePiece is a language-independent library that implements both BPE and Unigram directly on raw Unicode streams without requiring language-specific pre-tokenisation. |
Why BPE (Byte Pair Encoding) matters for on-premises AI
BPE underlies most common language model tokenisers, so how many tokens a model needs for Turkish depends largely on the data its vocabulary was learned from. Merges learned from a corpus with little Turkish leave common suffixes in pieces, so suffixed words need more tokens. On-premise capacity planning, context window calculations and cost per token are therefore measured with the model's own tokeniser.
A bank runs its product names and domain terms through a candidate model's tokeniser; finding that frequent terms break into many pieces, it considers vocabulary extension before fine-tuning.
Common questions
What is the difference between BPE and Unigram tokenisation?
BPE grows the vocabulary by merging the most frequent pairs and splits each text one way. Unigram prunes the vocabulary with a probability model and can score several splits of the same word.
What is byte-level BPE for?
Because it starts from bytes rather than characters, it represents any Unicode text without unknown symbols. Turkish characters can also be handled as bytes.
Related terms
Sources
- Sennrich, Haddow and Birch, Neural Machine Translation of Rare Words with Subword Units, 2016
- Kudo and Richardson, SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing, 2018
In the glossary: Glossary, A to C