How LLMs actually work · module 06 of 9 · ~55 min

What happens between your prompt and the next token

One prompt goes in, one probability distribution comes out, and everything in between is the same trip through the same machine. Tokens to vectors, attention routing context, MLP blocks holding facts, all writing to a shared residual stream, then unembedding back to the distribution module 1 sampled from.

By the end you can explain

  • Why the model only ever manipulates vectors, never text, after the first step
  • What attention actually does, and where the Q/K/V metaphor lies to you
  • Why most of the model's stored facts live in the MLP blocks, not attention
  • The residual stream as a running hypothesis every layer edits
  • Why the final step lands exactly on module 1's next-token distribution

You send a prompt, there's a short pause, then tokens stream out one at a time. Most of the model's quirks live in that pause and in the gaps between tokens: why it can't unsay a word once it's out, why a longer prompt costs more, why a pretty attention diagram isn't the model explaining itself. All of it comes from what the machine actually does in there, and it does the same thing every single time.

Here's the whole job, stated flat: given a sequence of tokens, output a probability distribution over the next token. One trip through the network produces one distribution. That's the entire forward pass. This module walks that trip at the altitude of Karpathy and 3Blue1Brown, concrete first, and by the end the last step will hand you back the exact distribution you learned to sample from in module 1.

The whole trip on one example

Take the prompt "The capital of France is". Step through what happens to it, one stage at a time, before we slow down on the interesting parts.

One forward pass, step by step

1 / 5 · Tokens

Prompt: "The capital of France is"

The·capital·of·France·is

The string is split into 5 tokens. The dot marks a leading space, part of the token. From here on the model never sees letters again, only the numbers each token maps to.

Five stages: the text becomes tokens, tokens become vectors, attention moves context between positions, the MLP does per-position work, and the final vector becomes a distribution over what comes next. Now the two stages worth dwelling on.

After step one, it's all vectors

The first thing that happens is tokenization, which module 2 covered: the string is chopped into tokens. The second thing is the one to internalize. Each token is looked up in an embedding table and replaced by a vector, a list of numbers. From that point on the model never touches text again. It manipulates vectors the whole way down and only converts back to a token at the very end.

That's not a technicality, it's the thing that makes the rest make sense. "Meaning" inside the model is direction in this vector space, the same idea I dug into in what cosine similarity actually measures. Tokens that behave similarly end up pointing in similar directions, which is why the model can treat "France" and "Paris" as related without any rule that says so. Everything the network does from here is moving and combining these vectors.

Attention: routing context between positions

At the input, each position's vector only knows its own token. "is" doesn't yet know it follows "France". Attention is the step that fixes that: for each position, the model looks back at earlier positions and pulls in information from the relevant ones.

Predict first

For the last position in 'The capital of France is', attention decides which earlier tokens to pull information from. Which one should it lean on most to predict what comes next?

The mechanism is a soft lookup, and this is where the standard metaphor comes in. Each position emits a query, a vector standing for "what am I looking for right now." Every position also emits a key, "what I contain," and a value, "what I'll hand over if I'm a match." The model takes the dot product of one position's query against every key to score relevance, softmaxes those scores into weights that sum to one, and the output is a blend of the values, weighted by those scores. So "is" asks a question, "France" turns out to be the strong match, and its value flows into the "is" position. Query, key, value: what am I looking for, what do I contain, what do I pass along.

That metaphor does real work, so it's worth saying plainly where it lies to you. It makes attention sound deliberate, like a little librarian at each position consciously searching for answers. There's no librarian and no intent. The query, key, and value are just three learned linear projections of the same vector, and "looking for" is nothing more grand than which vectors happen to point the same way under a dot product. A single attention head does one narrow, learned kind of routing, not intelligent search, and real models run many heads in parallel, each a different narrow slice. The metaphor buys you the intuition that information gets pulled from relevant earlier tokens. It oversells the moment you imagine anyone doing the looking.

And one more caveat, because you'll see it everywhere: an attention map, the pretty heatmap of which token attended to which, is not an explanation. It shows where information moved, not why the model produced its answer. Plenty of what determines the output rides in the residual stream that no attention picture shows, and averaging heatmaps across heads and layers actively misleads. The widget above says this in its own caption for a reason. Treat attention maps as a wiring diagram, never as the model's reasoning.

MLP blocks: where the facts live

Attention moves information between positions. It doesn't do much thinking about the information once it's there. That's the MLP block's job, and it runs on each position independently, after attention has routed context in.

Predict first

The fact 'the capital of France is Paris' is stored somewhere in the model's weights. Which part most holds that kind of stored fact?

So the division of labor is clean enough to hold in your head. Attention gathers the right context into a position. The MLP, holding the bulk of the parameters, is where features and facts get computed on that gathered context. In our example, attention brings "capital" and "France" together at the last position, and the MLP is what lights up the direction that means Paris.

The residual stream: a running hypothesis

Here's the piece that ties it together. Attention and the MLP don't replace the position's vector. They read from it and add their result back onto it. The vector flowing down through the layers is a shared workspace, the residual stream, and every block writes a small edit into it rather than overwriting it.

The useful way to picture it: the residual stream at the last position is the model's running hypothesis about what comes next, and each layer nudges that hypothesis. Early layers might establish "this is a geography question," a later attention head pulls in "France," a later MLP adds "and the capital of France is Paris-shaped." The final vector is the accumulation of every edit. This is also why the layers can be thought of as communicating: they all read and write the same channel.

Getting a token out, back to module 1

At the end, the final vector at the last position is multiplied by the unembedding matrix, which produces one score, a logit, for every token in the vocabulary. Then softmax turns those logits into probabilities that sum to one.

Stop there, because you've been here before. That distribution over the vocabulary, "Paris" at 71%, everything else splitting the rest, is exactly the distribution module 1 handed to the sampler. Temperature, top-k, and top-p act right here, on these logits, before a token is chosen. The entire forward pass exists to produce this one distribution, and everything module 1 taught you about sampling picks up precisely where this leaves off.

Predict first

You've watched the forward pass produce the final distribution. Now module 1's question in this module's vocabulary: you set temperature near zero. What are you doing to the logits at that last position?

Then the chosen token gets appended to the sequence and the whole trip runs again from the top, one full pass per token. That's why the model can't unsay a token: each pass only ever adds the next one. It's why longer prompts cost more: every position attends back over every earlier position. And it's why the thing streams. Each token you watch appear is one complete journey through the machine you just traced.

In production: knowing the trip pays off

Understanding this pass is what lets me reason about the tools instead of poking them. When verify-kit decides whether a paper supports a claim, I care that the decision rides on the actual retrieved source sitting in the context window, because attention can only route from what's present, and a fact that isn't in the tokens can't be attended to, only fabricated. When the NoeticMap pipeline extracts structured claims from tens of thousands of papers, knowing that cost scales with every position attending back over every other is why chunking and context budget are engineering decisions, not afterthoughts. And when I'm tempted to read an attention visualization as an explanation of why a model did something, this module is the reason I don't. The pass is the same every time. Knowing its shape turns a lot of the model's behavior from mysterious into predictable.

Can you retrieve it?

Answer out loud before revealing. If one is fuzzy, the section it came from is the one to reread.