How LLMs are trained

In one sentence

Pretraining on trillions of tokens builds knowledge into a base model; SFT teaches it to follow instructions; preference tuning (RLHF/DPO) shapes style and safety; RL with verifiable rewards teaches it to reason on checkable problems.

Analogy: Pretraining is reading the whole library; SFT is an apprenticeship with worked examples; preference tuning is an editor picking the better of two drafts; RLVR is a problem set graded by an answer key.

Left to right, compute drops by orders of magnitude while the change shifts from knowledge to behaviour.

How it works

  • Pretraining yields a base model: a superb autocompleter that may answer a question with more questions.
  • Later stages change behaviour more than knowledge: 1,000 curated SFT examples made a 65B base model an assistant (LIMA).
  • RLHF optimizes against a trained reward model with PPO; DPO skips it and learns from pairs directly.
  • RLVR rewards attempts a program verifies (tests pass, answer matches), producing long chain-of-thought reasoning models.
  • DeepSeek-V3: ~2.66M GPU-hours of pretraining vs ~5K for all post-training; pretraining buys knowledge and >95% of compute.

Check yourself

Answer to mark it learned; your answers aren't scored.

Your math-tutor product runs on an open SFT model. It follows the format well but fails multi-step problems, and every problem has a checkable numeric answer. There is no budget for human labelers. Which training stage fits best?

Go deeper

Full explanation

A modern LLM is built in stages. Each is far cheaper than the one before and changes behaviour more than knowledge.

  1. Pretraining. Predict the next token over trillions of tokens of web, code, books and papers (~15T for Llama 3, ~36T for Qwen3). Knowledge and skills come from here, and so does nearly all the compute: months on thousands of GPUs. The output is a base model, a superb autocompleter that may answer a question with more questions.
  2. Supervised fine-tuning (Supervised fine-tuning (SFT)). Train on tens of thousands to millions of curated (prompt, ideal response) conversations in a chat template. The model learns the format of being an assistant.
  3. Preference tuning. Humans or AI judges pick the better of two responses. RLHF: reinforcement learning from human feedback trains a reward model and optimizes against it with RL (PPO); DPO & preference optimization skips the reward model and learns from the pairs directly. This shapes helpfulness, tone and refusals.
  4. RL with verifiable rewards (RL with verifiable rewards (GRPO)). Sample many attempts at math, code or other checkable tasks and reward the ones a program verifies (tests pass, answer matches). This produced the long chain-of-thought Reasoning models & test-time compute such as o1 and DeepSeek-R1.

An instruct/chat model is a base model plus stages 2–4. Labs also add mid-training (long-context extension, annealing on high-quality data) and Knowledge distillation into smaller models.

Code One training example per stage (TRL-style formats)
pythonOne training example per stage (TRL-style formats)
# Pretraining: raw text, loss on every next token. No notion of question or answer.
pretraining = "The Treaty of Westphalia was signed in 1648, ending the Thirty Years' War ..."

# SFT: conversations; loss usually only on the assistant tokens
sft = {"messages": [
    {"role": "user", "content": "Summarize this ticket in one line: ..."},
    {"role": "assistant", "content": "Customer can't reset password: SSO redirect loop."},
]}

# Preference tuning (DPO): prefer chosen over rejected, stay close to the SFT model
dpo = {
    "prompt": [{"role": "user", "content": "Explain recursion to a 10-year-old."}],
    "chosen": [{"role": "assistant", "content": "Picture nesting dolls ..."}],
    "rejected": [{"role": "assistant", "content": "Recursion is a function invoking itself ..."}],
}

# RLVR: a program, not a person, grades each sampled attempt
def math_reward(completions, answer, **kwargs):
    return [1.0 if extract_final_answer(c) == a else 0.0
            for c, a in zip(completions, answer)]

# trainer = trl.GRPOTrainer(model=sft_model, reward_funcs=[math_reward],
#                           train_dataset=math_problems)
Real example

DeepSeek-V3 reported its final training run at ~2.79M H800 GPU-hours (~$5.6M at an assumed $2/hour, excluding earlier research and ablations). Pretraining on 14.8T tokens took ~2.66M of those hours, context extension ~119K, and all post-training (SFT + RL) only ~5K. That ratio is the lesson: pretraining buys knowledge and >95% of the compute; post-training is cheap and changes behaviour. DeepSeek-R1 then ran large-scale RL with rule-based rewards on top of V3-Base, and R1-Zero showed pure RL, with no SFT at all, can elicit long reasoning, at the cost of poor readability and language mixing.

Trade-offs +3 · −3

+ You gain

  • Separates expensive knowledge acquisition from cheap behaviour shaping: post-training can be redone in days.
  • Each stage is a reusable lever; most companies only touch SFT or DPO on an open model.
  • RLVR scales with compute alone wherever answers are checkable, with no human labels.

− You pay

  • Post-training adds little knowledge: facts missing from pretraining stay missing, and forcing them in raises hallucinations.
  • Preference tuning can over-optimize the reward: sycophancy, verbosity, over-refusal.
  • RLVR only works where a program can verify the answer; open-ended quality still needs human or AI judges.

Use it when

Use this map to pick what to change: missing knowledge → retrieval or continued pretraining; wrong format or behaviour → SFT; tone or preferences → DPO; reasoning on checkable tasks → RLVR. Also when choosing base vs instruct checkpoints.

Avoid it when

Don't plan to pretrain unless you're a frontier lab or own a truly unusual corpus and a multi-million budget; products start from an API or open instruct model, then prompt, retrieve or fine-tune.

Numbers to know 5
  • Pretraining data: Llama 3 ~15T tokens; DeepSeek-V3 14.8T.
  • Llama 3.1 405B: ~3.8×10^25 FLOPs on up to 16k H100 GPUs.
  • DeepSeek-V3: ~2.79M H800 GPU-hours in total, only ~5K of them post-training.
  • InstructGPT: labelers preferred the 1.3B instruct model over the 175B GPT-3 base.
  • LIMA: 1,000 curated SFT examples turned a 65B base model into a strong assistant.
Common pitfalls 4
  • Using a base model for chat (it continues text instead of answering) or an instruct model for raw likelihood scoring.
  • Fine-tuning to inject facts: SFT changes behaviour, not knowledge; use retrieval.
  • Ignoring the chat template: wrong special tokens in fine-tuning or prompting silently degrade quality.
  • Assuming aligned means safe: preference tuning is shallow, and jailbreaks route around it.
Interview questions 2

Answer out loud first, then reveal.

What's the difference between a base model and an instruct model, and when would you use each?Show answer

A base model is pure pretraining: it predicts plausible continuations, so it holds the knowledge but doesn't follow instructions or know when to stop. An instruct model adds SFT and preference tuning, plus RLVR for reasoning models, so it follows instructions in a chat format. I use instruct models for almost every product. I'd start from a base model for my own heavy fine-tune or continued pretraining, or for scoring tasks like perplexity, where chat tuning distorts the probabilities.

Your team wants to fine-tune a model so it knows the internal product docs. What do you tell them?Show answer

That fine-tuning mostly changes behaviour, not knowledge. Knowledge comes from pretraining; a few thousand SFT examples teach format and style, and training on facts the model didn't know tends to increase hallucinations rather than give reliable recall. The docs also change weekly. So I'd use retrieval for the knowledge, and SFT or DPO only if we need a consistent format, tone or tool-calling behaviour that prompting can't reach. Continued pretraining is the heavy option, for a large, stable domain corpus.