What Is an LLM
How a large language model generates text one token at a time, how chat assistants are built on that loop, and what it means for using them well.
On this page
A large language model, or LLM, is a program that reads text and predicts which piece of text is most likely to come next. Everything it does, including answering questions, summarising reports and writing code, comes from repeating that prediction many times. Knowing this explains both what these systems do well and why they fail in the ways they do.
If you only think of an LLM as something that "knows" things, its mistakes look random. If you think of it as something that continues text plausibly, most of them become predictable: it can write a confident sentence about a fact it never learned, and it cannot take back a sentence it has already written.
How the model generates text
Given some text, the model returns a probability for every piece of text it knows, saying how likely that piece is to come next. For "The capital of France is", the piece "Paris" gets a very high probability, and a piece such as "a" gets a small one, because "a" would start a phrase like "a city in Europe".
The model produces only one piece of text per call, so longer output comes from a loop outside the model. The program around the model gives it the text so far, receives the probabilities, chooses one piece, appends it to the text, and calls the model again with the longer text. The loop stops when the model chooses a special end marker or when a length limit is reached.
The pieces of text are called tokens. They are often whole words but can also be parts of words or single characters, and Tokens explains how text is split. This lesson treats tokens as words to keep the examples readable.
A worked example with a toy model
The toy model below is a small table that maps the last two words to probabilities for the next word. A real model is not a table and reads the whole text rather than two words, but the loop around it is the same. This version always chooses the most likely word, which is called greedy selection.
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 cat.- next word0sat1ran2ischance00.610.320.1
- text
- the cat
- context
- the cat
2For
the cat, "sat" is the most likely next word, at 0.6. The probabilities add up to 1. - text
- the cat sat
- context
- cat sat
3Append "sat". The context for the next call is now
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. Greedy selection ignores "down", even though it has a 0.2 chance. - next word0the1a2mychance00.810.1520.05
- text
- the cat sat on the
- context
- sat on
5For
sat on, "the" wins with 0.8, because this phrase almost always continues that way. - next word0mat1sofa2roofchance00.510.320.2
- text
- the cat sat on the mat
- context
- on the
6For
on the, "mat" wins with only 0.5, although "sofa" or "roof" would be just as reasonable. - 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
8The loop made five model calls and added five words. A long answer is produced the same way, by many calls in a row.
The same loop in code, with a dictionary standing in for the model:
// 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 ."Generating n new tokens takes n calls that must run one after another, because each call needs the token chosen by the previous one. This is why replies appear gradually and why longer replies take longer.
What makes a model large
A real LLM replaces the five-row table with a neural network, which is a large set of numbers called parameters that turns the text so far into probabilities for the next token. The Transformer design lets the network take every earlier token into account when it makes a prediction, instead of only the last two.
The parameters are set during training. The network is shown a very large amount of text, and after each example its parameters are adjusted so that the token that really came next gets a higher probability. Training and Fine-Tuning covers this in detail.
Predicting text well across many subjects forces the network to absorb a great deal about those subjects. To continue a physics textbook, a recipe or a Python file correctly, it has to pick up physics, cooking and Python. Nobody programs these skills in; they are a side effect of getting the prediction right.
How the next token is chosen
The model only produces probabilities, so the choice of token is made by ordinary code around it, and that choice changes the output a lot.
Greedy selection
Greedy selection always takes the most likely token, as in the trace above. The output is the same on every run, which is useful when you want repeatable results. The weakness is that the text tends to be plain and repetitive, and one early choice that was only slightly more likely than the others fixes the direction of everything after it.
Sampling
Sampling picks a token at random, weighted by its probability, so a token with 0.2 is chosen about one time in five. The same prompt can then give different answers on different runs. Sampling is the usual setting for chat and writing, where variety is wanted. A setting called temperature makes the choice more or less random, and Sampling and Temperature covers it.
For tasks with one correct answer, such as extracting a field from a document, you generally want the output to be as predictable as possible. For writing and brainstorming, you want some variety. Because sampling can pick an unlikely token, even a correct model can occasionally produce a wrong answer to a question it usually gets right, so one successful run does not show that a prompt is reliable.
How a chat assistant works
A chat assistant does not work differently from the loop above. The application joins the hidden instructions, your earlier messages, the earlier replies and your new message into one long text, with markers that show who said what. The model then continues that text with the assistant's next turn.
An assistant is made by first training a model on general text and then training it further on examples of helpful conversations, so that continuing a chat means writing a useful reply. The mechanism is still next-token prediction.
The model keeps nothing between calls. The application sends the whole conversation every time, and if a message is cut from that text because it no longer fits, the model has no way to know it was ever said. What Is Context builds on this.
Limitations that follow from next-token prediction
Stated facts are not looked up
A plain LLM has no database to consult. When it states a fact, that fact is the most plausible continuation of the text, which is often correct because the fact appeared many times in training, and sometimes a confident guess. Names, dates, citations and numbers that are rare in the training text are the most likely to be wrong, and Why Models Hallucinate covers this. If the answer must be correct, put the source text into the prompt or use a system that retrieves documents for the model, and check the result.
Output cannot be revised
The model commits to each token as it writes it and never goes back. If it begins an answer with a wrong claim, the rest of the text is then written to be consistent with that claim. For problems that need several steps, ask the model to work through the steps before it gives its final answer, as described in Give the Model Room to Think.
Fluent text says nothing about correctness
Smooth, well-organised sentences are what the model produces best, whether or not the content is right. The tone of an answer is therefore not evidence that it is accurate, and a wrong answer is usually as polished as a right one.
What this changes in practice
- Put everything the model needs into the prompt. It has no access to your documents, your earlier chats or your intent unless that text is in what you send.
- Treat answers about specific facts as claims to verify, and give the model the source material when accuracy matters.
- Expect different answers on different runs when sampling is on, and test a prompt several times before you rely on it.
- Start a fresh conversation when an earlier wrong turn is steering the answers. The wrong text is part of what the model continues, so correcting it in place often works less well than starting again with a better first message.
- Remember that you pay in time and cost for every token read and written, so long prompts and long replies are slower and more expensive.
For each sentence start, write the three most likely next words with rough probabilities, leaving some room for all other words. Which start is hardest to predict, and why?
Show a sample answer
- "much" 0.85, "very" 0.05, "kindly" 0.02. The next word is almost certain because 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, since it will be a time, but the exact word is not.
- "blue" 0.3, "green" 0.15, "red" 0.1. Many answers fit, and the text gives no clue about which one is meant.
A model is confident when the text pins the answer down, and its probabilities are spread out when it does not. In case 2 the true answer is a fact about your meeting that appears nowhere in the text. The model has to guess, and it will write "Thursday" as smoothly as it wrote "much" in case 1, which is why you should supply facts like this in the prompt.
What does a language model produce each time it is called?
With the loop in this lesson, how many model calls does a 200-token reply take?
You tell an assistant your name in the first message, and twenty messages later it still uses it. Why?
You run the same prompt twice with sampling on and get two different answers. What is the most likely reason?
What to practise next
Tokens explains the pieces the model predicts and why they affect cost, limits and some odd mistakes. Sampling and Temperature then covers how real systems choose among the likely tokens.