How a language model writes
What happens after you press Enter
You type a sentence into an AI assistant and a reply appears, a word at a time. This walkthrough follows one sentence through a real language model in six steps. No maths needed.
The model has one job: choose what comes next. The orange box shows it choosing, again and again, in proportion to its own probabilities.
About the model on this page. Everything you can click here was computed by GPT-2, a small language model that OpenAI released publicly in 2019. It is the same kind of system as the assistants you use at work, at a size small enough to open up and look inside. The final page shows what changes at full scale.
Step 1 of 6
Your text is cut into pieces
The model cannot read letters or words. Before anything else, your text is cut into pieces called tokens, and each piece is replaced by its number, its token ID, in a fixed list called the vocabulary. This model's vocabulary has 50,257 entries. Turning text into numbers this way is called encoding, and the tool that does it is the tokenizer.
The entries are pieces of text, not a dictionary. Some are whole words, many are fragments, and the same word can appear more than once, with and without a space in front.
Common words get a piece of their own. Rarer words, names and long numbers are built from fragments. A leading space counts as part of the piece, shown here as a faint dot.
Type anything in the box, or try the examples, and watch how it is cut.
What this means at work
Length limits and usage charges are counted in tokens. Everyday English comes out near one piece per word. Names, codes, figures and other languages use more.
The model also never sees the letters inside a piece. In the middle of a sentence, where a space comes before it, “strawberry” reaches the model as the single number . Typed alone, with no space in front, the same word is cut into three pieces: st, raw and berry. Try both in the box.
Either way the model receives numbers, never the ten letters. That is one reason assistants can miscount letters or slip on long numbers.
Loading GPT-2's real splitting rules so you can type your own text.
Step 2 of 6
Each piece gets a position on a map of meaning
A token number says nothing by itself. So the model swaps each one for a list of 768 numbers that it learned during training. A list of numbers like this is called a vector, and the vector that stands for a token is its embedding. You can think of it as coordinates: pieces that are used in similar ways end up close together.
Nobody sorted these words into groups. The model arranged them itself while learning to predict text.
The map flattens 768 coordinates, or dimensions, down to two so that it fits on a screen. Select a word to see its closest neighbours among all 50,257 pieces, measured with the full 768.
What this means at work
You rarely need exact keywords. Words with related meanings sit near each other, so a request phrased in your own way is usually treated like the textbook phrasing.
The same property has a cost. Close neighbours are easy to swap. Tuesday and Wednesday sit almost on top of each other, and so do many names, dates and figures.
Real positions from GPT-2. Similarity runs from 0 to 1; identical pieces would score 1.00.
Step 3 of 6
Pieces gather context from each other
So far each piece knows only itself. The word “she” could be anyone. In this step, called attention, every piece looks back over the pieces before it and pulls in information from the ones that matter to it.
This model has 144 attention units, known as attention heads. Each one picked up its own habit during training; none was programmed by hand. Two of them are on the right. One, when it reaches a pronoun, looks toward the name that fits. The other only ever looks one piece back.
Swap the names or the pronoun and watch the first unit follow. It is not tidy, and some attention lands on other words. In all four versions, though, the matching name gets far more than the other name.
What this means at work
The model can only connect what is in its context: the text in front of it, which is your prompt, anything you attached, and the reply so far. If the detail that matters is in your head, or in a document you did not include, there is nothing for attention to find.
Naming things plainly helps for the same reason. “The March invoice” gives it something definite to look at. “It” leaves the model to work out what you meant.
Select any word to look from it instead. Darker means more attention. Dashed words come later in the sentence: a piece can only look backward.
Real attention weights from GPT-2 for this exact sentence.
Step 4 of 6
Twelve rounds sharpen the guess
Attention, followed by a processing step, makes one round, called a layer. This model runs 12 layers, one after another. The design as a whole is called a transformer, which is the T in GPT. Each round rewrites the 768 numbers for every piece, mixing in a little more context.
The next word is read from the last piece of the sentence, “to”. On the right you can stop after any round and see what the model would say if it had to answer immediately.
Early on it treats “to” as the start of an action: to get, to make. Around round 7 it turns to dates. It settles on a weekday only in the last few rounds.
What this means at work
Nothing is looked up during these rounds. There is no database of facts and no search. The answer is computed from your text and from numbers that were fixed during training.
When an assistant searches the web or reads your files, the product does that as a separate step first, then adds what it found to the text the model sees.
Real GPT-2 values. Reading a model part-way through is a research technique called the logit lens. It gives an approximate view, because the model was trained to answer only after the final round.
Step 5 of 6
Every possible next piece gets a probability
After the last round the model scores every one of its 50,257 tokens. The raw scores are called logits. They are converted into probabilities that add up to 100%, which gives a probability distribution over the whole vocabulary. That ranked list is the model's entire output.
Look at what it has and has not worked out. It knows a weekday belongs here. It has no information about when your meeting actually is. Wednesday and Tuesday are both fluent, both plausible, and neither has been checked against anything.
One piece is then drawn from the list, with likelier pieces drawn more often. The draw is called sampling. A setting called temperature controls how adventurous the draw is. Low values nearly always take the top choice. High values give unlikely pieces a real chance.
What this means at work
Ask the same question twice and you can get two different answers, because each one is a fresh draw.
A confident tone tells you nothing about accuracy. The model scores what is likely to be written next. That is often what is true and sometimes only what sounds right, so dates, figures, names and citations are worth checking.
No draws yet. The right-hand column will count them.
Real GPT-2 scores for this sentence. The slider recalculates the probabilities across all 50,257 pieces.
Step 6 of 6
One piece is chosen, added, and it all runs again
The chosen piece is added to the end of the text. Then everything you have just seen runs again from the start, on the longer text, to produce one more piece. And again. The loop is called generation, and running a trained model like this is called inference.
A reply of a few paragraphs takes several hundred of these passes. On each pass the model reads its own earlier output as part of the input, exactly as if you had typed it.
Press Play. These are real runs from the same starting sentence. This small model learned from web pages, so it drifts toward news copy.
What this means at work
This is why replies appear a piece at a time. It is also why an early wrong turn tends to stick. The model does not go back and revise; it continues from whatever is already written.
If an answer starts badly, asking again is often quicker than trying to correct it.
Press Play or One step to begin.
Real GPT-2 output. Bars show the model's own probabilities at each step.
Putting it together
The same six steps, at a much larger scale
Systems of this kind are called large language models, or LLMs. GPT-2 is tiny by current standards. The table compares it with GPT-3, a 2020 model from the same family whose dimensions were published. Current assistants are larger again, and their makers generally do not publish exact sizes. They are also trained further to follow instructions and hold a conversation, and they are wrapped in product features such as search, file reading and tools. The core is still the loop you have just watched.
| What you saw | GPT-2 small, 2019 | GPT-3, 2020 |
|---|---|---|
| Layers: rounds of processing (step 4) | 12 | 96 |
| Embedding dimensions: numbers describing each token (step 2) | 768 | 12,288 |
| Attention heads (step 3) | 144 | 9,216 |
| Parameters: adjustable numbers in the whole model | 124 million | 175 billion |
| Context window: tokens it can take in at once | 1,024 | 2,048 |
Where did the model's numbers, its parameters or weights, come from? They started out random. During training the model was shown text with the next piece hidden, and each time its numbers were nudged so that the true next piece scored a little higher. GPT-2 did this over about 40 gigabytes of web text. After training the numbers are fixed. Talking to a model does not change them.
Six things to keep
- It works in pieces and numbersLetters and whole words are never what it sees. Unusual names, codes and long figures are where slips happen.
- Meaning is positionSimilar things sit close together, which makes it flexible with phrasing and prone to swapping near neighbours.
- It uses what is on the pageYour prompt and anything you attach are all it has to work with, alongside what was fixed in training. Put the context in.
- Nothing is looked upUnless the product adds a search or file-reading step, every statement is computed, with no source behind it.
- The output is a list of probabilitiesIt tells you what is likely to be written next. Whether that is true is a separate question, and it is yours to check.
- It writes one piece at a timeEach piece is a fresh draw and none is revised. Asking twice can give two answers, and both can sound equally sure.
The vocabulary, in one place
- Token
- A piece of text the model works with: a word, part of a word, or a punctuation mark.
- Tokenizer
- The tool that cuts text into tokens.
- Vocabulary
- The fixed list of every token a model knows. GPT-2's has 50,257 entries.
- Token ID
- A token's number in the vocabulary.
- Encoding
- Turning text into token IDs. Decoding turns IDs back into text.
- Vector
- A list of numbers treated as one object, like a set of coordinates.
- Embedding
- The vector that stands for a token and places it on the map of meaning.
- Dimensions
- How many numbers a vector holds. GPT-2 uses 768.
- Context
- Everything the model can see when it produces a token: your prompt, attachments and the reply so far.
- Context window
- The most tokens a model can take in at once.
- Attention
- The step in which each token draws information from the tokens before it.
- Attention head
- One attention unit. GPT-2 has 144.
- Layer
- One round of attention followed by processing. GPT-2 has 12.
- Transformer
- The overall design: embeddings passed through a stack of attention layers.
- Logits
- The raw score the model gives each token before scores become probabilities.
- Probability distribution
- The full list of tokens with the probability of each coming next.
- Sampling
- Drawing one token from the distribution, with likelier tokens drawn more often.
- Temperature
- The setting that makes sampling more cautious or more adventurous.
- Generation
- Producing text one token at a time, feeding each output back in as input.
- Inference
- Running a trained model to get output. Nothing in the model changes.
- Parameters
- The adjustable numbers inside a model, also called weights. GPT-2 has 124 million.
- Training
- Adjusting the parameters, over a very large amount of text, to make next-token predictions better.
- Large language model
- A transformer trained on text at this scale or larger. Often shortened to LLM.
What this page simplifies
- GPT-2 predicts text and nothing more. Assistants are built on much larger models that have had further training to follow instructions, and they run inside products that add instructions, memory, search and tools around the model.
- Step 2 leaves out one ingredient: the model also adds information about where each piece sits in the sentence.
- The map in step 2 is a two-dimensional flattening (t-SNE) of 768 dimensions, so distances on it are approximate. The neighbour lists use the full 768.
- Step 3 shows 2 of the model's 144 attention units, chosen because their behaviour is easy to read. Many units do things that are much harder to describe.
- Step 4 uses the logit lens, which reads the model's unfinished working with the tool meant for its final answer. Early rounds are best taken as a rough indication.
- In step 6, the three varied runs drew each piece from the 40 likeliest at temperature 0.8, a common setting. The bars show the model's unadjusted probabilities.
Sources and further reading
- Vaswani et al. (2017), “Attention Is All You Need”. The paper that introduced the design all of these models share.
- Radford et al. (2019), “Language Models are Unsupervised Multitask Learners”. The GPT-2 paper, including its training data. The model itself is published at huggingface.co/openai-community/gpt2.
- Brown et al. (2020), “Language Models are Few-Shot Learners”. The GPT-3 paper. The figures in the table are from its Table 2.1.
- nostalgebraist (2020), “Interpreting GPT: the logit lens”. The technique used in step 4.
- Brendan Bycroft, “LLM Visualization”. A 3D walk through the same steps that shows every calculation, for readers who want the arithmetic.