How AI Works

What is a token?

Before an AI reads your text, it cuts it into small pieces called tokens. Type below and watch it happen to your own words.

Token workbenchStep 1 of 6

What the AI reads
0tokens

Splits come from the tokenizer OpenAI published for GPT-4o, running in your browser. The text you type in the lesson is not sent anywhere.

Tokens, explained

A token is one of the pieces an AI system cuts text into before working with it. A token can be a whole word, part of a word, a punctuation mark or a single character, and each one stands for a number.

You saw this in the lesson above: an everyday word stayed in one piece, and a made-up word broke into several. The rest of this page explains why it works that way and where you will run into it.

What happens to your text

An AI system works with numbers, not letters. So everything you type goes through the same short journey before the AI does anything with it, and the answer makes the same journey in reverse.

From your sentence to the AI and back

You type a sentence. To you it is six words and a full stop.

The cat sat on the mat.

A tokenizer cuts it into pieces. Here that makes 7 tokens. The dot shows a space, which belongs to the word after it.

The·cat·sat·on·the·mat.

Each piece is swapped for its number. The numbers come from a fixed list. This row of numbers is what the AI receives.

The976·cat9059·sat10139·on402·the290·mat2450.13

The AI works out which token is likely to come next. Give it the start of the sentence and it scores every token on its list.

The·cat·sat·on·the·?
Likely next:·mat·floor·sofa

It adds one token, then does it all again. The chosen number (2450) is turned back into text. An answer is built this way, one token at a time, which is why replies appear on your screen bit by bit.

The·cat·sat·on·the·mat
Steps 1 to 3 show real splits and numbers from the tokenizer OpenAI published for GPT-4o. The guesses in step 4 are an illustration, not real output.

Why not just use words?

A list of whole words would never be finished. People invent words, misspell them, and write names, web addresses and code. An AI that only knew whole words would be stuck the first time it met one that was not on its list.

Single letters would solve that, but every text would become a very long row of pieces, which is slower and more expensive to work with.

Tokens sit in between. Common words get one token each, and anything unusual can still be spelled out from smaller pieces. The list used on this page has about 200,000 entries.

Where the list of tokens comes from

Nobody writes the list by hand. A program builds it by reading a very large amount of text and repeatedly joining the pieces that appear side by side most often. Here is a tiny version with three words.

Building a token list by joining common pairs

Start with single letters. At first every letter is its own piece.

low·lower·lowest

Join the pair seen most often. “l” followed by “o” appears three times, so “lo” becomes one piece.

low·lower·lowest

Do it again. Now “lo” followed by “w” is the most common pair, so “low” becomes one piece.

low·lower·lowest

Keep going until the list is full. Runs of letters that turn up everywhere end as one token. Rare ones are left in pieces, which is why a made-up word shatters.

low·lower·lowest
A simplified illustration of the method, called byte pair encoding. Real tokenizers do this over a huge amount of text, tens of thousands of times.

This is also why the same word can have more than one token. “cat” at the start of a line, “ cat” after a space and “Cat” with a capital were joined separately, so each has its own number.

Common questions

How many tokens is a word?

For ordinary English, OpenAI's rule of thumb is that one token is about four characters, or roughly three quarters of a word. So 100 tokens is about 75 words. It is an average: short common words are one token, long or unusual ones are several, and other languages often need more tokens to say the same thing.

Are tokens the same in every AI?

No. Different AI systems use different tokenizers, so the same sentence can be 7 tokens in one and 9 in another. This page uses the tokenizer OpenAI published for GPT-4o. Counts from it are a good guide to the idea, not an exact figure for every AI.

Why do tokens cost money?

Each token takes computing work to read or write. Companies that offer AI to software builders charge by the token, with separate prices for the text sent in and the text that comes back. If you use a chat app on a monthly plan you are not billed per token, though the plan's usage limits are worked out from them.

What is a token limit?

Every AI system has a maximum number of tokens it can take in at once, called its context window, and a maximum length for one answer. Both are counted in tokens. \2 to watch one fill up.

Why do AI systems struggle to count letters?

The AI receives a token as one number, not as a row of letters. Nothing in that number says how many times “r” appears in the word. An AI can often still work it out, but it is one reason such questions have tripped AI systems up.

Do tokens only apply to text?

No. AI systems that handle images or sound also turn them into pieces before working with them, and those count towards limits too. How that is done differs from system to system.

Last updated

Get the next lesson when it opens. Coming next is "What is an AI hallucination?". We'll email you when it's ready.
We use your email only to tell you when new lessons and the full course open. Privacy