Skip to content

Tokenizers: why models struggle to count letters

Byte-pair encoding, how text becomes numbers, and the quirks it leaves behind.

NZNarjis Zekhnini27 Oct 20263 min readDrafted with Inky · reviewed by Narjis
The·cat·sat·on·the·mat.·It·was·warm·and·soft

Language models never see letters. Before anything else, text is split into tokens — chunks that are often whole words, sometimes parts of words, sometimes single characters — and each token is replaced by a number.

Byte-pair encoding in one paragraph

Most modern tokenizers use a variant of byte-pair encoding (BPE). Start with individual bytes. Count which pair appears most often in a big training corpus and merge it into a new token. Repeat tens of thousands of times. Frequent words end up as a single token; rare words are built from several pieces.

See it yourself

# pip install tiktoken
import tiktoken
 
enc = tiktoken.get_encoding("cl100k_base")
for text in ["hello world", "strawberry", "Tokenization", " tokenization"]:
    ids = enc.encode(text)
    print(f"{text!r:18} {len(ids)} tokens ->", [enc.decode([i]) for i in ids])

You will notice that a leading space changes the result: " tokenization" and "Tokenization" are split differently. Spaces are usually glued to the start of the following word.

The quirks this creates

  • Counting letters is hard. If "strawberry" arrives as two or three chunks, the model never directly sees each r. It has to have learned the spelling of those chunks.
  • Numbers are uneven. Digits may be grouped in irregular chunks, which is one reason arithmetic on long numbers is fragile.
  • Non-English text costs more. Languages under-represented in the training corpus need more tokens for the same meaning, so they use more of the context window and cost more per request.
  • Prices and limits are in tokens. API pricing and context windows count tokens, not words. In English, a token averages roughly three-quarters of a word.

Practical tips

  1. Measure prompts in tokens with the provider's tokenizer before estimating cost.
  2. For tasks that depend on characters (spelling, counting, exact formatting), ask the model to write code or spell the word out first.
  3. When you compare models, remember that the same text may be a different number of tokens for each one.

Takeaways

  • Tokens are learned chunks of text, not words or letters.
  • Many "dumb" model mistakes are tokenization artefacts.
  • Always budget and debug in tokens.

Keep reading

Get the next tutorial by email

One email a week. No spam.