RLHF and Language Models
A language model trained to predict the next token is fluent but not helpful, honest, or harmless — the objective it was optimized for is not the objective we want. RLHF closes that gap by turning the one thing humans do reliably, comparing two outputs, into a reward.
╌╌╌╌
A large language model is trained on one objective: given the text so far, put high probability on the token that actually came next in the corpus. Trained at scale, the model absorbs the syntax, facts, and styles of the internet, but it is not an assistant. Ask it a question and it is as likely to continue with a plausible list of other questions, because on the web a question is often followed by more questions. The model is doing exactly what it was trained to do — imitate the distribution of text — and that is not the same as being helpful, honest, and harmless.1
This is a mismatch between the objective that was optimized (next-token likelihood) and the objective we actually want (a useful assistant). No amount of extra pretraining fixes it, because the loss cannot see the difference: a helpful answer and an unhelpful-but-fluent continuation can be equally probable under the data. Fixing it needs a new signal, one that scores behavior rather than likelihood. The trouble is that the behavior we want is almost impossible to write down as a formula. Reinforcement learning from human feedback (RLHF) constructs that signal from human judgment, and it is the reason the trial-and-error idea at the root of this course now aligns the largest models ever built.
The alignment problem: hard to specify, easy to compare
Suppose you wanted to write a reward function for a good answer.
You would have
to encode helpfulness, factual accuracy, appropriate refusals, tone, format,
and the absence of a thousand failure modes, all as a scalar. Nobody can do this.
Any explicit rule you write is either too coarse to capture what you mean or so
specific that a capable optimizer finds and exploits the gap between your rule
and your intent. Specifying the reward is the whole difficulty of
the problem.
Yet there is one thing humans do easily and reliably: shown two answers to the same prompt, a person can usually say which is better. They cannot tell you the number a good answer deserves, but they can rank a pair. That asymmetry — absolute reward hard to state, relative preference easy to state — is what RLHF exploits: it never asks a human for a score, only for comparisons, and it builds the reward from them.
The insight that a reward can be learned from preferences rather than hand-specified predates language models. Christiano et al. showed it on control tasks in 2017: a simulated robot learned a backflip — a behavior no one had written a reward for — from a few hundred human comparisons between short video clips of its behavior.2 RLHF for language models is that idea carried to a domain where the space of behaviors is the space of all text.
The RLHF pipeline
InstructGPT1 assembled the components into the three-stage pipeline that is now standard. Each stage produces the input the next one needs.
Stage 1 — supervised fine-tuning
Start from a pretrained base model and fine-tune it on a modest set of
demonstrations: prompts paired with high-quality answers written by humans. This
is ordinary supervised learning — maximize the likelihood of the demonstrated
answer given the prompt — and its job is narrow. It moves the model out of the
continue the document
regime and into the answer the instruction
regime, so
that its outputs are at least in the right format and roughly on task. The result
is the SFT model, written . It is a competent
instruction-follower but not yet tuned to human preference, and it is the
starting point (and, shortly, the anchor) for everything that follows.
Stage 2 — the reward model
Now build the reward from comparisons. Sample a prompt , draw two (or more)
candidate answers from the SFT model, and ask a human which they prefer. This
produces a dataset of triples where (the winner
) was
preferred over (the loser
). From this we fit a reward model — a network, usually the SFT model with its output head replaced
by a scalar — that scores how good an answer is for prompt .
The link between a scalar reward and a pairwise preference is the Bradley-Terry model. It posits that the probability a human prefers to is a logistic function of the reward difference:
where is the logistic sigmoid. The larger the reward gap in the winner's favor, the more confidently the model predicts the observed preference. Fitting is then maximum likelihood under this model: minimize the negative log-likelihood of the human choices,
This is a binary classification loss over pairs. Note what it determines and what it leaves free: only reward differences are constrained, so is identified only up to an additive constant — a fact DPO exploits later. The reward model distills thousands of individual human judgments into a single differentiable function that can score any answer the policy might produce, including ones no human ever saw.
Worked example: what the reward gap means in probability
The sigmoid link makes the reward difference interpretable directly as a confidence. Suppose the reward model, after training, scores a winning answer at and a losing one at . The gap is , so the model predicts a human prefers with probability . A gap of gives ; a gap of gives ; a gap of gives exactly , indifference. The per-example loss on a pair the model gets right with confidence is nats; on a pair it gets wrong (predicts with the same reward gap reversed) the loss is nats, nearly three times larger. The loss penalizes confident mistakes far more than unconfident ones, the behavior a calibrated preference model needs.
Stage 3 — policy optimization against the reward
With a reward in hand, the alignment problem finally becomes a reinforcement
learning problem. The environment is a bandit-like one: the state
is the prompt
, the action
is the full generated answer ,
and the reward is delivered once the answer is complete. We want
the policy that produces high-reward answers, which is a
policy-optimization objective — precisely the setting the
policy-gradient methods
of the approximation module were built for.
But maximizing alone fails. The reward model is an imperfect approximation of human preference, accurate near the answers it was trained on and unreliable far from them. A policy free to move anywhere will find the answers where is mistakenly high and collapse onto them. The fix is a leash: keep from drifting too far from the reference model (the SFT model) that generated the training answers, by penalizing the KL divergence between them. The RLHF objective is
where controls how tight the leash is. In practice the KL term is folded into the per-token reward and the whole thing is optimized with PPO — the clipped surrogate objective from the deep-RL module is the optimizer that does the actual work here, so RLHF is PPO pointed at a learned reward with a KL regularizer rather than a new algorithm. The reward each token sees is the terminal reward minus a running KL penalty,
so every token that makes the answer more probable under than under is charged for the deviation.
Worked example: the KL charge on a single answer
Take a short answer of five tokens and suppose the policy has, over training, pushed the log-probability of each token above the reference by the amounts nats — the third token is where the policy has moved most aggressively. The summed log-ratio is nats. With a leash of , the KL charge subtracted from the terminal reward is . If the reward model scored the answer at , the effective reward the PPO update actually optimizes is . Loosen the leash to and the charge falls to , leaving — the policy keeps almost all of its reward and is barely penalized for drifting. Tighten it to and the charge is , leaving ; now the gain from the reward model is nearly cancelled by the cost of having moved. The single scalar determines how heavily distance from the reference counts against the reward, token by token.
- 1initialize policy from the SFT model
- 2freeze a reference copy
- 3repeat
- 4sample a batch of prompts
- 5for each prompt do
- 6sample an answer
- 7score with the reward model
- 8KL term
- 9KL-penalized reward
- 10end for
- 11advantage estimates from (GAE with a value head)
- 12update by a PPO step on the clipped surrogate with advantages
- 13until reward stops improving or KL exceeds budget
- 14return
The output is the aligned policy — the model a user actually talks to. Every consumer chat assistant in wide use went through some version of this loop.
Reward hacking and why the KL leash matters
Drop the KL penalty () and the policy maximizes to the exclusion of everything else — and because is only a proxy for human preference, the maximum of the proxy is not the maximum of the truth. The policy discovers the reward model's blind spots: it learns that padding answers with hedged, agreeable filler scores well, or that a certain phrasing the reward model over-rewards can be repeated, and it drifts into degenerate text that the reward model scores highly and a human finds useless or bizarre. This is reward hacking (equivalently, over-optimization): optimizing the measure until it stops tracking the thing it was meant to measure.
The KL penalty is the defense. It keeps inside the neighborhood of where the reward model saw training data and is therefore trustworthy, trading a little reward for staying on reliable ground. The tradeoff is real and has a characteristic shape: as you loosen the leash (raise the KL budget), true quality first rises — the policy is genuinely improving — then peaks and falls as over-optimization sets in, even though the measured reward keeps climbing.3
Choosing (or an explicit KL target) is therefore central: it stops the optimizer from exploiting the reward model outside the region where it is accurate.
Direct Preference Optimization
The three-stage pipeline works, but it is heavy: a separate reward model to train and store, an on-policy RL loop with its own instabilities, sampling from the policy at every step, a value head, reward and KL bookkeeping. Direct Preference Optimization (DPO)4 asks whether the reward model is necessary at all, and shows by a change of variables that it is not.
The idea in one line: instead of learning a reward and then optimizing a policy against it, notice that the optimal policy is a stand-in for the reward, so you can train the policy on the preferences directly and skip the middle step. The two equations below make that precise — the first writes the best policy in terms of the reward, the second inverts it to write the reward in terms of the policy.
The derivation starts from the RLHF objective itself. For a fixed reward , the policy that maximizes reward minus has a known closed form — it is the reference policy reweighted by the exponentiated reward:
where is a normalizing constant. This is not something you can sample from directly — sums over all possible answers — which is why the standard pipeline needs RL. But the relation can be inverted to write the reward in terms of the optimal policy:
Now substitute this expression for into the Bradley-Terry preference loss. Because the loss depends only on the reward difference , the intractable term is identical for both answers to the same prompt and cancels exactly. What remains is a loss written entirely in terms of the policy we want to train and the frozen reference, with no reward model anywhere:
This is a single supervised classification loss on the preference pairs. It raises the policy's log-probability of the winning answer relative to the reference, lowers it for the loser, with playing the same leash role it did before — the reference appears in the loss, so DPO is implicitly KL-regularized by construction. There is no reward model to fit, no sampling from the policy during training, no RL loop. The reward model has not disappeared; it is implicit in the ratio — the reward the change of variables identified.
What the DPO gradient actually does
Differentiating the loss shows how DPO behaves. Write for the implicit reward. The gradient of the DPO loss is
Read the two pieces. The vector part raises the log-probability of the winner and lowers the loser's — ordinary contrastive learning. The scalar weight in front is the point: it is of the reward gap in the wrong direction, so it is large exactly when the implicit reward has the pair backwards (the model currently scores the loser above the winner) and small when the model already ranks them correctly. DPO concentrates its gradient on the pairs it currently ranks wrong and applies little to the ones it ranks right — a built-in hard-example weighting that a naive maximum-likelihood objective on the winners alone would not have. This is why DPO does not simply collapse onto the winning answers: once a pair is ranked correctly with margin, its gradient weight vanishes.
Worked example: one DPO update
Suppose for a pair the current policy assigns log-ratios of to the winner and to the loser — the model has it backwards, preferring the loser. With the implicit rewards are and , so the weight is , near its maximum of : almost the full gradient is applied, pushing the winner's probability up and the loser's down. After several such updates the log-ratios might flip to (winner) and (loser); now the implicit rewards are and , so the weight is — already below the maximum, and it keeps shrinking as the margin grows. The update throttles itself as it succeeds.
DPO is simpler, more stable, and cheaper, and it matches or exceeds PPO-based RLHF on many preference benchmarks, which is why it became a default for open-model alignment. The tradeoffs are worth naming: DPO learns only from the fixed preference dataset (it never generates fresh on-policy samples to be scored, so it cannot discover and correct its own new failure modes the way an online PPO loop can), and it inherits whatever coverage gaps the preference data has. The explicit reward model remains useful when you want to score many candidates, run best-of- sampling, or keep improving with fresh comparisons.
| RLHF (PPO) | DPO | |
|---|---|---|
| Reward model | explicit, trained separately | implicit in |
| Training signal | on-policy samples scored by | fixed offline preference pairs |
| Optimization | RL loop (PPO + KL penalty) | one supervised classification loss |
| KL regularization | explicit penalty term | built into the loss via the reference |
| Failure discovery | can find its own new failures online | limited to the dataset's coverage |
| Cost / stability | heavier, more moving parts | lighter, more stable |
Beyond human labels: RLAIF and verifiable rewards
Two directions push past the bottleneck of human comparisons.
The first replaces the human labeler with a model. In RL from AI feedback
(RLAIF) and Constitutional AI5, the preference labels are
produced by an LLM prompted with a written set of principles (a constitution
)
rather than by people. A model critiques and revises its own answers against the
principles, and generates the preference pairs used to train the reward model or
run DPO. This scales preference collection far beyond what human labeling can
reach and makes the values being optimized for explicit and auditable in the
constitution's text, at the cost of trusting the labeler model's judgment.
The second sidesteps preference modeling entirely when the task has a checkable answer. For math, code, and formal reasoning, correctness is not a matter of taste — a solution is right or wrong, and a verifier (a unit-test suite, a symbolic checker, a numeric comparison) supplies a verifiable reward with no reward model and no human in the loop. Running policy optimization against such a reward is RL with verifiable rewards (RLVR), and it is what underlies recent reasoning-focused models: the policy is rewarded for producing chains of thought that lead to verifiably correct answers, and the same PPO-style optimization that RLHF uses drives the improvement. The signal is cleaner and harder to hack, but it applies only where correctness is machine-checkable, which is a small slice of what we ask assistants to do.
| Variant | Where the reward comes from | Best suited to |
|---|---|---|
| RLHF | human pairwise comparisons | open-ended helpfulness, tone, safety |
| RLAIF / Constitutional | model judgments against written principles | scaling preference labels; explicit values |
| RLVR | a verifier checks correctness | math, code, formal reasoning |
Beyond the standard pipeline: critic-free RL, process rewards, and reasoning models
The InstructGPT recipe fixed the shape of RLHF, but the years after it reworked almost every stage. Four developments matter for where the field now sits.
Group-relative policy optimization (GRPO). PPO on a language model carries a value network — a second model as large as the policy — whose only job is to estimate the baseline for advantage computation. GRPO, introduced with the DeepSeekMath work, removes it.6 Instead of learning a value function, GRPO samples a group of answers to the same prompt, scores them all with the reward model, and uses the group's own mean and standard deviation as the baseline: the advantage of answer is . An answer better than its siblings gets a positive advantage, worse than them a negative one, with no separate critic to train or store. The saving is real — half the memory and one fewer network to stabilize — and GRPO is the optimizer behind the DeepSeek-R1 reasoning models.7
Process rewards versus outcome rewards. A verifiable reward on a math problem scores only the final answer — right or wrong — which gives the policy no signal about where a long chain of reasoning went wrong. A process reward model (PRM) scores each intermediate step instead, and Lightman et al. showed that supervising the process rather than only the outcome trained more reliable math solvers, because dense per-step feedback assigns credit far more precisely than a single terminal bit.8 The tradeoff is labeling cost: step-level labels are far more expensive to collect than final-answer checks.
Iterative and online DPO. DPO's main weakness — it never generates fresh samples, so it cannot correct failures outside its fixed dataset — is patched by running it in rounds: train with DPO, sample fresh answers from the improved policy, label the new pairs (by humans, an AI judge, or a verifier), and repeat. Each round's on-policy data closes coverage gaps the previous dataset missed, recovering some of the online-exploration advantage that plain PPO-RLHF has over one-shot DPO while keeping DPO's simplicity.9
Best-of- and rejection sampling. The explicit reward model keeps a use even where DPO wins on training: at inference, sample answers, score them all with , and return the best. This best-of- sampling spends compute to buy quality with no further training, and its offline cousin, rejection-sampling fine-tuning, generates many answers, keeps only the reward model's top picks, and fine-tunes on them — a lightweight alternative to a full RL loop that several production systems use as a first alignment pass before or instead of PPO.
Why this closes the loop
The first lesson of this course
defined reinforcement learning as learning to act from a scalar reward signal
found by trial and error, with no supervisor handing over the right answer. RLHF
is that definition applied to the largest models we build. There is no labeled
correct
response to imitate for an open-ended request; there is only a
comparison signal, distilled into a reward, and a policy improved against it by
the same policy-gradient
and PPO machinery the rest
of the course developed. The deep-learning course covers the same method from the
language-model side in its
RL from human feedback
lesson.
Pretraining gives the model its knowledge; RLHF makes that knowledge usable. The trial-and-error principle that started as a way to teach a simulated agent to walk is now the step that turns a fluent next-token predictor into something a person can actually use, which makes it, for the moment, reinforcement learning's largest real-world application.
Footnotes
- Ouyang, Wu, Jiang, et al. (2022),
Training language models to follow instructions with human feedback
, NeurIPS (the InstructGPT paper) — establishes the three-stage SFT / reward-model / PPO pipeline and shows a 1.3B aligned model preferred over a 175B unaligned one. ↩ ↩2 - Christiano, Leike, Brown, Martic, Legg, Amodei (2017),
Deep Reinforcement Learning from Human Preferences
, NeurIPS — learns a reward from a few hundred human comparisons between trajectory segments, including behaviors (e.g. a backflip) with no hand-written reward. ↩ - Gao, Schulman, Hilton (2023),
Scaling Laws for Reward Model Overoptimization
, ICML — measures how true quality diverges from measured reward as KL from the reference grows, quantifying the over-optimization tradeoff the KL penalty controls. ↩ - Rafailov, Sharma, Mitchell, Manning, Ermon, Finn (2023),
Direct Preference Optimization: Your Language Model is Secretly a Reward Model
, NeurIPS — derives the change of variables that turns the RLHF objective into a single classification loss on preference pairs, eliminating the explicit reward model and RL loop. ↩ - Bai, Kadavath, Kundu, et al. (2022),
Constitutional AI: Harmlessness from AI Feedback
, arXiv:2212.08073 — replaces human harmlessness labels with model self-critique and revision against a written set of principles, generating AI preference labels for training. ↩ - Shao, Wang, Zhu, et al. (2024),
DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models
, arXiv:2402.03300 — introduces Group Relative Policy Optimization (GRPO), which drops PPO's value network and estimates the advantage baseline from the mean reward of a sampled group of answers to the same prompt. ↩ - DeepSeek-AI (2025),
DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning
, arXiv:2501.12948 — trains reasoning models with GRPO against verifiable (rule-based) rewards, showing chain-of-thought reasoning can be elicited by RL with a checkable outcome signal. ↩ - Lightman, Kosaraju, Burda, et al. (2023),
Let's Verify Step by Step
, arXiv:2305.20050 — trains a process reward model that scores each reasoning step and shows step-level (process) supervision outperforms outcome-only supervision on mathematical reasoning, releasing the PRM800K step-label dataset. ↩ - Guo, Zhang, Liu, et al. (2024),
Direct Language Model Alignment from Online AI Feedback
, arXiv:2402.04792 — runs DPO iteratively on freshly sampled, on-policy responses labeled by an AI judge, recovering online-exploration benefits that one-shot offline DPO lacks. See also Xu et al. (2023),Some things are more CRINGE than others
and the iterative-DPO line for the same idea. ↩
╌╌ END ╌╌