"What tokens are, why AI uses them instead of words, and a tiny Python tokenizer you can run to see how it works."

tags: ai, python, beginners, machinelearning

Before your message reaches ChatGPT, it's chopped into pieces called tokens and turned into numbers. The model never sees your words, only those numbers.

That one fact explains three things people find confusing about AI:

  • why chat tools have length limits,
  • why APIs charge "per million tokens",
  • why a model can struggle to count the r's in "strawberry".

What's a token?

A token is a chunk of text: often a whole word, sometimes part of a word or a punctuation mark. In English, one token averages about 4 characters, roughly ¾ of a word.

Tokens Roughly
100 75 words, a short paragraph
1,000 750 words, about 1.5 pages
10,000 7,500 words, a long report

Why not just use words? There are too many of them, including names, typos and slang. Why not letters? Sentences would get very long. Tokens sit in between: common words get their own token, and rare words are built from pieces.

A tokenizer in 15 lines

Real tokenizers learn their vocabulary from huge amounts of text, but the core idea fits in a few lines: always grab the longest piece you recognise.

vocab = {"un", "believ", "able", "token", "s", "play", "ed", "ing"} def tokenize(word): tokens = [] i = 0 while i < len(word): for j in range(len(word), i, -1): piece = word[i:j] if piece in vocab: tokens.append(piece) i = j break else: tokens.append(word[i]) i += 1 return tokens for w in ["unbelievable", "tokens", "played", "playing", "xyz"]: print(w, "->", tokenize(w)) 
unbelievable -> ['un', 'believ', 'able'] tokens -> ['token', 's'] played -> ['play', 'ed'] playing -> ['play', 'ing'] xyz -> ['x', 'y', 'z'] 

played and playing share the play token. And xyz, which isn't in the vocabulary, still works because it falls back to single characters. A tokenizer never meets a word it can't handle.

Why this matters when you use AI

  • Limits are counted in tokens. A model's "context window" is how many tokens it can hold at once: your messages, its replies and any files you attach.
  • Prices are counted in tokens. APIs charge per million tokens, with input and output priced separately. Output usually costs more.
  • Some languages cost more. If a language appeared less often in the tokenizer's training text, its words get split into more pieces. The same sentence in Hindi or Tamil often takes more tokens than in English.

You'll see that last point for yourself in the exercise below.

So why "strawberry"?

The model receives "strawberry" as one or a few tokens, not ten letters. Counting the r's means reasoning about letters it never directly sees.

Try it

Run the tokenizer on "playfully". How many tokens do you get? Then add "ful" and "ly" to vocab and run it again.

.

.

.

Answer:

before: ['play', 'f', 'u', 'l', 'l', 'y'] 6 tokens after: ['play', 'ful', 'ly'] 3 tokens 

Same word, half the tokens, just because the vocabulary knows more pieces. That's why text in a well-covered language is cheaper and fits more into the context window.

To count tokens with a real model's tokenizer, paste any text into OpenAI's tokenizer.

Tokens are step one. For what happens to them next inside the model, see How large language models work, and if you want to build with them, the free Building AI Apps with LLMs course. Both are on TutorialsHub, which I run.

What surprised you most about tokens? Tell me in the comments.