Why does a 4-space Python indentation block often tokenize to a single token in cl100k_base?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Four spaces are one of the most frequent byte sequences in Python source, so BPE merged them into a single dedicated token rather than four space tokens.
Imagine a stenographer who invents shorthand for whatever phrases come up most. After transcribing thousands of Python files, they notice 'four spaces at the start of a line' shows up constantly, so they create one quick squiggle for it instead of writing four marks every time. The tokenizer does the same thing: because Python uses four-space indentation everywhere, the four-space run became so common during training that it earned its own single symbol, called a token. That one squiggle now stands in for what used to be four.
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.
2 min: BPE merges most-frequent pair + four-space run is ubiquitous in code training data + regex keeps the run intact + emergent not hardcoded + tabs/odd widths fragment.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
# A single level of Python indentation: four spaces
print(enc.encode(" ")) # often one token id
# Inside real code, the indent stays cheap
src = "def f(n):\n return n + 1\n"
print(len(enc.encode(src))) # leading indent contributes ~1 token
# Non-standard indentation fragments
print(enc.encode(" ")) # three spaces: may be 1-3 tokens
print(enc.encode("\t")) # a tab: separate entry, different costReal products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming the tokenizer has a hardcoded rule for indentation. It does not; the four-space token emerges purely from how often that byte sequence appeared in the training data.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.