The only thing an LLM does
You type on your phone, "I will reach home in", and the keyboard offers "10 minutes". It has not read your mind. It has seen millions of messages, and after "reach home in" a number of minutes usually comes.
Now imagine that keyboard grown enormous. It has read a large part of the internet, and it does not stop at one suggestion. It takes its own suggestion, adds it to the message, and suggests again. And again. That is a chatbot.
The question. When a chatbot writes a whole paragraph for you, what is it actually doing, step by step?
First, a word about words. The model does not read letters or whole words. It reads tokens: common words and pieces of rarer words, cut by the byte-pair method of Unit 16. GPT-2's list of tokens, its vocabulary, has 50 257 of them. In this unit a token is nearly always a whole word, to keep the pictures simple.
Here is everything a large language model (LLM) does. It is one loop of four moves.
- Read. Take every token written so far. Your question, the earlier chat and the part of the reply already written are all one long list of tokens.
- Score every token in the vocabulary. The transformer of Unit 18 reads the list and gives each of the 50 257 tokens a score, called a logit: "how well would you fit next?"
- Turn the scores into probabilities with softmax (Unit 14). They are positive and add up to 1.
- Pick one token, add it to the list, and go back to move 1. Stop when the model picks a special "end" token.
By hand. The text so far is "I drink". To keep it tiny, say the vocabulary has only five tokens: chai, coffee, water, cricket, the. The model gives them the scores . (We made these scores up by hand; a real model computes them.)
Divide each by the total:
Chai gets almost two thirds. Say we pick chai. The text is now "I drink chai", and the whole model runs again, from the start, on these three tokens, to choose the fourth. One run of the model gives one new token. A reply of 300 tokens is 300 runs.
The chain rule. How likely is the whole sentence? Multiply the guesses, each one given everything before it. This is the chain of guesses from Unit 16:
The n-gram model of Unit 16 kept only the last one or two words of the "given" part. An LLM keeps all of them: attention (Unit 18) looks back at every earlier token, and the causal mask (Unit 18) makes sure each guess sees only the past.
The game is simple, but playing it well is not. To guess what comes after "The capital of Japan is", the model must know geography. After "2 + 3 =" it needs arithmetic; after "def add(a, b):" it needs to know how Python code is written. Making the next-token guess good on trillions of tokens forces the weights to soak up grammar, facts and styles — because they all help the guess. And one step at a time is enough to write long, sensible text, because every step reads everything written before, including the words the model itself has just chosen.
Three next-word guessers, side by side. You have now met three machines that play the same game.
| n-gram (Unit 16) | recurrent net (Unit 17) | transformer LLM (Unit 18, this unit) | |
|---|---|---|---|
| what it reads | the last one or two words | one running note of everything so far | every earlier token, directly |
| the start of a long text | forgotten after a few words | fades as the note is rewritten | kept exactly, up to the context length |
| how it learns | counting | one word after another | every position at once (the mask) |
| work for one new token | one look-up in a table | one step of the cell | attention over all earlier tokens (§6 makes it cheaper) |
Rule of thumb. All three compute the same thing: a probability for the next token given the ones before. They differ only in how much of the past they can use. The transformer uses all of it, which is why it won.
The model does not plan the sentence and then type it out. There is no hidden finished answer waiting inside. Each token is chosen given only what is already written. When a reply starts well and then wanders, this is why: every new token becomes part of the "given" for all the tokens after it.
A language model is one function: "given the text so far, a probability for every possible next token". Run it, pick a token, append it, run it again. Multiply the guesses along the way and you get the probability of the whole text.
In the worked example you add 5 to every score: . What does the model do now?
You ask a question 40 tokens long. The model writes a reply of 60 new tokens (the last one is the "end" token). How many times did the model run?
If you want the algebra · 1 proof, step by step
Claim. For any tokens , (for the "given" part is empty: ).
The road ahead. The unit has four acts.
- A machine that guesses the next word (§1–§3): the loop, how a whole sentence is scored, and where the model's millions of numbers live.
- How it writes (§4–§7): picking the word — greedy, temperature, top-k and top-p — then the cache that makes writing fast and the clocks that make long texts possible.
- Training at scale (§8): how loss falls as models and data grow, and how to split a fixed budget.
- From guesser to assistant (§9–§13): copying good answers, fine-tuning with a thin low-rank patch, a reward learned from people's choices, the leash that keeps the model sensible, and a shortcut called DPO.
In one sentence: An LLM is your phone's autocomplete grown huge — read everything so far, give every token a probability, pick one, append it, repeat — and the chain rule multiplies those guesses into the probability of the whole text.
That was section 1. The rest of the unit opens when you unlock it.
13 more sections and the practice arena — 13 widgets, 32 checks and 16 solved problems in the whole unit (this preview had 1 widget and 2 checks).
- 2
Scoring a whole sentence: likelihood, loss and perplexity
- 3
Inside the stack, by the numbers
- 4
Picking the word: greedy and temperature
- 5
Cutting the tail: top-k and top-p
- 6
The KV cache: never recompute the past
- 7
Longer contexts: rotary positions at full size
- 8
Scaling laws: bigger, with more data, predictably better
- 9
Teaching it to follow instructions
- 10
LoRA: fine-tune a giant by changing a thin slice
- 11
A reward from human choices
- 12
Chasing the reward on a leash
- 13
DPO: skip the reward model
- 14
What to carry forward
- 15
Practice arena — sixteen problems, solved in full
Unlock this unit for ₹299, or all seven paid units for ₹999 — one-time payment, full refund within 7 days. See pricing.