Part A · Chapter 1 · The pieces

1. What a language model does

A language model does one thing: it gives every word it knows a probability of coming next. Writing, chatting and translating are that one step, repeated.

The next word, again and again
Your first words
Or type your own
Playback speed

The tiny GPT-style model reads the words in the box. Bars show the six words it finds most likely to come next, with their probabilities. The top word is added to the text, the model reads the longer text again, and new bars appear, up to five times or until the model writes its end marker, </s>. Every probability is the model's own.

The idea

Under every chat window and every API call sits the same small machine. You give it some words; it gives back a list: every word it knows, each with a probability of coming next. That is all a language model does.

To write a sentence, it runs that step again and again: take the most likely word (or draw one at random, as chapter 6 shows), add it to the text, and ask again with the longer text. You just watched our tiny model do it. Each round it reads everything written so far, which is how it keeps a sentence together.

Translation works the same way. The paper's model reads an English sentence, then writes the German one word at a time, each step looking back at the English and at the German so far. The GPT-style models behind today's chat APIs drop the separate English side and simply continue the text they are given.

Our two models are tiny. The GPT-style one has read only simple English sentences from a grammar we wrote, and knows 88 English words and punctuation marks; ask it about anything else and it has no word for it. But every number on this page comes from that real model, computed as you read.

A worked example: “The cat”

Give the model “The cat”. It gives “likes” a probability of 0.326, “sees” 0.239 and “eats” 0.221; the other words of its vocabulary share what is left. It takes “likes”, reads “The cat likes”, and now gives “the” 0.724.

Run to the end, it writes “The cat likes the old letter .”: grammatical, and about nothing in particular. It never checks the world; it only knows which words tend to follow which in its training sentences.

The formula

The paper writes the words, or tokens, of a sentence as \(y_1, \dots, y_m\). At each step the model computes one probability for every possible next token:

P(y_t \mid y_1, \dots, y_{t-1})
\(y_t\)
the token at position t: the one being predicted
\(y_1, \dots, y_{t-1}\)
everything written before it: the context
\(P(\,a \mid b\,)\)
the probability of a given b: “the chance of this word, given these words”

The probability of several words in a row is the step probabilities multiplied together, one per word. For two steps of the example:

P(\text{likes the} \mid \text{The cat}) = P(\text{likes} \mid \text{The cat}) \times P(\text{the} \mid \text{The cat likes}) = 0.326 \times 0.724 = 0.236

Why it matters

Every call you make to an LLM API runs this loop. The model reads your prompt, then writes the answer one token at a time, each step a full pass through the model. That is why answers stream in piece by piece, why a long answer takes longer than a short one, and why a limit such as max_tokens simply stops the loop. It is also why output tokens are billed: each one is a step of work.

It explains the most common surprise too: the model does not look anything up. It continues text the way similar text usually continues. When that continuation is false, nothing inside the loop notices.

Where it fails

  • Likely is not true. Our model writes a sentence about a cat liking a letter as happily as a true one. A bigger model's continuations are better, but they are still the probable ones, not checked ones.
  • Always taking the top word is dull. Taking the top word every time (greedy decoding) gives the same answer every time and tends to repeat itself; chat models usually draw from the probabilities instead, which chapter 6 shows.
  • It only knows its own words. Our model cannot read a word it never saw: type one into the box above and it says so. Real models avoid that with tokens made of word pieces (chapter 2).
  • One direction only. Once a word is written, it stays. The model cannot go back and fix the start of its answer; it can only continue.
Remember A language model gives every word it knows a probability of coming next. Writing is that step repeated: pick a word, add it, ask again.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

Predict before you play

Before you pick “She eats” in the animation: which word does the model find most likely to come next?

Two words in a row

After “The cat” the model gives “likes” a probability of 0.326; after “The cat likes” it gives “the” 0.724. What is the probability that “likes the” follows “The cat”? Three decimals.

Your answer
Within 0.002 of the answer

How many runs?

From “The cat”, the model writes “likes the old letter .” and then its end marker, </s>. How many times does the model run, one run for every token it writes, the end marker included?

Your answer
A whole number

What does it compute?

Which sentence describes what a language model computes at each step?

In the paper

The paper's abstract and introduction (section 1) are about sequence transduction: turning one sequence into another, such as an English sentence into German. Its model writes the output the way you just watched; section 3 says so in one line: the model is “auto-regressive, consuming the previously generated symbols as additional input when generating the next”.

In the paper: section Abstract, 1

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.