← Blog
aihow-it-workscomputer-scienceexplainer

How an LLM Works, in Six Steps

A language model does one thing: it predicts the next piece of text. Here is the path from the words you type to the words it writes back, in six diagrams. Each step is a few lines, with the detail and the source paper one tap away.

By Sandon Jurowski
Share𝕏⬡inYf✉

A large language model (an LLM) does one thing. It looks at the text so far and predicts what comes next. Everything that feels like conversation, reasoning or writing is that one move, repeated.

Here is the path in six steps. Each step has one diagram and a short summary. Open "More" under any step for the detail and the research behind it.

The whole path

Your text
1. Tokens
2. Embeddings
3. Attention
4. Transformer blocks
6. One new token
Back to step 2
Step 5, the context window, is not a stage in the path. It is the limit on how much text the path can hold.

Step 1: Tokens

The model cannot read letters, so your text is cut into pieces and each piece becomes a number.

Step 1: text becomes numbers
Your textWrite one true thing.TokensWrite9566one832true2081thing1517.13Token numbers: what the model reads
Swipe sideways to see the whole diagram.
These numbers are made up for the picture. Every model has its own list of tokens and its own numbers.
More about tokens

A program called a tokenizer does the cutting. A token is often a whole word. A long or unusual word gets cut into several pieces, so the model can still handle a word it has never seen.

This is why AI products talk about tokens when they describe limits and prices. Tokens are what the model actually counts.

Source: building rare words out of common pieces comes from Sennrich, Haddow and Birch (2016).

Step 2: Embeddings

Each number is swapped for a long list of numbers that stands for the token's meaning and its place in the sentence.

Step 2: numbers become meaning
Token numbers95668322081151713Embedding table: one row of numbers for every tokenOne vector per token, marked with its position in the text
Swipe sideways to see the whole diagram.
More about embeddings

A token number is only a label. It says nothing about meaning. So the model keeps a big table with one row for every token it knows, and it swaps each number for its row. The row is called a vector, or an embedding.

Training shapes those rows so that tokens used in similar ways end up with similar vectors.

The model also marks each vector with its position in the text. Without that mark, "the dog bit the man" and "the man bit the dog" would look the same.

Source: Mikolov and colleagues (2013) showed how much meaning this kind of table can capture.

Step 3: Attention

Each token looks back at the tokens before it and takes in the ones that matter to it.

Step 3: each token looks back
QueryKeysValuesScoresWeights (sum to 1)New vectorvalues blended by weightA darker square = that earlier token matters more
Swipe sideways to see the whole diagram.
The squares are made up for the picture. Real ones come from the query and key vectors.
More about attention

Words depend on each other. In "the journal was full, so I bought a new one," the word "one" means nothing until you connect it to "journal."

For each token, the model scores every earlier token for relevance, turns the scores into weights that add up to one, and blends the earlier tokens together in those proportions. The token's vector now carries its context.

Two details matter later. A token can only look backward, never ahead. And the model runs many of these lookups side by side, so one can track grammar while another tracks who is being talked about.

Source: this is the central idea of Attention Is All You Need (Vaswani and colleagues, 2017), the paper that today's language models are built on.

Step 4: Transformer blocks

Attention and a small network form one block, and the model stacks dozens of blocks, each refining the last.

Step 4: one block, repeated many times
InMulti-headattention+Add & normalizeFeed-forwardnetwork+Add & normalizeshortcutshortcutOutOne block. A model stacks dozens of them.
Swipe sideways to see the whole diagram.
More about transformer blocks

Attention is one half of a block. The other half is a small network that reworks each token's vector on its own.

Two tricks keep a tall stack stable. A shortcut adds each half's input back onto its output. Then the numbers are rescaled so they stay in a workable range.

Most of what a model "knows" lives in the numbers inside these blocks. Training is the long process of setting them.

Sources: the shortcut is from He and colleagues (2015). The rescaling is from Ba, Kiros and Hinton (2016).

Step 5: The context window

The model can hold only so much text at once, and it sees nothing outside that limit.

Step 5: what the model can see
Context windowT1T2T3…Tn-1TnVisible to the modelTn+1Tn+2…Written by the modelEverything in both boxes must fit inside the window.
Swipe sideways to see the whole diagram.
Anything that does not fit is invisible to the model.
More about the context window

Your message, any earlier conversation, and the reply being written all have to fit inside the window together.

The limit has grown quickly. GPT-3 could hold 2,048 tokens in 2020. Current models hold far more. But the rule has not changed.

This is also why a model has no memory of its own. If an assistant seems to remember yesterday, that is because the app saved yesterday's text and placed it back in the window today.

Source: the GPT-3 figure is from its paper, Brown and colleagues (2020).

Step 6: One new token

The model gives every possible next token a probability, picks one, adds it to the text, and starts over.

Step 6: predict, pick, repeat
Text so farToday I wrote aModel(steps 2 to 4)A probability for every tokenpage0.31letter0.21line0.15poem0.11… thousands morePick oneletterAdd it to the text… aletterRun again for the next token
Swipe sideways to see the whole diagram.
The probabilities are made up for the picture.
More about picking the next token

After the last block, the model takes the vector for the final token and turns it into a probability for every token it knows.

It does not always pick the most likely one. Always taking the top choice produces flat, repetitive text, so models sample: likelier tokens win more often, but not every time.

The loop continues until the model picks a special "stop" token or reaches its length limit.

Source: Holtzman and colleagues (2019) showed why sampling beats always taking the top choice.

Try it yourself

Press "Take the likeliest word" until the sentence stops, then start over and do it again. You get the same sentence every time. Then try "Roll the dice."

Pick the next word
Today I
wrote50%
felt30%
walked20%
A hand-written toy with a few dozen words. A real model does the same thing over tens of thousands of tokens, with probabilities it learned.

What this explains

  • The same question can get different answers. Step 6 involves chance.
  • A confident answer can be wrong. No step checks a claim against the world.
  • It only knows what is in the window. Put your situation in the message.
  • One early slip can carry forward. Each new token is built on every token before it.
A screen-free version for the kitchen table

One person starts a sentence. The next person says the single word they think comes next, and so on around the table. Then play again with a rule: you may not say the most obvious word. The first round is step 6 with no chance in it. The second round is closer to how a real model writes.

Go deeper

Sources: the seven papers behind this post
Share𝕏⬡inYf✉