What is the primary purpose of regex-based pre-tokenization in cl100k_base, and what constraint does it impose on BPE?
Pre-tokenization splits raw text into coarse chunks first, and those chunk edges become hard walls that BPE merges can never cross.
Imagine cutting a long ribbon into shorter pieces before you start gluing beads onto each piece. Once the ribbon is cut, you can only glue beads that sit on the same piece, never across a cut. Pre-tokenization is that cutting step: a pattern slices the text at spaces, punctuation, and digit-letter changes. Then the merging step that glues frequent neighbours together only works inside each slice. So a token like a word can form, but a token that spans 'word + space + next word' cannot, because the cut sits between them.
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.
3 min: regex splits raw text + chunk edges are hard walls + BPE merges only inside a chunk + no cross-word tokens + leading-space rule + cl100k vs o200k regex differences + pin encoding per model.
import regex as re
# A simplified cl100k_base-style pre-tokenization pattern.
# The real pattern also handles contractions, leading spaces, digits.
pat = re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+""")
text = "Hello world 42!"
chunks = pat.findall(text)
print(chunks) # ['Hello', ' world', ' 42', '!']
# BPE merges run INSIDE each chunk only.
# It can build a token for ' world', but never for 'Hello world'
# because the two sit in different chunks.Real products, models, and research that use this idea.
- OpenAI's tiktoken ships the cl100k_base and o200k_base regex patterns directly in its source, and o200k_base powers the GPT-5.5 family.
- HuggingFace tokenizers exposes pluggable pre-tokenizers (ByteLevel, Whitespace, Split), letting you compose the same boundary logic for custom BPE vocabularies.
- Llama 4's HuggingFace tokenizer.json encodes its own pre-tokenization rules, which is why its token boundaries differ from OpenAI encodings on the same text.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat would the vocabulary look like if you dropped pre-tokenization entirely and let BPE merge across spaces?
QHow do cl100k_base and o200k_base regex patterns differ, and what user populations does that affect?
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.
Thinking pre-tokenization cleans or normalizes the text by stripping punctuation or lowercasing, when its real job is to draw merge boundaries BPE cannot cross.
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.