Lesson 6.1 · Stage 06: LLMs, agents and building with AI
How a large language model is trained
The three stages behind models like GPT and Claude: pre-training, supervised fine-tuning and alignment, in plain language.
Training a large language model (LLM) happens in three stages: pre-training, supervised fine-tuning, and alignment. Each stage uses the result of the one before.
Stage 1: pre-training
This is the expensive stage.
- Tokenise the text. The training text is broken into small pieces called tokens. A word such as “database” may become two tokens, “data” and “base”.
- Start with random weights. The model is a transformer: a stack of layers, each made of attention and a feedforward network. At the start its weights are random numbers.
- Predict the next token. The model reads some text and outputs a probability for every possible next token. For “The capital of France is”, the correct next token is “Paris”.
- Measure the error and adjust. The error is measured with cross-entropy loss. Backpropagation works out how each weight contributed to the error, and an optimiser (usually AdamW) nudges the weights to reduce it.
- Repeat across the whole text collection. This runs on thousands of GPUs for weeks or months.
The result is a base model. It can continue any piece of text, but it does not yet follow instructions well.
Stage 2: supervised fine-tuning
The base model is trained further on a much smaller set of examples, each one a prompt paired with an ideal response. The training task is still next-token prediction, applied to the response part.
This stage teaches the format of being an assistant: answer the question, write the code, decline harmful requests.
Stage 3: alignment
The model learns which of its answers people prefer.
- People, or another AI model acting as a judge, compare two responses and pick the better one.
- Those preferences train a reward model that scores responses.
- The language model is then optimised to produce responses that score well, while a penalty stops it drifting too far from the fine-tuned model. This is called RLHF, reinforcement learning from human feedback. A related method, DPO, learns from the preferences directly without a separate reward model.
Newer models add reinforcement learning on tasks with checkable answers, such as maths problems and code with tests. This is one of the ways reasoning ability is built.
Evaluation
Between and after these stages the model is tested on benchmarks, probed for weaknesses by red teams, and checked for safety. The results feed back into further rounds of fine-tuning.
Where the training text comes from
The text is mostly public: web pages that crawlers can reach, curated collections such as books and reference works, datasets the developer has licensed, text written or labelled by people for training, and synthetic text generated by other models. Private data stored in cloud services is not part of the public web.
The whole pipeline in one table
| Stage | Data | What the model learns |
|---|---|---|
| Pre-training | Very large amounts of general text | Language, facts and patterns |
| Supervised fine-tuning | Prompt and ideal-response pairs | How to behave as an assistant |
| Alignment | Human or AI preferences between answers | Which answers people find better |