Predict the token count difference between 'def fibonacci(n):' and 'def f(n):' in cl100k_base.
Using tiktoken with cl100k_base, predict the approximate number of tokens for: - 'def fibonacci(n):' - 'def f(n):' Explain why the difference exists and what it implies for LLM code generation token budgets.
A long, rare identifier like 'fibonacci' fragments into several subword tokens, while the keyword 'def' and the terse 'f' are single tokens, so identifier choice quietly drives code token counts.
Imagine a typist with shorthand symbols for common words. 'def' and a lone 'f' show up so often that each gets one quick symbol. But a longer, rarer name like 'fibonacci' has no single symbol, so the typist has to write it in a few familiar chunks, like 'fib', 'on', and 'acci'. More chunks means more strokes. The tokenizer works the same way: short, common pieces become one token, while long, uncommon names get broken into several pieces, costing more.
Detailed answer & concept explanation~6 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: decompose 'def' + ' f' + '(n):' vs ' fibonacci' split + frequency drives merges + leading-space tokens + answer as a range + identifier-length tax at scale.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
long_id = "def fibonacci(n):"
short_id = "def f(n):"
print(len(enc.encode(long_id))) # ~5-6 tokens
print(len(enc.encode(short_id))) # ~4 tokens
# Inspect the actual split of the rare identifier
for tid in enc.encode(" fibonacci"):
print(tid, repr(enc.decode([tid]))) # shows subword fragmentsReal products, models, and research that use this idea.
- The OpenAI tokenizer playground shows 'fibonacci' splitting into multiple cl100k_base tokens while 'f' stays one.
- Coding assistants like Cursor and GitHub Copilot budget context partly around identifier verbosity in the user's open files.
- Models such as GPT-5.5 and Claude Opus 4.7 inherit code-aware tokenizers, but long identifiers still fragment under their encodings.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would identifier verbosity change your token budget for a 50-file code-review prompt?
QWould renaming variables to single letters meaningfully cut cost, and is it worth it?
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.
Counting one token per word, so estimating both snippets at four tokens. The long identifier 'fibonacci' actually splits into several subword pieces and costs more.
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.