Module 5 of 6 · 30 minutes

Transformers and how LLMs are trained

How does a next-token predictor become a helpful assistant? This module follows a large language model from raw text to finished product.

Learning objectives

  • Explain the transformer and the idea of attention
  • Distinguish pre-training from post-training
  • Describe RLHF and principle-based approaches such as Constitutional AI
  • Explain reasoning and multimodal models
  • Describe how tools turn models into agents

5.1 The transformer

In 2017, Google researchers published a paper titled "Attention Is All You Need", introducing the transformer. It's the "T" in GPT (Generative Pre-trained Transformer) and the foundation of essentially every major LLM today.

Its key idea is attention: when processing each token, the model looks at all the other tokens in the context and weighs how relevant each one is. In "The trophy didn't fit in the suitcase because it was too big", attention helps the model work out that "it" refers to the trophy. Transformers also process tokens in parallel, which made it practical to train on vastly more data using GPUs.

Thetrophydidn'tfitinthesuitcasebecauseitwastoobig0.900.350.25
Attention, simplified: when processing the token "it", the model weighs every other token. Here most attention goes to "trophy" — which is how it works out what "it" refers to. (Weights illustrative.)

5.2 Stage 1: pre-training

A base model is trained on a huge corpus — web pages, books, code, articles and other sources — to predict the next token. This stage uses enormous amounts of compute and produces a model with broad knowledge and language ability. But a base model isn't a helpful assistant: ask it a question and it might just continue with more questions, as if completing a web page.

5.3 Stage 2: post-training

To turn a base model into an assistant like ChatGPT, Claude or Gemini, developers apply further training:

  • Instruction tuning (supervised fine-tuning): training on examples of good instructions and helpful responses.
  • Reinforcement learning from human feedback (RLHF): people compare pairs of responses; a reward model learns their preferences; the LLM is then trained to produce responses the reward model rates highly.
  • AI feedback and principles: Anthropic's "Constitutional AI" approach uses a written set of principles, with AI-generated feedback, to train the model towards helpful, honest and harmless behaviour. Anthropic now publishes the constitution it uses for Claude.
  • Safety training: teaching models to refuse clearly harmful requests and to resist manipulation.
Why this matters

Post-training shapes an assistant's personality, its helpfulness and its weaknesses. For example, training on human approval can reward answers that please people — one cause of sycophancy, where a model agrees with you even when you're wrong (Module 6).

PromptResponse AResponse BPeoplecompareRewardmodelprefers ALLM updatedscoresthe improved model generates new responses
Reinforcement learning from human feedback (RLHF): people compare responses, a reward model learns their preferences, and the LLM is trained to produce responses the reward model rates highly.

5.4 Reasoning models

Since 2024, developers have released reasoning models that are trained (largely with reinforcement learning) to work through problems step by step before answering — spending more computation "thinking" on harder questions. They tend to do better on maths, coding and multi-step analysis, at the cost of speed and price. OpenAI's prompting guidance compares them to a senior colleague you can give a goal to, while faster general models need more explicit instructions.

5.5 Multimodal models

Modern frontier models are multimodal. Images, audio and documents are converted into token-like representations the transformer can attend to alongside text. That's how you can upload a chart and ask what it shows, or photograph a whiteboard and get a summary.

Pre-trainingtrillions of tokens→ base modelPost-traininginstructions, feedback,safety → assistantApplicationsystem prompt, tools,retrieval → product
The three stages from raw text to the assistant you use.

5.6 From models to tools and agents

On its own, an LLM only produces text. Applications extend it with tools: web search, code execution, file access and connections to other systems (for example through the open Model Context Protocol). An LLM that decides which tools to call, in a loop, to achieve a goal is an agent. The model's knowledge is frozen at training time, but tools let it look things up and act.

StageWhat goes inWhat comes out
Pre-trainingTrillions of tokens of text and code; lots of computeBase model with broad knowledge
Post-trainingInstruction examples, human and AI feedback, safety data, RLHelpful assistant model
ApplicationSystem prompt, tools, retrieval, guardrailsProduct: chatbot, copilot or agent

Key takeaways

  • Transformers (2017) use attention to weigh relevant context, and train efficiently at scale.
  • Pre-training gives broad knowledge; post-training makes a helpful, safer assistant.
  • Training on human approval can encourage sycophancy.
  • Reasoning models spend more computation thinking step by step.
  • Tools and agents extend a frozen model with search and actions.