Zenaique

Pre-tokenization splits text before BPE merges. Why is that boundary important?

Flashcard·Easy·4.0 · 0·~30s·Asked atSapSourcegraphTypeface·Relevant atOpenAI
Attempt it
TL;DR

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.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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 come from a specific regular expression that the tokenizer's designers picked carefully. 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.

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.

Pre-tokenization in the pipeline

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.

The hard boundary constraint
The cl100k_base regex, in detail
What o200k_base changed
Downstream consequences of the regex
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

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.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy do cl100k_base and o200k_base use different regex patterns?
A

o200k_base shipped with GPT-4o in 2024, with a vocabulary about twice as large and much better multilingual compression. The regex changes are: contractions attach to the preceding word instead of forming their own chunks; letter classes are case-aware and include combining marks, so an uppercase run followed by lowercase letters is split; and punctuation runs can absorb a trailing '/'. Digit grouping is the same in both encodings (runs of one to three digits). Compare token counts under both encodings on your own text to see the effect.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

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.

Sign in to see all red flags and common mistakes.

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).

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Why use subword tokens, and what is wrong with word level or character level?
Flashcard·Easy