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?
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
- Encode the exact string and print tokens and ids, including special tokens.
- Look for unexpected splits, a leading or trailing space, or very rare tokens.
- 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?
Check yourself · 3 of 3
Why does the same sentence cost more tokens in many non-English languages?
Practice items, not counted towards your mastery.
Start here
- Byte-Pair Encoding tokenization (Hugging Face LLM course) (opens in a new tab)CourseFree
Walks through BPE training and encoding step by step on a tiny corpus, including the byte-level trick that removes unknown tokens.
- Andrej Karpathy: minbpe and the GPT tokenizer lecture (opens in a new tab)ToolFree
Short, readable Python for training, encoding and decoding BPE; the best code to step through next to the merge algorithm.
- Hugging Face LLM Course: The Tokenizers library (opens in a new tab)CourseFree
Hands-on chapters on training a tokenizer from an old one and on the BPE, WordPiece and Unigram algorithms.
More for this topic
- Fishing for Magikarp: Automatically Detecting Under-trained Tokens in Large Language Models (opens in a new tab)PaperFree
Explains why some vocabulary entries are almost never trained and how to detect them.
- Tokenizer (OpenAI) (opens in a new tab)ToolFree
Paste any text and see its token boundaries and ids, useful for checking spaces, digits and non-English text.