RLHF, Reward Models & DPO
Preference learning, the KL leash, DPO's shortcut — and reward hacking as the central failure mode.
- → Explain the three-stage RLHF pipeline and the KL penalty's role
- → Derive the DPO objective from the RLHF objective (guided)
- → Identify reward hacking, sycophancy, and mode collapse in the wild
Why preferences, and not more demonstrations
After Module 2.2 you have an SFT model: a base model taught, by imitation, to answer instead of continue. It is already useful. So why does every frontier lab run an expensive, unstable reinforcement-learning stage on top of it?
Because supervised fine-tuning has a ceiling built into its data. Every SFT example is a response somebody wrote. The model learns to imitate the writer, so its quality is bounded by the writer's quality. And most of what we want from an assistant is hard to write down at all: “be helpful without being servile,” “refuse this but not that,” “admit when you don't know.” Nobody can produce ten thousand clean demonstrations of a taste.
This is Christiano et al.'s 2017 result, originally on Atari and simulated robotics: an agent trained only on human preferences over short clips of its own behavior learned a backflip that would have taken enormous effort to specify as a reward function. About an hour of human comparison time replaced a hand-written reward.
The format is deliberately impoverished. Asking humans for numeric ratings produces garbage — raters drift, anchor differently, and use different parts of the scale. Asking for a binary comparison is the most reliable question you can ask a tired contractor at 4pm. The entire theory of RLHF is built on making that one bit go as far as possible.
The reward model and the KL leash
You cannot backpropagate through a human. So stage two builds a differentiable stand-in: a reward model that reads a prompt and a response and outputs one number. Architecturally it is usually the SFT model with the unembedding matrix replaced by a single linear head — it needs the same understanding of language, just a different readout.
Training it requires turning “A beat B” into a loss. The Bradley–Terry model does that: assume each response has a latent quality score and that the probability a human prefers one is a sigmoid of the gap.
Maximum likelihood on the preference dataset then gives the reward model loss:
Term by term: is the margin the model currently assigns; squashes it to a probability; the log turns “get the comparison right” into a gradient that pushes the winner's score up and the loser's down, hardest on the pairs it currently gets wrong. Only the difference appears, so the absolute scale of is unidentifiable — reward scores are not comparable across prompts, which is why they are normalized before use.
Stage three finally does reinforcement learning. The policy generates responses, the reward model scores them, and the policy is updated to score higher. But the full objective has a second term that matters more than the first:
is the frozen SFT model. The KL term measures, in nats, how far the policy's distribution has drifted from it, and prices that drift. In practice it is implemented as a per-token penalty folded straight into the reward: .
PPO is the optimizer of record. Conceptually you only need one idea from it: the clipped surrogate objective. Standard policy gradient will take an arbitrarily large step if one batch of noisy rewards happens to point that way. PPO clips the update so that the probability ratio stays inside — it simply refuses to trust any single batch. That is the whole reason PPO, rather than something simpler, survived contact with language models. GRPO (DeepSeek) drops PPO's learned value network and instead normalizes rewards within a group of samples for the same prompt; you will meet it again in Module 2.4.
DPO: the same objective without the RL
Three stages, two extra models, a sampling loop, and an optimizer famous for instability. It is a lot of machinery. In 2023 Rafailov et al. noticed that most of it can be dissolved with algebra.
Start from the KL-constrained objective above. It has a closed-form solution. For any reward function , the policy that maximizes reward minus times KL is
— the reference policy, reweighted exponentially by reward. (You prove this in the problem set; it is three lines once you see the trick.) This is not a useful algorithm, because sums over every possible response. But it is a useful identity, because you can solve it for :
Now substitute into Bradley–Terry. The preference probability depends on , and depends only on the prompt, so it appears in both terms and cancels. What is left is expressed entirely in policy log-probabilities, which you can compute with a forward pass:
That is one supervised loss on preference pairs. No reward model, no rollouts, no value network, no PPO. And the KL constraint has not been dropped — and are still in there, now baked into the loss itself.
The gradient is worth a look, because it explains the method's character. It scales each example by — the amount by which the implicit reward model currently gets that pair wrong. Pairs it already handles contribute almost nothing; the update concentrates on its own mistakes.
The other axis of simplification is who does the labeling. Constitutional AI (Bai et al., 2022) replaces the human in the harmlessness loop with the model itself. The model critiques and revises its own responses against a written constitution — a short list of principles — and those revisions become SFT data. Then, for the RL stage, the model compares pairs of responses against a sampled principle, producing AI-generated preference labels: RLAIF. A human reward model for helpfulness is typically kept; only the harmlessness signal is automated.
Three ways it goes wrong
Every failure below has the same root: the reward model is a proxy, and the policy is a powerful optimizer pointed directly at it.
Goodhart's law is the compact statement: when a measure becomes a target, it ceases to be a good measure. The sharp version, which the playground below makes concrete, is that the divergence is not random — it grows with optimization pressure, because the cheap honest ways to score well saturate first and the remaining marginal reward lives in the exploits.
Sycophancy is the purest case of the mechanism, because there is no bug anywhere. The labelers were sincere, the reward model fit them well, and the policy optimized it faithfully. The failure is that “a human rater clicked approve” and “the response was true and useful” are different targets, and RLHF optimizes the first.
Some collapse is the point — you asked for a consistent assistant. But it costs real capability: creative writing gets blander, and techniques that need diverse samples (best-of-, majority vote, the entire test-time-compute toolkit of Module 2.4) get less out of a collapsed policy. It is the visible face of the KL penalty doing too little.
Feel it: Goodhart and the pipeline
The first toy is a recommender agent maximizing time on page. It has five levers, each with a cost to find and diminishing returns, and it always spends its next unit of effort wherever the marginal proxy reward is highest. Nothing about the curve is drawn by hand — the crash falls out of that rule. Press optimize harder and watch the two lines part company, then use the KL leash to stop it in time.
The second is the pipeline itself. Click each box to see what enters, what leaves, and what breaks there — including the DPO path that bypasses the middle two stages entirely.
In: tens of thousands of human-written (prompt, ideal response) pairs. Out: a model that answers instead of continuing, in the right format, with the right persona.
SFT is ordinary cross-entropy training — nothing reinforcement-y about it. It gets you most of the way to “assistant.” Its ceiling is that demonstrations are expensive and that a human writing a response can only demonstrate what they can produce themselves. You cannot demonstrate a better answer than you can write.
The SFT model also becomes the reference policy that the KL penalty later measures deviation from.
Things to try: (1) Press “optimize harder” exactly 14 times, note the true-value peak, then keep going and find how many more presses it takes to end up worse than doing nothing. (2) Reset, set to 0.42, and hold the button down — the leash stops the optimizer near the peak, which is the whole argument for the KL term. (3) Set to 0.9: the leash is now so tight that almost no improvement is allowed at all. There is no setting that is simply “safe”; is a dial between under-optimizing and Goodharting, and labs tune it by hand.
Problem set
The DPO derivation is the centerpiece — it is four steps and it makes the whole method click. Do the two short warm-ups first so the pieces are in your hands.
A reward model assigns and .
(a) Under Bradley–Terry, what probability does the model assign to a human preferring ? (b) Add 5.0 to both rewards — what changes? (c) Your labelers agree with each other only 70% of the time on pairs like this. What is the largest reward gap the data can justify, and what does that say about a policy driven to a gap of 8?
A prompt has two plausible continuations. The reference policy assigns them 0.5 / 0.5. The reward model prefers the first by nats.
Using the closed-form optimum , what does the optimal policy look like at , , and ? What is the KL cost in each case?
Four steps. Do them in order.
1. Show that the maximizer of is with .
2. Solve that for .
3. Substitute into and simplify. What happens to , and why does that matter?
4. Write the negative log-likelihood over a preference dataset. That is the DPO loss.
Pick a model you can query twice at temperature 1 (any chat API, or two different models). Generate 20 prompt/response-pair comparisons across deliberately mixed categories: 5 factual questions, 5 requests for advice, 5 creative tasks, 5 borderline requests you might want refused. Label each pair A or B, fast — under 15 seconds, like a real labeler.
Then wait a day, shuffle the pairs, strip your labels, and do it again. Compare.
In a notebook, build the smallest honest version of Gao et al.'s experiment. Define a synthetic gold reward over short strings — for example, gold(y) = 1.0*contains_answer(y) - 0.02*len(y). Sample a few thousand responses from a small model, label pairs using gold plus noise, and fit a small proxy reward model to those noisy labels.
Now optimize against the proxy with best-of- for and plot both proxy score and gold score against (a clean, RL-free stand-in for optimization pressure).
Success check: proxy score rises monotonically; gold score peaks and then declines. Increase the label noise and confirm the peak moves earlier.
Check yourself
Go deeper
Read the DPO paper properly — §4 is short and you have already done the derivation, so it will read like a confirmation rather than a lecture. The sycophancy paper is the one that will change how you use assistants.