cd ..
Part 2 · How LLMs Run Fast → A visual course · Part 1 · no math required

How a large language model actually works

ChatGPT, Claude, Gemini — under the hood they're all the same surprisingly simple idea, stacked up billions of times. This is a scroll-through, click-around tour that builds the whole picture from scratch: tokens, embeddings, attention, the transformer, and the different shapes these models come in. One animation at a time.

Tokens Embeddings / vectors Attention / generation Weights / parameters Training
Scroll to begin · 12 short chapters
Already comfortable with the basics? Skip to Part 2 · How LLMs Run Fast →
01 / THE BIG IDEA

At heart, it just predicts the next word

Strip away all the hype and a language model does one humble thing: given some text, it guesses what comes next. Not one guess — a ranked list of possibilities, each with a probability. That's the entire engine. Everything else in this course is machinery that makes that one guess shockingly good.

Next-token prediction
The cat sat on the▁▁▁

HOW IT WRITES →  To produce a whole sentence, it picks one word, sticks it on the end, and asks the very same question again — "now what comes next?" — over and over. A reply is just this guess, looped hundreds of times.

MythLLMs look up answers in a big database, or memorise the web.
RealityThere's nothing to search. Each next-token guess is computed fresh from statistical patterns baked into the weights — it's prediction, not retrieval.
02 / THE UNIT

First, text is chopped into tokens

A model can't read letters or words the way you do — it works with numbers. So step one is a tokenizer that splits text into small chunks called tokens (a short word, or a piece of a longer one) and swaps each for an ID number. Common words become one token; rarer words get broken into parts.

Tokenizer

"The cat sat on the mat, contentedly."

NOTICE →  "contentedly" splits into pieces while "cat" stays whole. From here on, every length, speed and limit you hear about a model is counted in tokens, not words.

MythOne word always equals one token.
RealityCommon words are a single token, but rare or long words break into pieces — and spaces and punctuation count as tokens too.
03 / MEANING AS SPACE

Each token becomes a point in meaning-space

An ID number like 2543 carries no meaning. So the model turns each token into a long list of numbers — a vector, or embedding — that places it as a point in a vast space. The trick: words with similar meanings land close together, and directions in that space capture relationships.

Embedding space · hover a word(shown in 2D — really hundreds of dimensions)
What you're seeing

Hover any word to light up its nearest neighbours. Notice cat / kitten / dog huddle together, far from Paris / Tokyo. Meaning became geometry.

Directions mean something too
king − man + woman ≈ queen

The step from man→woman is the same move as king→queen. The model learns these directions on its own.

04 / INSIDE THE MACHINE

How a neural network turns numbers into numbers

Time to open the box. A neural network is just numbers flowing through a web of connections. Values enter on the left; along every connection they're multiplied by a weight; at each node they're summed, nudged by a bias, and gently squished into a tidy range; then handed to the next layer. Do that a few times and an answer pops out on the right. Here's a tiny one you can actually run.

Forward pass · drag the inputs, or run it
One node, up close
out = squish( x₁·w₁ + x₂·w₂ + x₃·w₃ + b )
x₁0.85
x₂0.30
x₃0.60

Change an input and every node downstream recomputes. Line thickness = weight size; rose = positive, grey = negative.

LEARNING →  Nobody sets these weights by hand. Training shows the network millions of examples and nudges every weight a hair whenever the output is wrong — until the whole web produces good answers. The two outputs here stand in for a real model's next-token scores (which has one output per possible token — tens of thousands of them).

MythThe model is programmed with hand-written rules and if-else logic.
RealityThere are no rules to read — only these tuned numbers. Everything it "knows" lives in the weights, learned from data.
…and how many weights? Pick a scale
3
weights in this toy net

Same idea at every scale — just more knobs. The knobs are the model; they're what gets shipped as "the weights."

05 / ORDER MATTERS

Telling the model where each token sits

In a moment, attention will let every word look at all the others at once — which means, on its own, it has no sense of order. But "dog bites man" and "man bites dog" are very different! So first, each token's embedding gets a position tag added in, telling the model where in the line it sits.

Same words, different order
"dog bites man"
🐕 → 🦷 → 🧍 ouch for the man
"man bites dog"
🧍 → 🦷 → 🐕 a very strange headline

THE FIX →  Each token embedding = what the word is + where it sits. With positions added, attention can tell subject from object, first from last, near from far.

06 / THE BIG IDEA INSIDE

Attention: every word looks at the others

Here's the breakthrough that made modern models work. To understand a word, the model lets it look at every other word in the sentence and pull in what's relevant. The word "it" figures out what it refers to by paying attention to the right earlier word.

Self-attention · click a word to make it the "query"

Click it to see what it's really pointing at.

Q Query

The current word asks a question: "what here matters to me?"

K Keys

Every other word offers a label — "here's what I'm about."

V Values

Words that match get their meaning pulled in, weighted by the match.

MANY AT ONCE →  A real model runs dozens of these attention "lenses" (called heads) in parallel — one might track grammar, another who-did-what, another tone — then blends their findings.

MythSomeone told the model that "it" refers to "cat."
RealityNobody labels this. It learned to route attention from data — and Q, K and V are three roles the same token plays, not three different words.
07 / THE BUILDING BLOCK

Stack attention into a transformer

Attention is one ingredient. Wrap it with a small "thinking" step (a feed-forward network), add two safety rails that keep the numbers stable (residual shortcuts and layer-norm), and you have one transformer block. Then stack that same block dozens of times. Each layer refines the meaning a little more.

One block, stacked N times
↑ out to next block
Add & Norm
keep numbers steady
Feed-forward
think about each token
Add & Norm
+ residual shortcut
Self-attention
tokens share context
↑ in from previous block
12 layers deep

Early layers catch grammar and simple patterns; deeper layers capture abstract meaning, logic and style. Big models stack many dozens.

08 / WRITING THE REPLY

Sampling, and the temperature dial

Back to that ranked list of next tokens. How does the model choose? A dial called temperature reshapes the odds. Low temperature makes it play safe and pick the top choice almost every time; high temperature flattens the odds so it takes more risks — more creative, but more likely to wander.

Temperature · drag the slider
0.7
balanced
The cat sat on the mat

TWO PHASES →  Reading your prompt (all tokens at once) is fast; writing the reply (one token, then the next, then the next) is the slow part. That's why long answers stream out gradually.

MythThe same prompt always gives the same answer.
RealityOnly at temperature 0. Once there's any randomness in the sampling, re-running the same prompt can land on different words — that's the variety, not a bug.
09 / HOW IT LEARNS

From raw text to a helpful assistant

A model isn't programmed with facts — it's grown in stages. First it reads a huge slice of the internet to learn language itself. Then it's taught to follow instructions. Finally, it's polished using human preferences so its answers are helpful and safe.

The training pipeline
Stage 1

Pre-training

Predict the next token across trillions of words of text. This is where raw knowledge of language, facts and patterns is soaked up. Slow, enormous, expensive.

the whole internet · months of compute
→
Stage 2

Instruction tuning

Show it thousands of example question → good answer pairs so it learns to respond to requests, not just autocomplete text.

curated examples · learns to be useful
→
Stage 3

Preference tuning

Humans (and other models) rank answers; the model is nudged toward the ones people prefer. This is the RLHF step that shapes tone, honesty and safety.

human feedback · alignment

TRAIN vs RUN →  All of that is training — done once, ahead of time, to set the weights. When you chat, the weights are frozen: that's inference, just running text through the finished machine.

MythIt learns from your chats in real time.
RealityThe weights are frozen while you use it — your conversation isn't retraining the model. New knowledge only arrives when it's trained again, later.
10 / THREE SHAPES

Not all transformers are built the same

The same building block gets wired up in three classic ways, depending on the job. The difference comes down to who's allowed to look at whom — the attention pattern. Pick one below to watch data flow through it, and see the attention grid where a lit cell means "this token can look at that one."

Pick an architecture · watch the data flow
who can look at whom · lit = allowed
11 / BIGGER & LONGER

What "more powerful" usually means

Progress has largely come from turning up three dials together. More parameters, more training data, and a longer context window — how much text the model can hold in mind at once when it answers you.

The dials that grew
Parameters
0
Training data
0
Context window
0

CONTEXT WINDOW →  This is why a model can "forget" the start of a very long chat: everything it's considering — your prompt, the documents, its own reply so far — has to fit inside that token budget.

12 / THE WHOLE PICTURE

Every word, in plain language

You've now seen each piece move. Here's the quick glossary to keep — the jargon, decoded.

Token
A chunk of text — a short word or word-piece. The unit everything is counted in.
Embedding
A token turned into a list of numbers (a vector) that places its meaning as a point in space.
Weights / parameters
The billions of tunable knobs learned during training. Together they are the model.
Attention
The mechanism that lets each token look at other tokens and pull in what's relevant.
Transformer block
Attention + a feed-forward step + stabilisers, stacked many times to build understanding.
Positional encoding
Extra info added to each token so the model knows its order in the sequence.
Autoregressive
Generating text one token at a time, feeding each new one back in to get the next.
Temperature
The dial that trades safe & predictable against creative & risky when picking tokens.
Pre-training → fine-tuning → RLHF
Learn language → learn to follow instructions → learn human preferences.
Encoder / decoder
Encoder = read & understand (looks both ways). Decoder = generate (looks only backward).
Context window
The maximum number of tokens the model can consider at once when answering.
Part 2 · continue the course
How LLMs Run Fast →
You know how a model works — now see how it's made cheap and fast to run: quantization, the KV cache, speculative decoding and more. How a 140 GB model is squeezed onto one GPU and answers in milliseconds.