Pre-tokenization splits text before BPE merges. Why is that boundary important?
Pre-tokenization is the regex step that splits raw text into coarse chunks before BPE merging. It enforces hard boundaries: merges can never cross them, which shapes what the vocabulary can contain.
Imagine BPE is a robot that walks through your text and glues adjacent pieces together into bigger units. Without any rules, the robot might glue 'hello' and 'world' together into a single weird unit because the space between them is just another character. Pre-tokenization is the step that draws walls in the text first: here is one word-ish chunk, here is the next, here is a punctuation mark on its own. The merge robot can glue pieces together inside a chunk but never across a wall. The walls are drawn by a specific regular expression that the tokenizer's designers picked very carefully, because the choice of walls limits what kinds of merged tokens can ever exist in the vocabulary.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: define pre-tokenization, place it in the pipeline between normalization and merging, explain the hard-boundary constraint on the merge algorithm, describe the regex pattern at a high level, contrast cl100k_base and o200k_base regexes briefly, and connect to the leading-space token effect.
Real products, models, and research that use this idea.
- OpenAI's tiktoken source publishes the exact regex for cl100k_base and o200k_base; you can inspect them in the encoding registry and even compile and apply them yourself in Python.
- The cl100k_base regex specifically handles English contractions (s, t, re, ve, m, ll, d) as their own chunks, which is why 'don't' tokenizes consistently across casing variations.
- o200k_base introduced number grouping by threes, improving numeric handling for the o-series reasoning models like o1 and o3 that needed better arithmetic encoding.
- Hugging Face's tokenizers library exposes pre-built pre-tokenizers (Whitespace, Punctuation, Digits, ByteLevel, Sequence) that can be composed; the ByteLevel pre-tokenizer is what tiktoken-style tokenizers use under the hood.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy do cl100k_base and o200k_base use different regex patterns?
QWhat is the relationship between the pre-tokenization regex and the leading-space token effect?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Confusing pre-tokenization with tokenization. Pre-tokenization is the splitting step that runs before BPE merges; the merges then operate within each pre-tokenization chunk separately.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.