AI Learning Courses
AI Learning Courses › Tutorials › Tutorial 14
AI TUTORIAL 14 · 9 MIN READ

How AI fixes and rearranges a sentence: tokens, ID numbers and attention, step by step

Type a broken sentence and AI fixes it in a second. Follow one real sentence inside: how it is cut into tokens, how every token gets an ID number (and where those numbers come from), how attention finds the mistakes, and how the corrected sentence is written one token at a time.

Turn on JavaScript to watch the animated lesson. The full tutorial text is below.

Our example sentence Watch

Someone types: "yesterday me and my friend goes to market for buy vegetable." and asks the AI to fix it. We spot the mistakes at once. The AI does not see words like we do - it sees numbers. Let us follow the journey.

Correct this sentence and list every change you made:
yesterday me and my friend goes to market for buy vegetable.

Step 1: The sentence is cut into tokens Watch

First a program called the tokenizer cuts the text into small pieces called tokens. A token can be a whole word, part of a word, a space plus a word, or a full stop. Our sentence becomes 13 tokens.

Step 2: Every token gets an ID number Watch

Next, the tokenizer looks up each token in its vocabulary: a fixed list of every token the model knows, each with its own number, called the token ID. These are the real IDs from the tokenizer OpenAI uses for GPT-4o, which knows about 200,000 tokens:

TokenID numberWhat it shows
"y" + "esterday"88 and 15198small y at the start: split into two tokens
"Yesterday"70877capital Y: one token, a different number
" friend" (with a space)5168the space is part of the token
"friend" (no space)18924without the space it is another token
" to"316the same token always gets the same number
" vegetable" and " vegetables"50593 and 30400singular and plural are separate tokens

So the AI never receives your words. It receives a row of numbers: 88, 15198, 668, 326, 922, 5168 ... and from here on it works only with numbers.

Where do the ID numbers come from? Watch

The numbers are not random, and nobody types them by hand. They are made once, before the AI model is trained, when the tokenizer itself is built. The usual method is called Byte Pair Encoding (BPE), a 1994 data-compression trick that was adapted for AI language models in 2016.

  1. Collect a huge amount of text: books, websites and code, in many languages.
  2. Start with single characters: the first 256 numbers go to single characters (bytes). That is why "y" is 88 and the full stop is 13.
  3. Count the pairs: find the two pieces that sit next to each other most often in all that text, such as "t" and "h".
  4. Merge and number: join that pair into one new token and give it the next free number.
  5. Repeat: do this again and again, until the list is full: about 50,000 tokens for GPT-2, 100,000 for GPT-4 and 200,000 for GPT-4o.

After that the vocabulary is frozen. The same text always gives the same numbers, which is why " to" is 316 every single time.

What the ID number means, and what it does not Watch

Step 3: ID numbers become meaning Watch

An ID number alone carries no meaning, so the model uses it as a row number in a giant table called the embedding table: one row for every token, and each row is a long list of numbers (GPT-3 used 12,288 numbers per token). ID 5168 simply means "take row 5168".

Those rows were learned during training, so tokens with similar meanings ended up with similar rows: " went" sits close to " goes" and " go", and " vegetables" close to " vegetable" and " fruits". That is how the AI knows that goes and went are the same action at different times.

Step 4: Attention finds the mistakes Watch

Now every token looks at every other token and asks: which of you matter to me? This is called attention, and it runs through dozens of layers.

Nobody typed these grammar rules into the model. It learned the patterns by reading billions of correct sentences during training.

Step 5: The corrected sentence is written one token at a time Watch

The AI now writes its answer. At every step it gives each of its 200,000 possible tokens a score and turns the scores into probabilities. After "Yesterday, my friend and I", a likely result is: " went" 91 percent, " go" 4 percent, " goes" 2 percent (example numbers). " went" wins, is added to the answer, and the whole process repeats for the next token.

Token by token the new sentence appears: "Yesterday, my friend and I went to the market to buy vegetables." - 14 tokens, turned back into words by the same vocabulary list. Many apps then show the old and new sentence side by side, with every change highlighted.

Does the AI remember my sentence? Watch

While it works, your sentence sits in its short-term memory, the context window. When the answer is done, those temporary numbers are thrown away, and the model does not learn from your sentence on the spot. The app may save your chat history, depending on its settings - that is separate.

Lesson learned Watch

AI never reads your words: it cuts them into tokens, swaps each for an ID number from a fixed list built by merging common pairs, looks up its meaning, finds the mistakes with attention, and writes the fix one token at a time.

Frequently asked questions

What is a token ID?

The number a tokenizer gives each token: its position in the model's fixed vocabulary list. It is a label, not a value, and the model uses it to look up the token's meaning vector.

Who decides the ID numbers?

Nobody picks them by hand. They are created once when the tokenizer is built, usually by Byte Pair Encoding: single characters first, then the most common pairs merged and numbered in order.

Why does the same word have different IDs in ChatGPT and Gemini?

Each model family builds its own vocabulary from its own text, so the lists and the numbers differ.

Why does Hindi use more tokens?

Older vocabularies were built mostly from English text, so Hindi words were cut into many small pieces. Newer vocabularies, like GPT-4o's, include many more Hindi pieces.