Skip to content
mentoor

Topic 5 of 6 · 5 min read

Tokenizer quirks behind model bugs

Goal: Trace an odd model behaviour, such as a glitch token or broken arithmetic, back to how the input was tokenized.

Many model failures that look like reasoning or knowledge problems are tokenization effects: the model never sees letters, only ids, and the way text is cut changes what it can do. When a model behaves oddly on a specific input, print the tokens before blaming the weights.

Check yourself · 1 of 3

Why can a completion prompt that ends with a trailing space produce odd output?

Choose one answer

Why it matters

You can spend a day rewriting prompts for a bug that is a stray space. Tokenization bugs are deterministic and cheap to check, so they are the first thing to rule out.

The usual suspects

  • Leading spaces. " hello" and "hello" are different tokens with different embeddings. A prompt that ends with a trailing space pushes the model to continue with a word that has no leading space, which is rare in training. Completion prompts ending in a space often produce strange output.
  • Digit splitting. Numbers are cut into pieces that follow the tokenizer's rules, not place value. "1234567" may become "123", "456", "7", and the same number with different punctuation splits differently. Arithmetic and digit counting suffer.
  • Spelling and counting letters. A word like "strawberry" can be two or three tokens, so counting its letters means reasoning about hidden characters. That is why letter-counting questions trip models.
  • Glitch tokens. A vocabulary can contain tokens made from strings that appeared in the tokenizer's data but almost never in the model's training data. Their embeddings are barely trained, and prompts containing them can give erratic output. The paper "Fishing for Magikarp" studies how to detect them.
  • Non-English text. A tokenizer trained mostly on English cuts other languages into more tokens per word. The same meaning costs more context and money, and the model has seen less well-formed text in those pieces.
  • Case and whitespace variants. "Hello", "hello" and "HELLO" are different token sequences, as are tabs versus spaces.

How to debug

  1. Encode the exact string and print tokens and ids, including special tokens.
  2. Look for unexpected splits, a leading or trailing space, or very rare tokens.
  3. Change one thing (remove the trailing space, add spaces between digits) and compare outputs.
ids = tokenizer.encode(prompt)
print([tokenizer.decode([i]) for i in ids])

Before and after

Weak: "The model can't count the r's in strawberry, so it is bad at logic." Fixed: "The word is split into a few multi-letter tokens, so the model has no direct view of its letters. Ask it to spell the word out with separators first, then count."

What goes wrong

  • Trailing whitespace in templates. A newline or space at the end of a few-shot prompt changes the next-token distribution.
  • Comparing non-English costs by characters. Bills and context limits are in tokens, so estimate with the real tokenizer.
  • Blaming the model for a copy-pasted string. Invisible characters and unusual Unicode look the same on screen but tokenize differently.

Examples

The trailing space

A few-shot prompt ends with Answer: (with a space). The model writes lowercase fragments or repeats itself. Printing the tokens shows the last token is a bare space, which rarely occurs before a word in training text. Removing the space so the prompt ends at Answer: lets the model produce " Paris" as one token with its own leading space.

Same sentence, different cost

One sentence in English takes 9 tokens. The same meaning in Thai takes 31 with the tokenizer you are using. Counting characters says the texts are about the same length. Counting tokens says the second costs more than three times as much and uses that much of the context window. Always measure with the real tokenizer.

Key points

  • The model sees token ids, not letters, so letter counting and spelling depend on how a word is split.
  • Whitespace belongs to tokens: a trailing space or missing leading space changes the next-token distribution.
  • Numbers are split by tokenizer rules rather than place value, which hurts arithmetic and digit tasks.
  • Glitch tokens are vocabulary entries the model barely trained on and can trigger erratic output.
  • Non-English text usually costs more tokens per word, which means more cost and less usable context.
  • First debugging step for an odd output: print the tokens of the exact input.

Common mistakes

  • Rewriting prompts for hours before printing the tokens of the failing input.
  • Ending a completion prompt with a space or an extra newline.
  • Estimating cost or context use for non-English text from English character counts.
  • Treating a letter-counting failure as a general reasoning failure.

Check yourself · 2 of 3

A model gives wrong digit counts for long numbers. What should you check first?

Choose one answer

Check yourself · 3 of 3

Why does the same sentence cost more tokens in many non-English languages?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic