How Large Language Models Work
Next-token prediction, training data, the transformer and attention, pretraining vs instruction tuning vs RLHF, and why models can be wrong.
What is it?
A Large Language Model does one thing: given some text, it predicts what comes next. Everything else (answering questions, writing code, following instructions) is built on that single skill.
Text is first split into tokens: small chunks like whole words, parts of words or punctuation (un, believ, able). Each token has an id number.
The model reads a sequence of token ids and outputs a probability distribution over its whole vocabulary for the next token: a list of possible next tokens, each with a likelihood, adding up to 1 (100%). For 'The cat sat on the', it might output mat 0.41, floor 0.18, sofa 0.09, ... and thousands more with tiny probabilities.
To generate text, the system picks one token from that distribution, adds it to the input, and asks the model again, one token at a time. This is autoregressive generation. When a chatbot seems to 'type', you are watching this loop.
Where does the knowledge come from? In pretraining, the model reads a huge amount of text (web pages, books, code, articles). At every position it guesses the next token, compares the guess with the real one, and adjusts its parameters slightly using gradient descent (see what-is-ai).
After trillions of guesses, the parameters encode grammar, facts, reasoning patterns and styles, because predicting text well requires them. To finish 'The capital of France is', it helps to have learned that the answer is Paris.
The transformer. Modern LLMs use a neural network design called the transformer. Its key idea is attention: for each token, the model looks back at all earlier tokens and decides how much each one matters. In 'The animal didn't cross the street because it was too tired', attention lets the model link it to animal.
Each token is a list of numbers (a vector). Attention scores how well each pair of tokens 'matches' and blends in information from the best matches. A transformer stacks dozens of attention layers, each refining the representation.
From text predictor to assistant. A model that is only pretrained (a base model) just continues text. Ask it 'What is the capital of France?' and it may reply with more quiz questions, because that is what such text looks like on the web. Two more training stages make it a helpful assistant:
- Instruction tuning (supervised fine-tuning): more training on many instructions paired with good responses, written or curated by people. The model learns the format of being an assistant: read the request, answer it.
- Reinforcement learning from human feedback (RLHF) and related methods: the model writes several answers, people (or a model trained on their preferences) rank them, and the model is trained to produce more of the preferred kind: more helpful, more honest, safer. Some labs also use AI feedback guided by a written set of principles.
Why models can be wrong. An LLM is trained to produce plausible text, not verified true text. It has no built-in fact database and no automatic way to check itself. If the training data on a topic was thin, outdated or contradictory, the most plausible answer may be false. This is a hallucination.
Its knowledge also stops at a training cutoff date. It does not know about later events unless you put that information in the prompt.
What the model does NOT do:
- Search the internet by itself (unless you give it a tool).
- Remember your previous conversations (unless you send them again).
- Learn from your chats in real time. Its parameters are fixed during inference.
Explain like I'm 10
Picture the world's keenest reader playing a game: you read them the start of a sentence and they guess the next word. After reading a library bigger than any human could, they get so good that 'guessing the next word' after a question produces a correct, well-written answer. Instruction tuning is finishing school: it teaches them to answer requests instead of rambling on. But they are still guessing. If they never read about something, they may confidently guess wrong instead of saying 'I don't know'.
Examples
A toy next-word predictor from bigram counts
// A "bigram model": predict the next word from the current word only,
// using counts from a tiny training corpus. LLMs use the WHOLE preceding
// context and billions of parameters, but the job is the same.
const corpus = [
"the cat sat on the mat",
"the cat ate the fish",
"the dog sat on the rug",
"the dog chased the cat",
];
// Training: count which word follows which
const next = {};
for (const sentence of corpus) {
const words = sentence.split(" ");
for (let i = 0; i < words.length - 1; i++) {
const a = words[i], b = words[i + 1];
next[a] = next[a] || {};
next[a][b] = (next[a][b] || 0) + 1;
}
}
// Inference: turn counts into a probability distribution
function predict(word) {
const options = next[word] || {};
const total = Object.values(options).reduce((s, c) => s + c, 0);
return Object.entries(options)
.map(([w, c]) => [w, c / total])
.sort((x, y) => y[1] - x[1]);
}
for (const w of ["the", "cat", "sat"]) {
const dist = predict(w).map(([t, p]) => t + "=" + p.toFixed(2)).join(", ");
console.log("after '" + w + "':", dist);
}
// Autoregressive generation: always take the most likely word (greedy)
let word = "the";
const out = [word];
for (let i = 0; i < 5; i++) {
const dist = predict(word);
if (dist.length === 0) break;
word = dist[0][0];
out.push(word);
}
console.log("generated:", out.join(" "));The generated text loops ('the cat sat on the cat ...') because a bigram model sees only one word of context. Transformers fix this with attention over the whole context, which is why they write coherent paragraphs.
Attention intuition: which earlier word does 'it' look at?
// Each token gets a small vector (real models: thousands of numbers).
// Attention = compare the current token's QUERY with every token's KEY,
// turn the scores into weights with softmax, and blend.
const tokens = ["the", "animal", "crossed", "the", "street", "because", "it"];
const keys = {
the: [0.0, 0.1, 0.0],
animal: [0.9, 0.8, 0.1],
crossed: [0.1, 0.0, 0.9],
street: [0.2, 0.9, 0.0],
because: [0.0, 0.0, 0.3],
it: [0.6, 0.4, 0.1],
};
const query = [3.0, 1.5, 0.0]; // what "it" is looking for: a living thing / a noun
const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0);
const scale = Math.sqrt(query.length);
const scores = tokens.map(t => dot(query, keys[t]) / scale);
// softmax: exponentiate and normalise so weights sum to 1
const exps = scores.map(s => Math.exp(s));
const sum = exps.reduce((a, b) => a + b, 0);
const weights = exps.map(e => e / sum);
tokens.forEach((t, i) => {
const bar = "#".repeat(Math.round(weights[i] * 60));
console.log(t.padEnd(8), weights[i].toFixed(3), bar);
});The vectors here are hand-picked to show the idea. In a real transformer, learned parameters compute the query and key vectors from the tokens. Many attention 'heads' run in parallel, each tracking a different kind of relationship (who did what, which noun a pronoun refers to, matching brackets in code...).
Watching autoregressive generation happen (Python, streaming)
import anthropic
client = anthropic.Anthropic()
# Streaming shows text as it is generated, chunk by chunk.
with client.messages.stream(
model="claude-opus-5-5",
max_tokens=64000,
messages=[{"role": "user", "content": "Write one sentence about why the sky is blue."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
final = stream.get_final_message()
print()
print("output tokens generated:", final.usage.output_tokens)Each printed chunk is one or a few newly generated tokens. The output token count tells you how many times the model ran its next-token step.
How it works
Step 1 - Tokenise. The input text becomes a list of token ids, e.g. [464, 3797, 3332, 319, 262].
Step 2 - Embed. A learned table maps each token id to a vector (a list of numbers). Each token's position is added too; otherwise the model could not tell 'dog bites man' from 'man bites dog'.
Step 3 - Transformer layers. The vectors pass through many layers. In each layer, an attention step lets every token gather information from relevant earlier tokens. A feed-forward step then transforms each token's vector on its own; much of the learned knowledge is thought to live here.
In a language model, attention is causal: a token can only look at tokens before it, because at generation time the future does not exist yet.
Step 4 - Predict. The final vector at the last position becomes one score per vocabulary token (logits). A function called softmax turns those scores into probabilities.
Step 5 - Sample. One token is picked from the distribution (see sampling-and-model-settings for how) and added to the input. Steps 1-5 repeat until the model outputs a special end-of-turn token or hits the maximum length you set.
Training stages, in short:
- Pretraining: predict the next token on a huge text corpus. Learns language and knowledge.
- Instruction tuning: learn to follow instructions from example conversations.
- Preference training such as RLHF: learn which of several answers people prefer.
The later stages are much smaller than pretraining but change the model's behaviour a lot.
Why it can be wrong: the model outputs the most plausible text, and plausible is not the same as true. Gaps or errors in training data, the training cutoff, vague prompts and random sampling all lead to fluent but wrong answers. Good AI engineering puts the right facts in the prompt (RAG), lets the model use tools, and checks outputs.
"The cat sat on the"
| tokenise
v
[464][3797][3332][319][262]
| embed (+ position)
v
+-----------------------------+
| transformer layer 1 |
| attention: look back |
| feed-forward: transform |
+-----------------------------+
| ... many layers ...
v
scores for every token --> softmax
v
mat .41 | floor .18 | sofa .09 ...
| sample one
v
"mat" --> append, repeatWhy does it exist?
Next-token prediction is self-supervised: the 'right answer' at every position is just the next token in existing text. No human labelling is needed, so you can train on almost unlimited data. Predicting text well forces the model to learn language, facts and reasoning patterns along the way.
The transformer exists because earlier sequence models read text one word at a time and struggled to connect distant words. Attention links any two positions directly and runs in parallel on GPUs, which made training on huge datasets practical.
Instruction tuning and RLHF exist because a raw text predictor is not a helpful assistant. These stages align the model's behaviour with what users want.
When to use it
This mechanism explains much of daily AI engineering: why the prompt matters so much (it is the context for prediction), why you pay per token, why long outputs take longer (one token at a time), why the model may be outdated (training cutoff), and why you must supply facts and check outputs.
When not to use it
You don't need transformer maths to build good applications, so don't get stuck on matrix calculus before shipping your first prompt.
Also avoid treating the model like a person. 'The model thinks/knows/wants' is handy shorthand, but design your systems around what it really is: a powerful next-token predictor shaped by training.
Common mistakes
Believing the model retrieves sentences from a stored copy of its training data; it generates from learned parameters.
Expecting the model to know recent events after its training cutoff without being told.
Assuming the model learns from your conversation permanently; parameters do not change during inference.
Thinking the model plans the whole answer before writing it - it produces one token at a time, so asking it to reason before answering can help.
Treating fluent, confident text as evidence of correctness.
Confusing a base model (continues text) with an instruction-tuned chat model (follows requests).
Assuming the model sees characters; it sees tokens, which is why letter-counting and spelling tasks can trip it up.
Practice exercises
- Easy:
In the bigram demo, add two sentences to the corpus and print the new distribution after 'the'. Which probabilities changed and why?
- Easy:
Explain to a friend, in four sentences, the difference between pretraining, instruction tuning and RLHF.
- Medium:
Extend the bigram model into a trigram model: predict the next word from the previous TWO words. Does greedy generation still loop?
- Medium:
In the attention demo, change the query vector so that 'it' attends most to 'street'. What does that correspond to in the sentence?
- Hard:
Build a character-level bigram model: train it on a paragraph of text, then generate 80 characters by sampling (not greedy) using a seeded random number generator. Compare the output with the greedy version.
Interview questions
What is an LLM actually trained to do?
Predict the next token given the previous tokens. During pretraining it sees vast amounts of text and adjusts its parameters to make the real next token more likely. Generation repeats that prediction one token at a time.
What is attention, intuitively?
A mechanism that lets each token weigh how relevant every earlier token is and mix in information from the relevant ones. It is computed by comparing query and key vectors, normalising the scores with softmax, and averaging value vectors with those weights.
What is the difference between a base model and a chat/instruct model?
A base model has only been pretrained and continues text. A chat model has additionally been instruction-tuned and preference-trained (e.g. RLHF) to follow instructions, answer helpfully and refuse harmful requests.
What does RLHF do?
Reinforcement learning from human feedback: people compare model outputs, a reward signal is built from those preferences, and the model is optimised to produce outputs people prefer. It shapes helpfulness, tone, honesty and safety rather than teaching new knowledge.
Why do LLMs hallucinate?
They are trained to produce plausible continuations, not verified facts. With gaps or errors in training data, a training cutoff, ambiguous prompts or randomness in sampling, the most plausible text can be wrong, and the model has no built-in fact check.
Why is generation slower than reading the prompt?
The prompt's tokens can be processed in parallel, but output is autoregressive: each new token depends on the previous one, so tokens are produced one after another.
Does an LLM remember previous conversations?
Not by itself. Its parameters are fixed at inference time and the API is stateless. Any memory comes from the application sending previous messages or retrieved information again in the prompt.