What the split before the merges is for
RolesLLM Product EngineeringAI Data SystemsInference Engineering
The question
Before any merge runs, a production tokenizer cuts the text into chunks with a regular expression. Show me what the classic pattern does to Hello, world! Don't stop. and explain why the cut exists at all, given that the merges could run on the raw stream.
Answer it first, aloud or on paper. Then open the answer.
The answer
Show short answer
The pattern cuts the sentence into eight chunks, Hello, ,, world, !, Don, 't, stop and .: each word after the first keeps the space in front of it, contractions split at the apostrophe, and punctuation stands alone. The merges then run inside each chunk and can never join symbols across a chunk boundary. Without the cut, training learns merges that span words, the vocabulary fills with whole phrases, and the pieces generalize worse to new text.
Show full answer
The pattern
'(?:[sdmt]|ll|ve|re)| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+In order: common contraction endings, a word with an optional leading space, a run of digits with an optional leading space, a run of punctuation, and whitespace. The \p{L} and \p{N} classes need the third-party regex module; the standard library falls back to ASCII classes, which is wrong for multilingual text.
Why the leading space travels with the word
hello and hello are different tokens. Keeping the space on the word means a word-initial piece and a word-internal piece stay distinct, and the decoder can rebuild the spacing from the tokens alone. The same reason forces every caller to be explicit about whether a prefix space is added at the start of a prompt.
Why cut at all
The merge loop is greedy on frequency. In a raw stream the pair "e" followed by a space followed by "t" is frequent, so the loop would happily learn a piece that ends one word and starts the next. Such pieces do not transfer: the next text has different word pairs. Cutting first keeps every merge inside a word, which is where the regularities that generalize live.
The other design
A tokenizer that works on raw text treats the space as an ordinary symbol and lets the merge or prune algorithm find the boundaries. It needs no language rules and handles scripts without spaces, and it allows cross-word pieces, which can compress a little better and read a little worse.
The capstone's version
A small split on whitespace and punctuation. It is enough to keep merges inside words for a small training set, and the rest of the pipeline stays the same.
Follow-ups
L2WorkingWhy does a prompt that starts without a space tokenize differently from one that starts with a space?
Show expected answer
the space is part of the word-initial token, so the first word maps to a different id; set the prefix-space behavior explicitly.
L3SeniorWhy does the
regexmodule matter for this pattern?Show expected answer
the Unicode letter and number classes cover scripts beyond ASCII, which the standard library's engine lacks.
L3SeniorWhat is the trade in skipping the cut and running on raw text?
Show expected answer
no language rules and good behavior for scripts without spaces, against cross-word pieces that compress slightly better and are less interpretable.
L3SeniorA tokenizer file is unchanged but ids differ between two machines. What in this stage can cause it?
Show expected answer
one machine fell back to the ASCII pattern because the
regexmodule was missing, so the chunks differ.
Wrong answers
Show the wrong answers that interviewers hear
“Pre-tokenization is just splitting on spaces.”
The pattern also handles contractions, digits, punctuation and runs of whitespace, and it keeps the space attached to the word.
“The cut is cosmetic; the merges would find the same pieces.”
Without the cut the merges cross word boundaries and the vocabulary fills with phrases.
“Attach punctuation to the word it follows.”
Then
world!andworldare different pieces, and the vocabulary pays for every punctuation variant.
Taught in
The tokenizer build lesson gives the pattern and its output, the BPE capstone explains why merges stay inside words, and the subword lesson lists the whitespace pitfall.