Tokenization: why it miscounts letters but restructures a file fine
The model never sees the letters in your string. It sees a short list of integers, and "strawberry" is two or three of them. I build Settl, an expense-splitting app, solo with Claude, and this cost me a group name that overflowed its card because a "trim to 30 characters" was quietly guessed.
What the model actually receives
Your text goes through a tokenizer first. It holds a fixed vocabulary, typically 100,000 to 200,000 entries, built by merging frequent character pairs until the vocabulary is full. Your input is chopped into those entries and each one becomes an integer.
"strawberry" -> ["straw", "berry"] -> [496, 15717]
" strawberry" -> [" straw", "berry"] -> different ids, the space is in the token
"Strawberry" -> ["Str", "aw", "berry"] -> different again, casing changes the split
"getUserById" -> ["get", "User", "By", "Id"]
"1234567" -> ["123", "4567"] -> digit groups, not digits
Three consequences worth internalising:
- Splits are frequency-based, not linguistic. They follow neither syllables nor morphemes.
Nothing about
strawplusberrywas designed. It just happened to be cheap. - Whitespace and casing live inside the token.
" the"and"the"are different integers entirely. - Numbers split arbitrarily. Long-number arithmetic fails for the same reason, far less visibly.
Why meaning survives and spelling does not
Each token id becomes a vector learned from how that token gets used. It carries an enormous amount about meaning, because meaning is what usage encodes. It carries almost nothing reliable about the characters inside, because character identity is hardly ever the thing being predicted.
So straw plus berry still points at the fruit. Structure, intent, types and control flow all
survive tokenization intact, which is why the same model that miscounts rs can restructure a
600-line module correctly. Nothing in that job requires looking inside a chunk.
Spelling is recoverable only where it was written out in training text: word games, dictionary entries. That is memorisation, not counting, which is why common words come out right and an unusual identifier is a coin flip.
The tasks that quietly break
Each of these returns a confident, correctly-shaped, wrong answer:
- Character limits. "Trim to 30 characters", "keep it under 160". You get a string in the right ballpark and a number that was never measured.
- Reversing, palindromes, anagrams. All character-level, all guesswork.
- Arithmetic on long numbers. The tokenizer already split them into inconsistent groups.
- Anything non-Latin or emoji. One visible character can be several tokens, and separately one
emoji can be several code points, so
"👍🏽".lengthis 4 in JavaScript. "Length" is already ambiguous before the model is involved.
Move the counting to the computer
The answer is not a better prompt for counting. It is refusing to let the model be the counter.
Rule for this session: never state a character count, byte count, digit
count or string length from your own reading of the text.
If a count matters, write the code that computes it, run it, and quote the
actual output. If you cannot run it here, give me the one-line command to
run myself. Never estimate a count.
Put that rule in your CLAUDE.md and it becomes standing policy instead of something you retype
every session (settl.fyi/social/tip-claude-md).
For anything with a hard limit, make the limit a test rather than an instruction:
Write the truncation helper, then a test asserting truncate(name, 30)
returns exactly 30 characters for a 100-character input and leaves short
inputs unchanged. Run it, paste the raw output. Use Array.from(str).length,
not str.length, so astral characters count once.
The escape hatches
When you cannot run code, force one token per character:
Spell the word out with a space between every letter, one letter per line,
numbered from 1. Then count only the lines whose letter is "r". Show me the
numbered list before the total.
Spacing makes each character its own token, and numbering turns counting into sequence prediction, which the model is good at. It has a real ceiling: it degrades on long strings. Use it once, never in a loop.
When tokenization is not your problem
The strawberry explanation is popular enough that people reach for it too often. It explains character-level and digit-level failures. It does not explain:
- A wrong algorithm. That is reasoning, which fails for different causes.
- A function that does not exist. Ungrounded prediction
(
settl.fyi/social/ai-invented-a-function). - A model that forgot your schema at message 200. The context window
(
settl.fyi/social/why-long-chats-get-worse).
There is a clean test for which one you have. Ask the same question with the input spaced out character by character. If the answer becomes right, it was tokenization. If it stays wrong, the cause is elsewhere and no amount of spacing will save you.
Run this on your codebase
Paste this into a session at your repo root:
Find every place in this codebase where a length or character count matters,
and tell me whether it is computed or assumed.
1. Search for truncation, slicing, padding, .length, substring, slugify, and
hard-coded limits like 30, 140, 160, 255, 280.
2. For each hit, state which unit it counts: UTF-16 code units, code points,
grapheme clusters, or bytes. Flag every place two of those disagree,
especially user-facing fields that can hold emoji or non-Latin script.
3. Flag every limit living only in a comment, a prompt, or a CSS width
rather than in an assertion.
Change nothing yet. Give me a table of file, line, unit, risk. Then pick the
highest-risk row and write the failing test that pins its real behaviour.