Beyond BPE: The Tokenizer Landscape
BPE (Byte Pair Encoding) is dominant, the algorithm that iteratively merges the most frequent character pairs, covered in its own lesson. But it's not alone. Depending on the model you're using, the tokenizer, the program that cuts text into tokens, the elementary units a model manipulates, can be completely different:
- BERT (Bidirectional Encoder Representations from Transformers): WordPiece
- T5 (Text-to-Text Transfer Transformer), ALBERT (A Lite BERT), XLNet: SentencePiece + Unigram
- GPT-2/3/4 (Generative Pre-trained Transformer): BPE via tiktoken
- LLaMA, Mistral: BPE via SentencePiece
Why so much variety? Because each algorithm makes different trade-offs. Some prioritize linguistic coherence, others multilingual universality, others probabilistic robustness. Choosing the right tokenizer for your use case has a direct impact on model performance, training speed, and even your bill, since model providers charge per token.
Let's see what each one brings to the table.
WordPiece: BERT's Algorithm
Originally developed by Google for speech recognition, WordPiece (sometimes abbreviated WPM, for WordPiece model) was later adopted for BERT (Bidirectional Encoder Representations from Transformers) and for its direct descendants such as DistilBERT and ELECTRA. It's a very close cousin of BPE, but with a subtly different merging philosophy.
One caveat: not every model in the BERT family inherited WordPiece. RoBERTa switched to byte-level BPE, and ALBERT to SentencePiece, both described below.

Key Difference: The Merge Criterion
BPE merges the most frequent pairs. WordPiece merges pairs with the best likelihood. The nuance is important.
| Algorithm | Merge criterion |
|---|---|
| BPE | Merge (a, b) if is maximal |
| WordPiece | Merge (a, b) if is maximal |
Reading the symbols: is how many times piece is immediately followed by piece in the corpus. is the probability of meeting piece anywhere, the same for , and the probability of meeting them stuck together. The denominator is therefore the probability that they end up adjacent by pure chance, if their positions were independent. So the fraction reads: "how many times more often than chance". Statistics has a name for this quantity: its logarithm is the pointwise mutual information (PMI). The averaged version, mutual information, has its own lesson.
Intuitive translation: WordPiece asks "do A and B appear together more often than chance would predict?" It's a measure of statistical association, not just raw frequency. Imagine that "a" and "s" often appear together, but that's mainly because "a" and "s" are individually very frequent. Their co-occurrence isn't surprising. In contrast, "i" and "ng" appear together far more than their individual frequencies suggest: it's the verbal suffix "-ing", a real linguistic unit.
Concrete example:
- BPE: merges (a, s) because this pair is the most frequent in the corpus
- WordPiece: merges (i, ng) because "ing" is a statistically surprising association, even if it's less frequent in absolute numbers
The result: more linguistically coherent subwords. WordPiece tends to discover true morphemes (prefixes, suffixes, roots) rather than arbitrary fragments.
The ## Marker for Position
WordPiece uses a ## marker to indicate that a subword is not at the beginning of a word:
| Word | WordPiece tokens |
|---|---|
| "tokenization" | ["token", "##ization"] |
| "unhappiness" | ["un", "##happi", "##ness"] |
Why is this useful? Because it preserves position information within the original word. The model knows that "cat" at the start of a word (the word "cat" by itself) and "##cat" at the end of a word (as in "tomcat") are two different things. Simple but devastatingly effective for linguistic understanding.
Typical vocabularies:
- English BERT: ~30k tokens
- Multilingual BERT: ~119k tokens (to cover 104 languages)
SentencePiece: The Universal Solution
Developed by Google, SentencePiece isn't just a tokenization algorithm. It's a complete software library that solves a problem BPE and WordPiece ignore.
The Fundamental Problem: Pre-tokenization
BPE and WordPiece assume that you first split the text into words (typically by separating on spaces). That works well for English or French. But this assumption poses three serious problems:
The first problem is that some languages simply don't put spaces between words. Chinese, Japanese, and Thai write everything together, making it impossible to separate "猫は寝ている" into words without knowing Japanese grammar.
| Language | Example | Spaces |
|---|---|---|
| English | "The cat sleeps" | Clear spaces between words |
| Chinese | "猫は寝ている" | Zero spaces, words glued together |
The second problem is that pre-tokenization introduces cultural biases: languages with spaces are favored, while agglutinative languages (Turkish, Finnish) or spaceless languages are penalized from the very first step of the pipeline.
Finally, the third problem concerns inconsistencies between implementations. Handling of apostrophes, hyphens, and punctuation varies from one system to another, creating subtle but real differences between models.
The Solution: Process RAW Text
SentencePiece bypasses all of this by treating raw text as a stream of characters, without assuming any word structure. Spaces are not special separators: they're just another character, represented by ▁ (an unusual underscore, picked because it almost never occurs in real text):
| Language | Text | SentencePiece tokens |
|---|---|---|
| English | "I eat an apple" | ["▁I", "▁eat", "▁an", "▁apple"] |
| Chinese | "猫は寝ている" | ["▁猫", "は", "▁寝", "ている"] |
The space is inside the token, not between tokens. This approach means that the same algorithm, the same code, and the same pipeline work for every language in the world, without any special cases or language-dependent rules of thumb.
SentencePiece Is More Than an Algorithm
Concretely, SentencePiece is a Python/C++ library that implements several algorithms inside:
- BPE (used by LLaMA, Mistral)
- Unigram (used by T5, ALBERT, XLNet)
Same library, same Unicode normalization, same space handling. Only the segmentation algorithm changes behind the scenes. That's why you often see "SentencePiece + BPE" or "SentencePiece + Unigram": SentencePiece handles the universal part (raw text processing), and the chosen algorithm handles the segmentation.
Unigram: The Probabilistic Algorithm
Unigram takes a radically different approach from BPE. Where BPE builds its vocabulary by merging pairs, Unigram does exactly the opposite.
Construction: Top-down, Not Bottom-up
BPE starts small and progressively merges (bottom-up). Unigram starts with a gigantic vocabulary and prunes (top-down):
- Step 1: Create ALL possible subwords from the corpus ("t", "th", "the", "they", "he", "hey", etc.), you start with a huge vocabulary, sometimes millions of entries
- Step 2: Calculate the optimal probability for each subword (via the EM algorithm, Expectation-Maximization, an iterative statistical estimation method)
- Step 3: Iteratively remove the subword whose removal does the least damage to the likelihood of the entire corpus
- Step 4: Repeat until reaching the desired vocabulary size (typically 30-100k)
It's like a sculptor who starts with a block of marble and removes what's not needed, rather than a mason who stacks bricks.
Strength: Probability
Each subword in the Unigram vocabulary has an associated probability. For a given word, multiple splits are possible, each with its own probability. The numbers below are made up for the illustration, they come from no real model:
| Split of "running" | Tokens | Probability |
|---|---|---|
| Split 1 | ["run", "ning"] | 0.7 |
| Split 2 | ["ru", "nn", "ing"] | 0.2 |
| Split 3 | ["r", "u", "nn", "ing"] | 0.1 |
BPE gives you only one split (deterministic). Unigram gives you a probability distribution over all possible splits. At use time, the Viterbi algorithm picks the most probable path, a classic algorithm that finds the best sequence of choices without enumerating them all.
Bonus: Subword Regularization
This is where Unigram really shines. During training, instead of always using the most probable split, you can sample alternative splits. The same word is sometimes split into 2 tokens, sometimes 3, sometimes 4. This variability forces the model not to depend on a specific split.
The result: the model becomes more robust to tokenization variations. If a word appears in a slightly different context and ends up split differently, the model still knows how to handle it. Generalization improvement at zero cost. Elegant.
Direct Comparison
| Aspect | BPE | Unigram |
|---|---|---|
| Direction | Bottom-up (merge) | Top-down (prune) |
| Criterion | Frequency | Likelihood |
| Split | Deterministic (1 only) | Probabilistic (multiple) |
| Native regularization | No | Yes (subword regularization) |
| Used by | GPT, RoBERTa, LLaMA | T5, ALBERT, XLNet |
tiktoken: OpenAI's Pro Implementation
tiktoken is not a new algorithm. It's a heavily optimized implementation of byte-level BPE written in Rust, used by GPT-3.5, GPT-4, and the entire OpenAI API (application programming interface). The algorithm is the same as BPE; only the code differs, and it is significantly faster than Python implementations, OpenAI's own library documentation puts the factor at roughly 3 to 6.
Multiple Encodings Available
tiktoken offers several encodings (i.e., different vocabularies, trained on different data):
| Encoding | Model | Vocabulary size |
|---|---|---|
| cl100k_base | GPT-3.5, GPT-4 | about 100,000 tokens |
| o200k_base | GPT-4o | about 200,000 tokens |
Warning: each OpenAI model has its own encoding. Using the wrong one to count tokens gives incorrect results, and therefore a badly estimated bill, since pricing is per token. The gap between two encodings on the same text is not negligible; measure it yourself on a sample of your own data rather than trusting a general rule of thumb.
How to Use It
import tiktoken
# Load the tokenizer for GPT-4
enc = tiktoken.encoding_for_model("gpt-4")
# Count tokens
text = "Hello, world!"
tokens = enc.encode(text)
print(f"{len(tokens)} tokens") # Displays the count
# Decode (useful for debug)
decoded = enc.decode(tokens)
print(decoded)
An indispensable tool before calling the OpenAI API, to estimate real costs and avoid nasty surprises on the bill.
The Hidden Impact of the Tokenizer
The choice of tokenizer affects everything invisibly. Here are the three areas where it hurts the most:
Fertility: The Curse of the Non-native Tokenizer
A tokenizer trained mostly on English needs appreciably more tokens to encode French, Arabic, or Chinese, a factor that often exceeds 2. This is called the tokenizer's fertility: the average number of tokens it produces per word.
| Tokenizer | "Bonjour" | Number of tokens |
|---|---|---|
| BPE (trained on English) | ["Bon", "##jour"] | 2 tokens |
| BPE (trained on French) | ["Bonjour"] | 1 token |
The consequences are concrete, and the arithmetic is simple: if French costs twice as many tokens per word, a 4,000-token window holds only the equivalent of 2,000 tokens of English content. You pay the API twice as much for the same text, and the useful context shrinks by the same factor. This is one of the reasons models trained mostly on English are generally better in English than in French.
Arithmetic
Large language models (LLMs) are notoriously bad at math, and the tokenizer is partly to blame. The number "380" can be tokenized in different ways depending on context:
| Option | Tokens for "380" | Description |
|---|---|---|
| Option 1 | ["3", "80"] | The 3 is isolated, 80 together |
| Option 2 | ["38", "0"] | 38 together, 0 isolated |
| Option 3 | ["380"] | All in a single token |
The model sees different tokenizations for the same digits depending on context. There's no consistent structure: "3" in "380" and "3" in "3000" are not represented the same way. Arithmetic operations become chaotic, because the model never sees digits uniformly.
Code
Spaces and indentation consume enormous numbers of tokens with naive tokenizers: in a deeply indented Python file, a large share of the tokens encode nothing but whitespace. That's why recent tokenizers (like GPT-4's) have specific optimizations for code: they group several consecutive spaces into a single token. It's also why specialized code models (CodeLlama, StarCoder) use tokenizers specifically trained on code.
The Verdict: No Universal Winner
Each tokenizer excels in a domain, and that's why they coexist:
- WordPiece: understanding (BERT), linguistically coherent subwords
- SentencePiece: multilingual universality (T5, LLaMA), zero language assumptions
- Unigram: robustness via probabilities and regularization, split flexibility
- BPE/tiktoken: GPT and mainstream models, byte-level universal, ultra-optimized
Why This Matters
The tokenizer is the first critical link in the chain. Before the model sees anything, the text passes through the tokenizer. Its choices propagate throughout the entire network: a bad split, fertility that's too high, an inadequate vocabulary, and performance degrades invisibly.
When your model goes haywire on a task, the classic reflex is to blame the architecture or the data. But often, the culprit hides at the very beginning of the pipeline. Check the tokenization before anything else.
1. How do the merge criteria of BPE and WordPiece differ?
2. What exactly is SentencePiece?
3. What fundamentally sets Unigram apart from BPE?
4. Why must you use the right tiktoken encoding (cl100k_base, o200k_base, and so on) to count tokens?