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.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Pre-tokenization is the step in the tokenizer pipeline that most users never look at directly but that quietly shapes everything downstream. It is the regex or rule set that splits the input string into coarse chunks before the BPE merge algorithm runs. The choice of regex determines what merges are even possible to learn, which in turn determines how the tokenizer behaves on real-world text.
This deep dive places pre-tokenization in the pipeline, explains why it imposes a hard constraint on the merge algorithm, walks through the cl100k_base and o200k_base regex patterns, and connects pre-tokenization choices to downstream behaviors like the leading-space token effect.
Pre-tokenization in the pipeline
Modern tokenizer pipelines have four sequential stages. Normalization runs first (a string transformation for Unicode and case handling). Pre-tokenization runs second (splitting the string into a list of chunks). The model runs third (BPE merges, or SentencePiece-Unigram segmentation, operating on each chunk independently). Post-processing runs last (insertion of special tokens like BOS, EOS, role markers).
Pre-tokenization is the bridge between text and the merge algorithm. Its input is a string. Its output is a list of strings, each of which the merge algorithm will tokenize separately. The chunks are usually word-like: a leading whitespace plus a sequence of letters, or a punctuation character, or a digit sequence, or a piece of whitespace.
The Hugging Face tokenizers library makes this stage explicit. A tokenizer.json file has a pre_tokenizer field that describes the splitter as a structured config: which regex to use, how to handle whitespace, whether to preserve byte-level encoding. The tiktoken library does the same thing in code: each encoding (cl100k_base, o200k_base) has a published regex that runs as the pre-tokenization step.
The key consequence of this pipeline shape is that the merge algorithm never sees a single coherent input string. It sees a list of chunks. Adjacent characters in the original input that fall into different chunks are not adjacent from the merge algorithm's perspective; they are in different lists and cannot be combined.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy do cl100k_base and o200k_base use different regex patterns?
o200k_base was designed for GPT-4o and the o-series reasoning models, which needed better handling of numbers (for arithmetic), better multilingual coverage, and slightly different code behavior. The regex changes (digit grouping, Unicode handling, apostrophe tweaks) are calibrated against measurable downstream performance. cl100k_base was good enough for GPT-3.5 and GPT-4; o200k_base is incrementally better for the use cases the o-series targets.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases 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.
60 second bullets to scan on the way to the call.
Define pre-tokenization in one sentence.
Identify where it sits in the tokenizer pipeline (after normalization, before merging).
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.