ai/courses

AI Fundamentals/Lesson 3 of 8

What Is a Token in AI? How LLMs Read Text

How text becomes numbers, and why the model can’t count the r’s in “strawberry”.

Lesson
3 of 8
Time
20 min
Updated

TL;DR

The short answer

A token is the unit of text a language model actually reads and writes: often a whole common word with its leading space, sometimes a piece of a longer word, a punctuation mark or a single character. Before a model sees your text, a tokenizer cuts it into tokens from a fixed vocabulary and replaces each one with a number.

In English, a token averages about four characters, or three quarters of a word: 1,000 tokens is roughly 750 words. Everything practical about language models is counted in tokens: the price of a request, the size of the context window, and the speed of the answer.

A network needs numbers, not letters

From lesson 2 you know that a neural network is a big calculator: numbers go in, numbers come out. So the first problem of any language model is turning text into numbers. There are three obvious ways to do it, and the first two don't work well.

One number per letter. Simple, and the vocabulary is tiny. But a page of text becomes thousands of steps, the model has to learn that "c", "a", "t" next to each other mean a small furry animal, and every computation gets slower because sequences are long.

One number per word. Now "cat" is a single number. But English has hundreds of thousands of words, plus names, typos, slang, code, URLs, and every other language. Any word not in the dictionary becomes "unknown," and the model is blind to it.

Something in between. Common words get their own number. Rare words are built from pieces: "tokenization" becomes "Token" + "ization." Anything at all, even a random string or an emoji, can still be spelled out from small pieces or raw bytes. These pieces are tokens, and every major language model uses them.

Fig. 1The sentence “Tokenization isn't magic.” becomes five tokens, and each token becomes a number from the vocabulary. These are the real token IDs in the tokenizer used by GPT-4o.

See what the model sees

Below is the real tokenizer used by OpenAI's GPT-4o family of models, called o200k_base, running in your browser. Type anything, or try the presets. Other models (Claude, Gemini, Llama) use their own tokenizers, so their splits differ, but the patterns are the same.

DemoEvery chip is one token (a ×2 badge means one character took two tokens, as emoji often do). Toggle the IDs to see the numbers the model actually receives. Notice where the spaces go, and what happens to numbers, rare words and other languages.

A few things to notice, because each one explains real model behavior:

  • The space belongs to the next word. " magic" with its leading space is one token. That's why "hello", " hello" and " Hello" are three different tokens with three different IDs (24912, 40617 and 32949). To the model, they're as different as three unrelated words until training teaches it that they're related.
  • Capitals change everything. " Hello" is one token, " HELLO" is two (" HEL" + "LO"). Shouting costs more.
  • Long rare words get chopped. " antidisestablishmentarianism" is six tokens: " ant", "idis", "est", "ablishment", "arian", "ism". The pieces don't always follow the word's real structure.
  • Numbers are cut into chunks of up to three digits. "1234567" becomes "123", "456", "7". The model never sees a number as a quantity, only as a few chunks, which is one reason plain language models are clumsy at arithmetic.

Where the vocabulary comes from

Nobody writes the list of tokens by hand. It's learned from a big sample of text with an algorithm called byte-pair encoding (BPE), and the idea fits in one paragraph.

Start with the smallest possible pieces: the 256 possible bytes, which can spell any text in any language. Scan the sample text and find the pair of pieces that appears next to each other most often, maybe "t" followed by "h." Merge that pair into a new piece, "th," and add it to the vocabulary. Repeat: maybe "th" + "e" becomes "the," and later " " + "the" becomes " the." Keep merging until the vocabulary reaches the size you want.

The result is a vocabulary shaped by what's common in the sample. Frequent words become single tokens; rare words stay split into pieces. The size of the vocabulary is a design choice, and it has grown over the years:

TokenizerUsed byVocabulary size
r50k_baseGPT-2 and GPT-3about 50,000 tokens
cl100k_baseGPT-3.5 and GPT-4about 100,000 tokens
Llama 3 tokenizerMeta’s Llama 3 modelsabout 128,000 tokens
o200k_baseGPT-4o and later OpenAI modelsabout 200,000 tokens
TableBigger vocabularies cover more languages and code with fewer tokens, at the cost of a larger embedding table inside the model.

A bigger vocabulary mostly helps languages other than English. Early tokenizers were trained on text that was mostly English, so other languages got chopped into many more pieces, which made them slower, more expensive and harder to learn. Here's the same sentence ("The weather is beautiful today, so we are going to the beach") in three generations of OpenAI tokenizers:

LanguageGPT-2/3 (r50k)GPT-4 (cl100k)GPT-4o (o200k)
English14 tokens14 tokens14 tokens
Spanish23 tokens15 tokens13 tokens
German23 tokens17 tokens14 tokens
Russian56 tokens27 tokens14 tokens
Hindi86 tokens57 tokens17 tokens
TableWe counted these with the published tokenizers. Same meaning, very different cost, until vocabularies grew.

Why tokens explain weird AI mistakes

Many famous chatbot failures stop being mysterious once you think in tokens.

Counting letters. Ask "How many r's are in strawberry?" and the word arrives as a single token, " strawberry," ID 101830. The model never sees s-t-r-a-w-b-e-r-r-y. It sees a number. To answer, it must have learned the spelling of that number indirectly, from text that happened to spell it out. That's why models long failed this kind of question while writing fluent essays. (Newer models usually get it right, often by spelling the word out letter by letter first, which turns each letter into its own token.)

Reversing, rhyming, counting words. Same problem. Any task about the letters inside a word is about information the tokenizer hid.

Arithmetic. "1234567 + 89" is a handful of chunks, and "456" in the middle is a token like any other. Models learn to do math on these chunks, but it's fragile, which is why serious AI apps hand arithmetic to a calculator tool.

Tokens are money and memory

Once you work with AI APIs, tokens are the unit of almost everything:

  • Price. Providers charge per million tokens, with separate prices for input (what you send) and output (what the model writes). Output tokens usually cost several times more, because each one requires a full pass through the model.
  • Context window. The limit on how much a model can see at once is measured in tokens: your instructions, the conversation, any documents and the answer all have to fit. Lesson 7 covers what happens when they don't.
  • Speed. Models generate one token at a time, so a 500-token answer takes about five times longer than a 100-token one. Speed is quoted in tokens per second.

The rule of thumb for English, from OpenAI's own guidance, is that one token is about four characters, so 100 tokens is about 75 words. A 300-page book is around 120,000 tokens. Code, numbers, unusual formatting and many other languages use more tokens per word.

count_tokens.py
import tiktoken

enc = tiktoken.get_encoding("o200k_base")   # the GPT-4o tokenizer
text = "How many r's are in strawberry?"
ids = enc.encode(text)
print(len(text), "characters ->", len(ids), "tokens")
print(ids)
print([enc.decode([i]) for i in ids])
CodeCount tokens in Python with OpenAI’s open-source tiktoken library. Output: 31 characters -> 8 tokens, and the pieces, with " strawberry" as one token.

Exercise · about 5 minutes

Catch the tokenizer in the act

Open any chatbot (ChatGPT, Claude, Gemini) and run three small experiments:

  1. Ask: "Spell the word 'lollipop' backwards, letter by letter." Then ask it to reverse the word without spelling it first. Does one approach work better? In the GPT-4o tokenizer, " lollipop" is just two tokens, " l" and "ollipop."
  2. Ask: "What is 48,213 × 7,029?" Check the answer with a calculator. Then ask it to show its work step by step and compare.
  3. Paste the same paragraph in English and in another language you know into the tokenizer demo above. Which uses more tokens?

Write down one sentence for each: what did the model see, and how did that explain the result?

Quiz

  1. 1.What is a token?

  2. 2.Roughly how many tokens is 750 words of ordinary English text?

  3. 3.Why did chatbots long struggle to count the r’s in “strawberry”?

  4. 4.How is a tokenizer’s vocabulary usually created?

  5. 5.Which of these is NOT measured in tokens?

0 of 5 answered

Key takeaways

  • Models read tokens, not letters or words. A tokenizer splits text into pieces and maps each to a number.

  • In English, 1 token ≈ 4 characters ≈ ¾ of a word. Code, numbers and many other languages use more.

  • The vocabulary is learned with byte-pair encoding: frequent pieces get merged into single tokens.

  • Tokens explain odd failures: letters inside a token are invisible to the model, and numbers arrive in chunks.

  • Price, context limits and speed are all counted in tokens.

Go deeper

AI Fundamentals: How Modern AI Actually Works

  1. 01How machines learn25 min
  2. 02Neural networks30 min
  3. 03Tokens20 min
  4. 04Embeddings25 min
  5. 05Attention and transformers30 min
  6. 06How LLMs are trained30 min
  7. 07How LLMs generate text25 min
  8. 08RAG, tools and agents30 min