What Is an LLM
A large language model does one thing: predict the next piece of text, again and again. See the loop step by step, with a toy model in JS and Python.
On this page
Type "See you" on your phone and the keyboard offers three guesses for the next word: "soon", "later", "tomorrow". Tap one, and it offers three more. Keep tapping the first suggestion and you get a sentence. Usually a strange one, because the keyboard only knows a little about how people write.
A large language model, or LLM, works on the same principle. It reads the text so far and predicts what comes next. The difference is scale: it has learned from an enormous amount of writing, and it looks at everything you wrote, not just the last word. That is enough to turn a keyboard trick into something that can draft an email, explain a bug or summarise a report.
One job: predict what comes next
Give a model some text and it does not return an answer. It returns a list of chances: for every piece of text it knows, how likely that piece is to come next. For "The capital of France is", the word "Paris" gets a very high chance, and "a" gets a small one (as in "is a city of...").
To write more than one word, you run a loop:
- Give the model the text so far.
- Get the chances for the next piece.
- Pick one piece.
- Stick it on the end of the text, and go back to step 1.
Stop when the model picks a special "end" piece or the text reaches a length limit. The long replies you get from an AI assistant are built this way, one piece at a time.
The "pieces" are called tokens. They are often whole words, but not always. The next lesson, Tokens, shows how text is cut up. For now, think of them as words.
Watch the loop by hand
Here is a toy model. It is a small table that maps the last two words to chances for the next word. A real model is not a table, and it reads the whole text rather than two words, but the loop around it is the same. This version always takes the most likely word, which is called greedy picking.
text = start wordsrepeat: context = last two words of text chances = model(context) word = most likely word in chances append word to text if word is ".": stop- text
- the cat
1Start with the text
the cat. The model has not been called yet.- next word0sat1ran2ischance00.610.320.1
- text
- the cat
- context
- the cat
2Ask the model about
the cat. It says "sat" is most likely, at 0.6. Note that 0.6 + 0.3 + 0.1 = 1. In a real model the chances over every possible piece add up to 1. - text
- the cat sat
- context
- cat sat
3Append "sat". The text grew by one word, so the context for the next call changes to
cat sat.- next word0on1down2stillchance00.710.220.1
- text
- the cat sat on
- context
- cat sat
4For
cat sat, "on" wins with 0.7 and is appended. "down" was a real option too; greedy picking ignores it. - next word0the1a2mychance00.810.1520.05
- text
- the cat sat on the
- context
- sat on
5For
sat on, "the" wins with 0.8 and is appended. The model is confident here because this phrase almost always continues this way. - next word0mat1sofa2roofchance00.510.320.2
- text
- the cat sat on the mat
- context
- on the
6For
on the, "mat" wins and is appended, but only with 0.5. The model is much less sure: "sofa" or "roof" would be fine sentences too. - next word0.1and2todaychance00.610.320.1
- text
- the cat sat on the mat .
- context
- the mat
7For
the mat, a full stop wins with 0.6. The loop sees "." and stops. - text
- the cat sat on the mat .
- model calls
- 5
8Five model calls produced five new words. That is the whole trick: a long answer is just many calls in a row, each one adding a single piece.
The loop in code
The same toy model in real code. The dictionary plays the part of the model; everything else is the generation loop you just traced.
// A stand-in for the model: last two words -> chances for the next word.
const nextWord = {
"the cat": { sat: 0.6, ran: 0.3, is: 0.1 },
"cat sat": { on: 0.7, down: 0.2, still: 0.1 },
"sat on": { the: 0.8, a: 0.15, my: 0.05 },
"on the": { mat: 0.5, sofa: 0.3, roof: 0.2 },
"the mat": { ".": 0.6, and: 0.3, today: 0.1 },
};
function generate(words, maxWords) {
const text = [...words];
while (text.length < maxWords) {
const options = nextWord[text.slice(-2).join(" ")];
if (!options) break; // the toy model has nothing to say
let best = null; // greedy: take the most likely word
for (const [word, p] of Object.entries(options)) {
if (best === null || p > options[best]) best = word;
}
text.push(best);
if (best === ".") break;
}
return text.join(" ");
}
generate(["the", "cat"], 20); // "the cat sat on the mat ."# A stand-in for the model: last two words -> chances for the next word.
NEXT_WORD = {
"the cat": {"sat": 0.6, "ran": 0.3, "is": 0.1},
"cat sat": {"on": 0.7, "down": 0.2, "still": 0.1},
"sat on": {"the": 0.8, "a": 0.15, "my": 0.05},
"on the": {"mat": 0.5, "sofa": 0.3, "roof": 0.2},
"the mat": {".": 0.6, "and": 0.3, "today": 0.1},
}
def generate(words, max_words):
text = list(words)
while len(text) < max_words:
options = NEXT_WORD.get(" ".join(text[-2:]))
if options is None:
break # the toy model has nothing to say
best = max(options, key=options.get) # greedy: most likely word
text.append(best)
if best == ".":
break
return " ".join(text)
generate(["the", "cat"], 20) # "the cat sat on the mat ."Writing n new words takes n calls to the model, one after another. Each call needs the word before it, so they cannot run side by side. That is why long replies appear gradually on screen, and why a long answer costs more time than a short one.
What "large" changes
The toy table has five rows. A real LLM replaces the table with a neural network: billions of numbers, called parameters, that together turn the text so far into chances for the next token. The Neural Networks lesson shows what those numbers do; the Attention and Transformers lesson shows the design that lets every word take every other word into account.
Nobody writes those numbers by hand. They are set during training, by showing the network huge amounts of text and nudging the numbers so that the real next token gets a higher chance each time. Training and Fine-Tuning covers this.
Something surprising happens at that scale. To predict the next token of a physics textbook, a recipe or a Python file well, it helps to have absorbed a lot about physics, cooking and Python. Nobody programs those skills in. They are a side effect of getting very good at prediction.
A chat is a document being continued
When you chat with an assistant, the app joins your messages, its earlier replies and some hidden instructions into one long piece of text, with markers showing who said what. The model's job is to continue that text with the assistant's next turn.
The model does not remember your earlier messages between calls. The app sends the whole conversation again every time, and the model reads it fresh. If a message falls out of that text, the model no longer knows it was said. What Is Context builds on exactly this.
Common mistakes
Try it yourself
For each start of a sentence, write the three next words you think are most likely, and give each a rough chance that adds up to about 1 across the three (leave some room for everything else). Then note which one is hardest to predict, and why.
Show a sample answer
One reasonable set of guesses:
- "much" 0.85, "very" 0.05, "kindly" 0.02. This is almost certain: the phrase is extremely common.
- "Monday" 0.15, "Thursday" 0.15, "next" 0.15, with the rest spread over other days, dates and "the". The kind of word is clear (a time), but the exact word is not.
- "blue" 0.3, "green" 0.15, "red" 0.1. Many answers are fine, and the text gives no clue which one this person means.
What this shows: a model is confident when the text pins the answer down, and spreads its chances out when it does not. In case 2, the true answer is a fact about your meeting that is nowhere in the text. A model has to guess it, and if it picks "Thursday" it will write it just as smoothly as it wrote "much".
Check yourself
What does a language model produce each time it is called?
Using the loop from this lesson, the model writes a reply of 200 tokens. How many times is the model called?
You tell an assistant your name in the first message. Twenty messages later it still uses it. Why?
What to practise next
Continue with Tokens, which explains the pieces the model actually predicts and why that matters for cost, limits and some odd mistakes. Later, Sampling and Temperature replaces the greedy pick from this lesson with the methods real systems use.