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
Step 1: Tokens
The model cannot read letters, so your text is cut into pieces and each piece becomes a number.
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.
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.
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.
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.
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.
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."
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
- Learn to code on your phone: Enki, an App Store app from another company. We have no tie to it.
- Build a real website: QuickSites, our sister product.
- Work with us: Point Seven Studio internships. Today's roles are in writing, audiobook production and community, not engineering.
- What goes wrong: the 7 kinds of AI hallucination, and how to catch each one.
- The short version: the six steps on the Learn shelf.
Sources: the seven papers behind this post
- Vaswani and colleagues, Attention Is All You Need (2017). Attention and the transformer block.
- Sennrich, Haddow and Birch, Neural Machine Translation of Rare Words with Subword Units (2016). Tokens made from word pieces.
- Mikolov, Chen, Corrado and Dean, Efficient Estimation of Word Representations in Vector Space (2013). Meaning in word vectors.
- He, Zhang, Ren and Sun, Deep Residual Learning for Image Recognition (2015). The shortcut connection.
- Ba, Kiros and Hinton, Layer Normalization (2016). The rescaling step.
- Brown and colleagues, Language Models are Few-Shot Learners (2020). The GPT-3 paper, including its context window.
- Holtzman and colleagues, The Curious Case of Neural Text Degeneration (2019). Why models sample.