All courses

Behavioral artifacts of tokenization

Predict tokenization-caused failures: counting letters, reversing strings, digit arithmetic, leading-space variants, and higher token cost for non-English text. Explain each from the fact that the model never sees characters directly.

Builds on: Byte-pair encoding tokenizers

▶ Watch this lesson Start the whole course

What the animation says

Five bugs, one cause

A language model can write a sonnet, then tell you strawberry has two r's. It also garbles reversed words, slips on long addition, and stumbles when a prompt ends in a space. And the same sentence in Hindi costs more than in English. These look like five separate bugs. They share one cause. So when you type strawberry, what does the model actually receive?

The model reads ids

Here is what arrives. In one real tokenizer, strawberry becomes three chunks, and each chunk becomes one number. The tokenizer keeps the table of which text each id stands for. The model never sees that table. It is like reading a book where every chunk is replaced by a codebook number. You learn meaning from context. You know a chunk's spelling only if you memorized it as a separate fact. So to count r's, the model must recall each chunk's spelling, then add. Tap every chunk that contains an r.

Reversing and digits

Reversing a word is harder still. The model must recall every spelling, then move letters across chunk borders. Watch the last letter of berry jump all the way to the front. Numbers have the same problem. Tokenizers chunk digit strings by how often they appear, not by place value. Here, four eight two one splits after three digits, while three nine oh seven splits after two. To add, the ones digits must line up. Tap the two chunks that hold the ones digits.

The trailing space

Next, whitespace. A space usually rides on the following word, so space-Paris is a single token. Space-Paris and plain Paris are different ids. Now end a prompt with a trailing space: the capital of France is, then a space. That space becomes a lone token of its own. So what must the model produce next?

Bytes per word

Answer: after a lone space, it needs a space-less Paris. That pairing is rare in text, so output gets shakier. Last failure: cost. Tokenizers start from UTF-8 bytes and merge frequent byte runs. This loop prints code points and bytes for one greeting in four scripts. English: five and five. Russian: six and twelve. Hindi: six and eighteen. Japanese: five and fifteen. An English-heavy tokenizer has few merges for Devanagari, so text stays near raw bytes. That means more tokens, higher cost, and less fits in context.

One cause

One cause sits behind all five. The model sees opaque ids, never characters. Tasks that need letters or digit positions force it to recall hidden spellings, so expect errors there. Exact whitespace can create rare splits. And scripts rare in the merge data split into more tokens, so they cost more and less of them fits. When a model fails oddly, ask: did this task need what the ids hide?

Why does a fluent model miscount letters?

An LLM can write a sonnet yet say "strawberry" has 2 r's, garble a reversed word, slip on multi-digit addition, act oddly on a prompt ending in a space, and charge more for the same sentence in Hindi. These look like five separate bugs. They share **one cause**, and once you see it you can predict where the next failure will be.

The model reads ids, not letters

The model never receives characters. It receives token ids. In one real tokenizer, "strawberry" arrives as `[496, 675, 15717]`. Each id is an opaque integer. The tokenizer knows which bytes an id stands for, but the model does not see that table. It is like reading a book where every word chunk is replaced by a codebook number. You can learn meaning from context. You know a chunk's spelling only if you happened to memorize it as a separate fact.

Five predictable failure types

The input is ids $x_1,\dots,x_T \in \{0,\dots,V-1\}$. Spelling is a fact *about* an id, never part of it. - **Counting letters:** the model must recall each token's spelling, then count. - **Reversing:** it must recall each spelling, then reorder characters across token boundaries. - **Digits:** BPE chunks digit strings by frequency, not by place value. - **Leading space:** `" cat"` and `"cat"` are different ids, so a prompt ending in a lone space creates a rare split. - **Rare scripts:** a tokenizer built mostly from English has few merges for scripts like Devanagari. The text stays close to raw bytes and needs more tokens.

Tracing the failures by hand

Suppose "strawberry" → `str|aw|berry`. To count r's, the model must recall 1 + 0 + 2 = 3 from three memorized facts about ids. Reversing needs the same recall, plus reordering. Digits: `4821` → `482|1` but `3907` → `39|07`. The ones digits sit in mismatched chunks, so carrying needs unpacking first. Space: normally the space rides on the next word, as in `" Paris"`. A prompt ending in `"is "` leaves a lone `" "` token. The model must then continue with a space-less `"Paris"`, a pairing that is rare in text.

Bytes per word: worst-case token count

BPE starts from UTF-8 bytes. If no merges cover a script, each byte becomes one token. Basic Latin (ASCII) letters take 1 byte each and Cyrillic letters 2. Devanagari and most Japanese characters take 3. Here `len(w)` counts code points. नमस्ते is 6 code points because its vowel signs and virama are separate. An English-heavy tokenizer has few merges for these scripts. So the same greeting costs several times more tokens, and less of it fits in the model's context.
words = ["hello", "привет", "नमस्ते", "こんにちは"]
for w in words:
    b = w.encode("utf-8")
    print(f"{w}: {len(w)} code points, {len(b)} bytes")
# hello: 5 code points, 5 bytes
# привет: 6 code points, 12 bytes
# नमस्ते: 6 code points, 18 bytes
# こんにちは: 5 code points, 15 bytes

Remember this

The model sees opaque token ids, never characters. Tasks that need letters or digit positions force it to recall hidden spellings. Exact whitespace can create rare token splits. Expect errors in both cases. Text in scripts that were rare in the tokenizer's merge data splits into more tokens, so it costs more and less of it fits in the context.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.