What the tokenizer does to your eval
Token counts are not word counts. A benchmark that truncates silently is measuring your tokenizer, not your model.
Two models, same prompt, same answer, different score. The prompt was long enough to hit the context limit; one tokenizer used twenty percent more tokens for the same text. The last paragraph — the one with the instruction — was truncated before the model ever saw it.
Tokenization is the least interesting part of the stack until it changes the result.
What a token is
Byte-pair encoding starts with bytes or characters and merges frequently occurring pairs. The vocabulary is learned, so tokens are not words. antidisestablishmentarianism becomes several tokens. 1234567890 can split into pieces that depend on the vocabulary. JSON punctuation, indentation, and Cyrillic text often cost more tokens per character than English prose. Chat templates add special tokens that a bare encode call does not.
That matters for three things: the context budget, the bill, and the evaluation. A 128k-token window is not 128k words. Few-shot examples can spend a large fraction of the budget before the question arrives. If the harness truncates from the end, the answer may be cut off; if it truncates from the front, the instructions are.
Token counts are model-specific. The same string can be one token in one vocabulary and five in another. Comparing context usage or cost across models by character count is a category error.1
Where evals break
A short, unglamorous script catches more problems than most prompt changes:
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained(model_id)
ids = tok(prompt, add_special_tokens=False).input_ids
print(len(ids))
For a chat model, count what the model actually sees:
ids = tok.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
)
The usual failures:
- Silent truncation. Log the truncation rate and the token count per example. If the harness cuts at 8k tokens, say so; do not let the score imply the model saw the whole prompt.
- Exact match and F1. Token-level and character-level scoring disagree on punctuation, Unicode, and code whitespace. Pick one and keep it fixed across models.
- Answer length. A
max_tokenslimit that cuts the answer is a scoring artifact, not a model failure. Measure how often it happens. - Few-shot budget. A model with a more efficient tokenizer gets more room for examples. That is a real advantage, but it belongs in the comparison, not in an unstated prompt-length difference.
- Multilingual and code. A prompt that fits in 8k tokens for English may not fit for another language or for a code-heavy corpus. Measure tokens per character on your own data.2
- Tokenizer changes. A model update with a new tokenizer changes the prompt length and can move the score. Pin the tokenizer version with the model version.
What to do
Measure the token distribution of your eval set before you run it. Report the token budget alongside the score. Use the model’s own tokenizer, not an approximation, and use the chat template for chat models. When you compare models, compare on the same examples after each one’s tokenizer has done its work; if one model cannot fit the example, that is part of the result.
None of this is exotic. It is the difference between “model A is better” and “model A’s tokenizer left room for the instruction.” Those are different claims, and only one of them is about the model.
- The tokenizer is part of the model’s interface. A different tokenizer is a different input distribution, even when the text is identical. ↩
- For multilingual work, measure tokens per character by language before comparing models. The difference can change which examples fit in the context, which is a scoring decision. ↩