How LLMs work
Next-token prediction, a learned function with billions of parameters, and why the output is a probability distribution rather than a lookup.
How LLMs work
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
What you'll learn
- A language model is one enormous learned function that maps a sequence of tokens to a probability distribution over the next token.
- Generation is that function run repeatedly, each output appended to the input, which is why a long response costs far more than a long prompt.
- Training sets the parameters so that real text is assigned high probability; nothing in the model stores text to be looked up later.
The one idea worth holding on to
A language model predicts the next token. Everything else — chat, reasoning, tool use, agents — is that single operation wrapped in enough scaffolding to look like something else. Keeping this in view explains behaviour that is otherwise baffling: why a model invents a citation with total confidence, why asking it to think step by step changes the answer, why the same prompt gives a different result twice.
From text to a distribution
A model’s input is a sequence of tokens — covered properly in the next lesson, but treat a token here as roughly a word fragment. Each token is converted to a vector by a lookup table called an embedding layer, so the sequence becomes a stack of vectors. Those vectors pass through dozens of repeated blocks, each combining two operations: self-attention, which lets every position in the sequence pull information from every earlier position, weighted by how relevant that earlier position is to this one, and a feed-forward network, which processes each position’s vector independently and is where most of a model’s raw parameter count actually lives. After the last block, a final linear layer projects the last position’s vector onto one score per word in the vocabulary — tens of thousands of scores — and a softmax turns those scores into a probability distribution that sums to one. That distribution is the model’s entire output for this step. Generating one token means sampling from it; generating the whole response means repeating the entire process, once per output token, with the newly produced token appended to the input each time.
Where the parameters came from
Those billions of parameters are not programmed; they are fit by gradient descent against a simple objective during pretraining: given everything so far, predict the next token correctly, scored against trillions of tokens of real text. Nothing in that objective asks the model to be honest, helpful or careful — only to predict plausible continuations. The behaviour that looks like following instructions or refusing harmful requests is added afterwards, in a second stage usually called instruction tuning or alignment, where the model is further trained on examples of the behaviour a product wants, and often refined again using human or automated feedback on which of several responses is better. The pretrained model is a raw continuation engine; the model you call through an API has had two or three additional rounds of training stacked on top to make it behave like an assistant.
Why generation is the expensive half
Processing the prompt happens in a single pass: every position attends to every earlier position at once, which is embarrassingly parallel on a GPU. Producing the response cannot be parallelised that way, because token five needs to exist before token six can be generated — each output token is its own full pass through every layer of the network, conditioned on everything produced so far. This asymmetry is the direct cause of the pricing split covered in cost and latency: reading is cheap because it is parallel, writing is expensive because it is sequential.
What “no memory” actually means
A deployed model holds no state between API calls. Every turn of what feels like a continuous conversation is, mechanically, a brand new forward pass over the entire transcript re-sent from scratch. The model is not “remembering” the earlier turns; it is reading them again, exactly as if for the first time, packed into the same context window as the current question. This is why a conversation that has grown very long costs more per turn even when the new message is short, and it is the reason the context window has to be managed as a budget rather than treated as free memory — the subject of a later lesson in this group.
Check yourself
Why is a model's output a distribution rather than a single answer?
The final layer scores every token in the vocabulary and a softmax turns those scores into probabilities. Picking one is a separate decision made by the sampler, not by the model.
Why does generating 1,000 tokens cost more than reading 1,000 tokens?
The input is processed in one parallel pass, but each output token requires its own forward pass through the whole network, with every previous token in context.
Go deeper
The maths underneath
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.