Summary
Tokenization splits text into discrete units (tokens). Modern LLMs use subword schemes (BPE, WordPiece, SentencePiece) that decompose rare words into subword pieces while preserving common words as single tokens.
- Tokenization defines the model’s input and output space, it’s irreversibly baked into a trained model.
- Token count drives both cost and latency at inference. Different tokenizers give different counts for the same text (sometimes by 30%+).
- Tokenization mismatches between training and serving cause silent quality regressions that are hard to debug.
- Production LLM bugs in multilingual quality, code performance, and unusual-character handling often involve tokenization.
The main families
BPE (Byte-Pair Encoding)
Original from text compression (1994), adapted for NLP. Algorithm:
- Start with a vocabulary of single characters.
- Find the most frequent adjacent pair in the corpus.
- Merge that pair into a new vocabulary item.
- Repeat until target vocabulary size.
Learning objective: trace how corpus-wide pair counts create ranked, reusable BPE tokens that compress common words while leaving rarer words as multiple pieces.
Learning objective · toy byte-level corpus
How does one frequent byte pair grow into reusable subword and whole-word tokens?
START · COUNT ADJACENT PAIRS ACROSS ALL OCCURRENCES
Corpus: m a p × 5 · m a p s × 3 · c a p × 2
Largest count: a + p = 5 + 3 + 2 = 10
Rank 1 merge: add ap to the vocabulary.
ROUND 2 · RECOUNT AFTER APPLYING a + p → ap
Corpus now: m ap × 5 · m ap s × 3 · c ap × 2
Largest count: m + ap = 5 + 3 = 8
Rank 2 merge: add map to the vocabulary.
ROUND 3 · RECOUNT AFTER APPLYING m + ap → map
Corpus now: map × 5 · map s × 3 · c ap × 2
Largest count: map + s = 3
Rank 3 merge: add maps to the vocabulary.
APPLY · REPLAY MERGES IN LEARNED RANK ORDER
maps → [maps] · common whole wordcap → [c] [ap] · shared learned endingtraps → [t] [r] [ap] [s] · rarer word, more pieces
ap forms before map, and map before maps. Frequent patterns therefore become larger tokens; an unfamiliar word still falls back to available byte pieces. Original worked example informed by Sennrich et al. (2016) and the tiktoken documentation.Result: common words become single tokens; rare words decompose into subwords; arbitrary text is always representable (down to characters).
Used in: GPT-2/3/4, LLaMA family, most modern LLMs.
Byte-level BPE
BPE applied at the byte level, not the character level. Vocabulary starts with 256 byte values; merges happen on byte sequences. The advantage: any UTF-8 text is representable without unknown tokens, and the base vocabulary is fixed.
Used in: GPT-2 onward, RoBERTa, most modern LLMs. This is what “BPE” almost always means in 2026.
WordPiece
Similar to BPE but uses a likelihood-based merging criterion (maximize the probability of the corpus given the vocabulary) instead of frequency. Tokens not at word start are prefixed with ##.
Used in: BERT, distilBERT.
SentencePiece (Unigram)
A different paradigm: starts with a large vocabulary, removes pieces that contribute least to corpus likelihood. Trains on raw text with no pre-tokenization (handles whitespace and punctuation as part of the algorithm). Implementation by Google, often paired with their models.
Used in: T5, mT5, ALBERT, multilingual models.
Tiktoken / cl100k / o200k
Tiktoken is OpenAI’s BPE implementation. Their tokenizers (cl100k_base, o200k_base) are byte-level BPE optimized for code and multilingual text. Available open-source.
What an interviewer expects you to say
If asked about tokenization:
- Define subword tokenization and why it solves the OOV (out-of-vocabulary) problem.
- Explain BPE algorithm (frequency-based merging).
- Distinguish character-level vs byte-level BPE.
- Mention vocab size trade-offs (small vocab → more tokens per word; large vocab → more parameters in embedding/output layer).
- Mention that tokenization choice is fixed at training time: you can’t change it post-training without retraining.
Bonus depth: mention the multilingual tokenization issue (English-trained tokenizers have very different per-character costs for non-Latin scripts, sometimes 5-10x more tokens for the same content).
Common confusions
- “Tokens are words.” No. Tokens are arbitrary pieces. “tokenization” itself might be one or two tokens depending on the tokenizer.
- “Switching tokenizer = small change.” It’s a categorical change. The model has to be retrained.
- “All tokenizers are roughly equivalent in cost.” No. cl100k vs LLaMA tokenizer can give 10-30% different token counts on the same text. For multilingual and code-heavy content, the gap is much larger.
- “BPE is the same as WordPiece.” Similar idea, different merge criterion (frequency vs likelihood). The differences in practice are small.
The LLM-era tokenization gotchas
A few tokenization issues that bite in production:
- Tokenizer drift between training and serving. Different versions of the same tokenizer family can subtly differ. Always pin the exact tokenizer version with the model.
- Inconsistent BOS/EOS tokens. Different chat-tuned models expect different special tokens for system prompts, user turns, assistant turns. Send the wrong format → quality regression. Use the model’s chat template, not your own.
- Multilingual cost asymmetry. English text is ~4 chars/token; Japanese can be ~1 char/token; some scripts are even worse. A 1000-character Japanese document can cost 10x more than a 1000-character English document.
- Code tokenization. Code-aware tokenizers handle indentation, common keywords, and operators efficiently. Non-code tokenizers (older BERT, etc.) treat code very inefficiently.
- Unicode normalization. Different unicode normalizations of the “same” string (composed vs decomposed) can produce different token sequences. NFC normalization is standard but worth verifying for your domain.
- Number tokenization. Some tokenizers split numbers digit-by-digit (great for arithmetic), others treat numbers as multi-digit tokens (worse for arithmetic). LLaMA and modern models tend to split digits.
Why interviewers ask
Tokenization questions test:
- Whether you know the algorithm vs just the vocabulary file.
- Whether you’ve debugged a real tokenization bug in production.
- Whether you know modern LLM tokenization choices (byte-level BPE, digit splitting, code-aware vocab).
- Whether you’re aware of multilingual fairness/cost issues.
A common follow-up asks how to handle an inefficient tokenizer for medical, legal, or code text. Extend the tokenizer and continue pretraining, or train a new tokenizer on domain text and retrain the model.
Related: Transformer architecture, LLM Evals essay.