How an LLM is trained: data, next-token prediction and alignment

An LLM learns by playing one game trillions of times: predict the next token. It reads text with the next token hidden, gives a probability to every possible token, and is nudged towards the right one. This self-supervised pre-training needs no human labels. Later stages teach the model to follow instructions and to behave safely.

Everyday analogy

A child who learns to finish sentences by listening to millions of conversations, long before anyone teaches them manners. Pre-training is the listening; alignment is the manners.

Example

Given "The sky is", the model might give blue 62%, clear 14% and grey 8%. If the real next word was "blue", training slightly raises that probability for next time.

“The sky is …”the model scores every possible next tokenblue62%clear14%grey8%falling4%
Training nudges these probabilities so the real next word gets a higher score next time.

The training pipeline

1. CollectWeb pages, books, code, papers: trillions of tokens
→
2. CleanRemove duplicates, spam, toxic content, personal data
→
3. TokenizeText to token IDs
→
4. Pre-trainNext-token prediction on thousands of GPUs for weeks to months
→
5. EvaluateLoss, perplexity, benchmarks
→
6. Fine-tune and alignSFT, RLHF, DPO
→
7. DeployServe it for inference

The training score is called cross-entropy loss. It is low when the model gave the correct next token a high probability, and high when it was confidently wrong. Training is the process of lowering it.

Alignment: from clever to helpful and safe

MethodHow it works
SFT (supervised fine-tuning)Train on thousands of good examples of instruction → response
RLHFPeople rank several answers; a reward model learns their preferences; the LLM is tuned to score well
Constitutional AIDeveloped by Anthropic: the model critiques and revises its own answers against written principles, reducing the need for human labels
DPO (direct preference optimisation)Learns directly from pairs of preferred and rejected answers; simpler than RLHF, with no separate reward model
Safety fine-tuningExtra training on examples of harmful requests and safe responses

Get the wording right: a model has billions of parameters and is trained on trillions of tokens. For scale, GPT-3 (2020) was trained on about 300 billion tokens; today's leading models use many trillions.

Try it yourself

About 10 min

Play the next-token game

  1. Write 4 sentence starts, e.g. The best thing about Mondays is.
  2. Ask an AI chatbot for the 5 most likely next words for each, with rough percentages.
  3. Compare with your own guesses.

What you should see: A spread of likely words with probabilities: the same game the model played trillions of times in training.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Build it for real · guided projectBuild your own custom AI assistantAbout 60 min · No coding needed · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.