Skip to content

From GPT to Production

Alignment: RLHF, reward models, and DPO

Teaching a model what humans prefer

Turn a fluent-but-aimless base model into a helpful assistant by teaching it human taste - and keeping it on a short leash.

A base language model has read the internet and can continue any text - but it has no idea it is supposed to be helpful. Ask it a question and it might answer, or it might write three more questions, or drift into an ad. Alignment is the step that turns raw fluency into an assistant. The catch: "a good, helpful, honest answer" is not something you can write down as a loss function. So we do something sneakier - we let humans compare answers, learn a model of what they prefer, and steer the language model toward it. This lesson builds that loop from scratch: preference pairs, a reward model, RLHF with a KL leash, and the closed-form shortcut called DPO.

The one-line idea

A base model is fluent but not yet helpful. We cannot write a formula for "a good answer," but a person can reliably say which of two answers is better. Collect enough of those pairwise votes, distil them into a reward model that scores any answer, then nudge the language model to chase higher reward - while a KL leash stops it wandering so far from the base that it forgets how to write. That loop is alignment.

Preferences, not scores

Ask ten people to rate an answer from 1 to 10 and you get ten incompatible scales - one person's 7 is another's 4. Ask instead which of two answers is better and they mostly agree. Comparisons are cheap, stable, and human-friendly. So the raw material of alignment is the preference pair: one prompt, two candidate responses, and a human who picks a winner. We write the winner as and the loser as . Collect tens of thousands of these pairs and you have a dataset that encodes taste without ever naming a numeric target.

The reward model: a learned taste

A number that says "how good is this answer" is exactly a learned function from Lesson 2: feed it a response, out comes a scalar reward . We fit its parameters by gradient descent so that on every recorded pair the winner outscores the loser. The reward model never sees an absolute "correct score" - it only ever learns from comparisons, using the Bradley-Terry loss in the math box below. Once trained, it can score answers it has never seen, standing in for a human labeler at scale.

PPO and the KL leash

Now improve the policy - the language model itself. Sample answers, score them with the reward model, and push the model to produce higher-reward answers. (PPO, Proximal Policy Optimization, is the reinforcement-learning algorithm that does the pushing; the RL machinery is not the point here - the objective is.) Left unchecked, the policy learns to exploit the reward model: it discovers that padding, flattery, or one favorite phrase reliably scores high even when the answer got worse. That is reward hacking - Goodhart's law, "a measure that becomes a target stops being a good measure." The fix is a leash: penalize drifting away from the frozen base model , measured as KL divergence, with strength . It is the same idea you met as overfitting control - stay close to something already trusted. Play the labeler below and feel the leash pull.

Preference-tuning simulator you are the human labeler

A user asked: "How do I center a div in CSS?" Two candidate replies are sampled from the current model. Pick the one you'd rather ship. Every pick trains the reward model (its taste tilts) and nudges the policy along the style axis - while the KL-leash meter tracks how far you've dragged it from the base model.

Aterse

Use flexbox.

Bhelpful

On the parent, use display:flex with justify-content:center and align-items:center - that centers the child both horizontally and vertically. (Grid with place-items:center works too.)

Make your first pick to start training the reward model.

0 preferences

Reward model - taste learned from your picks

terse
helpful
over-
optimized

Policy on the style axis (beta pulls it toward base)

base
policy
tersehelpfulover-optimized

KL-leash meter

drift = 0.00 - KL = 0.000 - within the leash - safely near the base model.

reward-model score
actual helpfulness (hidden)

Notice the trap the demo makes visible. Keep preferring the longer, friendlier reply and the reward model happily concludes "more is better." With a loose leash the policy chases that signal all the way into gushing, emoji-laden padding - the reward-model bar climbs while the hidden true-helpfulness bar collapses. Tighten and the leash drags the policy back near the base, trading a little reward for staying sane. That balance - enough optimization to become helpful, enough leash to stay grounded - is the whole art of RLHF.

DPO: the reward model in closed form

Training a separate reward model and then running an RL loop is fiddly and unstable - two moving models, reward-hacking to police, hyperparameters everywhere. Direct Preference Optimization (DPO) asks: what if the policy is its own reward model? The RLHF objective has a known closed-form optimum, and inverting it shows the reward is already implicit in the policy as . Substitute that into Bradley-Terry and the reward model cancels out - leaving one plain supervised loss over the preference pairs, with the KL leash baked in through . No reward model, no RL, same math. It is simpler, steadier, and now the default recipe for many open models.

The math: Bradley-Terry, the leash, and DPO's shortcut

Humans give relative judgments, so the reward model is trained with the Bradley-Terry model of pairwise choice: the probability a labeler prefers over is . Maximizing that likelihood is the same as minimizing

a loss you descend by gradient descent. RLHF then optimizes the policy against that reward, but on a leash:

This objective has a known optimum, . Invert it and the reward is already inside the policy, . Put that back into Bradley-Terry and the reward model disappears - you get DPO, one supervised loss on the pairs:

# Reward model: Bradley-Terry loss on one preference pair (y_w beat y_l)
def rm_loss(r, y_w, y_l):
    return -log(sigmoid(r(y_w) - r(y_l)))     # push winner's reward above loser's

# DPO: no reward model, no RL loop. beta is the same KL leash;
# ref is the frozen base model, pi is the policy we are training.
def dpo_loss(pi, ref, y_w, y_l, beta):
    lw = logp(pi, y_w) - logp(ref, y_w)     # how far the policy moved on the winner
    ll = logp(pi, y_l) - logp(ref, y_l)     # ...and on the loser
    return -log(sigmoid(beta * (lw - ll)))     # raise winner, lower loser, leash by beta
Both losses, side by side

Check yourself

In RLHF, what job does the KL penalty do?

What is DPO's central simplification over classic RLHF?

Walk the RLHF pipeline: how do preference pairs become a better assistant, and where does DPO cut a corner?

Nail down the full loop. Try to state it, then check.

Primary source

For the full RLHF pipeline drawn end to end - preference data, reward model, and the PPO loop with its KL term - read Hugging Face's Illustrating Reinforcement Learning from Human Feedback (RLHF). For the closed-form alternative and the derivation that makes the reward model vanish, see the DPO paper, Direct Preference Optimization: Your Language Model is Secretly a Reward Model (Rafailov et al., 2023).

Ask your teacher

Two connections tie this lesson back to fundamentals. The reward model is just a learned function (Lesson 2) - parameters fit by gradient descent on human preference data, no different in machinery from any classifier you have trained. And the KL penalty is a regularizer: exactly the leash-toward-the-trusted idea you saw as overfitting control, here keeping the tuned policy from wandering off the base model's manifold. Ask me why comparisons beat absolute ratings, how PPO actually applies the reward-minus-KL gradient, why trades helpfulness against drift, or when DPO underperforms a well-tuned RLHF loop.

Lock it in

  • Alignment turns raw fluency into a helpful assistant by letting humans compare answers and distilling their taste into a reward model.
  • The KL leash (strength beta) keeps the tuned policy near the base model - loose and it reward-hacks, tight and it barely moves.
  • DPO eliminates the reward model entirely: it inverts the RLHF optimum so the policy trains directly on preference pairs with one supervised loss.
  • Reward hacking is Goodhart's law in action - the reward-model score climbs while actual helpfulness collapses when the leash is too loose.