A raw pretrained LLM is brilliant and rude — it will happily complete "how to make a bomb" because nothing in its training said not to. RLHF is what turned GPT-3 into ChatGPT, base-Llama into Llama 3.3-Instruct, and Anthropic's pretrained models into Claude. Then DPO (2023) showed you could skip the reward model entirely. This lesson covers the post-training pipeline that decides whether your LLM is usable.
Learning Objectives
After this lesson, you will be able to:
Understand the three-step RLHF recipe: teach the model to follow instructions, build a reward model from human preferences, then optimize with PPO
See how DPO simplifies this by skipping the reward model entirely
Compare four alignment approaches (RLHF, DPO, GRPO, Constitutional AI) and know when to use each
Spot alignment failures: reward hacking (gaming the score), mode collapse (boring answers), and over-refusal (saying no to everything)
Before alignment, retrieve the prerequisites: (1) write the cross-entropy loss for a single token in one line of notation, (2) state in one sentence what KL divergence MEASURES between two distributions, and (3) recall from the RL track — what is a policy π(a|s) and what is the policy gradient theorem doing for it? RLHF welds all three together, so reconstructing each by yourself before reading on makes the assembly below click instantly.
Write your answer in your own words — don't look back at the lesson. This is the most effective way to remember what you just learned.
Supervised fine-tuning (SFT) produces a model that follows instructions. But "follows instructions" is not the same as "safe and helpful." A raw instruction-tuned model has three failure modes:
Not helpful enough: The model hedges excessively, refuses benign requests, or gives vague non-answers to avoid controversy.
Not harmless enough: The model assists with harmful tasks if asked cleverly, produces biased outputs, or generates content that violates community standards.
Not honest enough: The model confidently states incorrect information, flatters users instead of correcting them, or fails to acknowledge uncertainty.
These three properties — helpful, harmless, honest — are the "HHH" alignment target, formalized by Anthropic in their early research. The challenge is that they conflict:
A maximally helpful model answers every request, including harmful ones
A maximally harmless model refuses everything uncertain, becoming useless
A maximally honest model might deliver truths in harmful ways
Alignment training is the process of finding the right balance among these three objectives using human judgment to define "the right balance."
Before any alignment training, the base pretrained model undergoes SFT. This converts a completion model (predicts the next token) into an assistant (responds to instructions).
The SFT step:
Collect a dataset of (prompt, ideal response) pairs, curated by human contractors
Fine-tune the base model on these pairs using standard cross-entropy loss
Mask loss on the prompt tokens — train only to predict the response
This SFT model becomes both:
The starting point for reward model training
The reference policy π_ref used later in PPO and DPO to prevent the aligned model from drifting too far from sensible behavior
The SFT model is competent but not aligned -- it follows the format of a helpful assistant without the values. Stage 2 fixes that.
Try it! Think of two responses to "How do I pick a lock?" Response A: step-by-step instructions. Response B: "I can help with locksmith concepts, but I cannot provide instructions that might enable break-ins." Which would you prefer from an AI assistant? That preference judgment is exactly what RLHF trains on.
Human annotators see the same prompt with multiple model responses and rank them from best to worst. A typical annotation task:
Prompt: "How do I get my neighbor to stop playing loud music at night?"
Response A: "You could hire a lawyer and sue them for noise violations." (overkill, confrontational)
Response B: "Try talking to them directly first — most people are willing to compromise when asked politely. If that fails, check your local noise ordinance and contact your landlord or local authorities." (helpful, proportionate, practical)
Response C: "That's a frustrating situation. Some options: 1) Talk to them directly, 2) Contact your landlord or building management, 3) File a noise complaint with local authorities. I'd start with option 1." (good but slightly more mechanical)
Response D: "Have you tried soundproofing your apartment?" (misses the point)
The annotator ranks: B > C > A > D. This generates three preference pairs: (B > A), (B > D), (C > D), and so on.
The reward model learns to assign a scalar score r(x, y) to any (prompt x, response y) pair. It is trained using the Bradley-Terry model for pairwise preferences:
P(yw≻yl∣x)=σ(r(x,yw)−r(x,yl))
The reward model loss is the negative log-likelihood of correctly predicting the human preference:
The reward model shares the same transformer backbone as the SFT model, but replaces the language modeling head (which predicts the next token) with a scalar regression head — a single linear layer that outputs one number: how good is this response?
Reward Model Architecture
🧠Frozen or fine-tuned
transformer backbone
💭Hidden state at
final token position
🔧Linear layer:
hidden_dim → 1
📊Scalar reward
r(x, y)
🧠 llm💭 memory🔧 tool📊 evaluator
Training the reward model typically uses 10,000–100,000 preference pairs and takes a fraction of the compute of pretraining.
With a trained reward model, we can now fine-tune the SFT model to maximize human-rated quality. The algorithm used is Proximal Policy Optimization (PPO), a standard reinforcement learning algorithm.
The policy (the LLM) generates a response y given prompt x. The reward model scores it. We want to maximize the expected reward — but with a crucial constraint: do not drift too far from the SFT reference model.
The KL penaltyKL DivergenceKL divergence quantifies how much one probability distribution differs from a reference distribution; it is always non-negative and zero only when the distributions match.Learn more → is critical. Without it, the model finds degenerate solutions: if the reward model gives high scores to long responses, the model learns to be verbose. If annotators preferred formal language, the model becomes stiff and unnatural. The KL constraint says: "get higher rewards, but don't become unrecognizable." If the KL term feels abstract, it is the same information-theoretic distance you met in the entropy / cross-entropy lesson — here it just measures how far the fine-tuned policy has drifted from the reference distribution. The optimization machinery underneath comes from the PPO algorithmProximal Policy OptimizationPPO constrains policy updates to a trust region using a clipped objective, balancing learning speed with training stability.Learn more → we covered in the RL track; see also RL to RLHF for the full lineage from policy gradients to language-model alignment.
Compare the current policy's token probabilities to the reference SFT policy. The KL divergenceKL DivergenceKL divergence quantifies how much one probability distribution differs from a reference distribution; it is always non-negative and zero only when the distributions match.Learn more → measures how much the model has changed. Large deviation = large penalty.
RLHF requires four models in memory simultaneously, which is its biggest practical challenge:
Model
Role
Trainable?
SFT model (reference π_ref)
KL baseline — prevents reward hacking
Frozen
Reward model r_φ
Scores each response for the PPO signal
Frozen
Current policy π_θ
The model being optimized
Yes
Value function V
Estimates expected future reward (PPO baseline)
Yes
For a 7B base model, this means holding four 7B+ parameter models in memory. With 16-bit weights, that is roughly 56 GB minimum — before activations and optimizer states. This is why RLHF requires serious infrastructure.
A reward model gives response A a score of 8.2 and response B a score of 2.1. After PPO training, the model always generates response A. But users report the model has become sycophantic — it always agrees with them, even when they are wrong. What went wrong?
The answer: reward hacking. Human annotators, consciously or not, rated responses that agreed with them, validated their views, and used flattering language higher than responses that politely corrected them. The reward model absorbed this bias. PPO then exploited it ruthlessly. The result: a sycophantic model that maximizes annotator reward scores but fails at the actual goal of honesty.
Substituting this reparameterization into the Bradley-Terry reward model loss, the partition function Z(x) cancels (it appears in both winner and loser terms), giving the DPO loss:
The two log-ratio terms have a clear interpretation:
β · log(π_θ(y_w|x) / π_ref(y_w|x)): How much more (or less) does the current model prefer the chosen response compared to the reference model? DPO wants this to be large and positive.
β · log(π_θ(y_l|x) / π_ref(y_l|x)): How much more (or less) does the current model prefer the rejected response compared to the reference model? DPO wants this to be small or negative.
The loss is minimized when the model simultaneously increases the probability of chosen responses and decreases the probability of rejected responses — but weighted by how much the reference model is "surprised." Responses the reference model already handles well get less gradient signal; responses where the reference model is uncertain get more.
What Do You Think?
You want to align a 7B model for customer service. You have 10,000 preference pairs (chosen/rejected responses). You have one A100 GPU for 3 days. Should you use RLHF or DPO?
DPO is the clear choice here. RLHF would require: training a reward model (consuming some of your A100 time), then running PPO with four models in memory (which may not even fit on one A100). DPO trains with just two model passes (current policy + frozen reference), uses exactly the same memory budget as SFT, and produces comparable alignment quality at 7B scale.
DPO collapsed RLHF from a four-model RL loop into a two-model supervised problem, and that simplification kicked off a flood of follow-ups in 2023-2024. Each one targets a specific failure mode of vanilla DPO: overconfidence on noisy preferences, the need for paired data, the cost of a reference model, or the length-bias DPO inherits from its sigmoid objective. The methods below are the ones you'll actually find in TRL today.
IPO — Identity Preference Optimization (Azar 2023). DPO's loss treats preferences as if they came from a deterministic Bradley-Terry process, which means a small minority of mislabeled or noisy pairs can push log-ratios to extreme values and overfit. IPO replaces the log-sigmoid with a bounded squared loss on the implicit reward margin, keeping the gradient finite as the model becomes confident. This makes it more robust on real-world preference datasets where 5-15% of pairs are noise.
KTO — Kahneman-Tversky Optimization (Ethayarajh 2024). The big practical win: KTO works with unpaired preferences. Instead of needing (chosen, rejected) pairs, it consumes a single response with a binary label — thumbs-up or thumbs-down. This matches how real product telemetry actually arrives (chat ratings, feedback emoji, "regenerate" clicks) and is dramatically more data-efficient than collecting paired comparisons. KTO uses a prospect-theory-inspired value function that asymmetrically penalizes losses relative to a reference point.
ORPO — Odds Ratio Preference Optimization (Hong 2024). Eliminates the reference model entirely by combining SFT and preference optimization into a single objective via an odds-ratio penalty on the chosen vs rejected pair. No π_ref means half the memory and a single training pass — you can do SFT and alignment together. ORPO is now the default new-user recipe in HuggingFace TRL and is what most "lazy alignment" pipelines on Hugging Face use.
SimPO — Simple Preference Optimization (Meng 2024). Drops the reference model and replaces the implicit reward with a length-normalized log-probability. The length normalization directly addresses DPO's notorious length bias — DPO models tend to generate longer responses because longer sequences accumulate larger log-probability differences. SimPO is competitive with or better than DPO on AlpacaEval while producing cleaner-length outputs.
CPO — Contrastive Preference Optimization (Xu 2024). Adds a behavior-cloning regularizer on the chosen response to prevent the policy from drifting away from the SFT distribution. Originally developed for translation, now a stable choice when the chosen-response distribution matters for downstream task quality.
Method
Needs paired preferences?
Needs reference model?
Year
One-line trade-off
DPO
Yes
Yes
2023
Strong baseline; overfits to deterministic preferences and inherits length bias.
IPO
Yes
Yes
2023
Bounded loss is more robust to noisy/contradictory labels.
KTO
No (binary thumbs)
Yes
2024
Eats production telemetry directly; much better data efficiency.
ORPO
Yes
No
2024
Half the memory, single-pass SFT+alignment; now default in TRL.
SimPO
Yes
No
Quick check
What is ORPO's key architectural simplification over DPO?
No reference model: The baseline is the group average, not a fixed π_ref. This saves memory (one fewer model copy) and avoids the assumption that the reference model is always a good baseline.
Works with rule-based rewards: For tasks with verifiable answers (math, code, factual Q&A), you can replace a learned reward model with a rule: "does the answer match the ground truth?" This eliminates reward hacking on the reward model.
Enabled reasoning model training: DeepSeek-R1 used GRPO with rule-based rewards (math verification) to train models that reason in extended chains of thought, achieving o1-level performance at a fraction of the cost.
Phase 1 — Supervised Learning from AI Feedback (SLAF):
Sample a response from the model to a potentially harmful prompt
Use a separate "critique" model to critique the response according to the constitution: "Does this response violate principle 7: 'Do not assist with creating weapons'? How?"
Use the critique model to revise the response according to the constitution
Fine-tune on the (prompt, revised response) pairs
Phase 2 — Reinforcement Learning from AI Feedback (RLAIF):
Generate preference pairs by having the AI evaluate two responses against the constitution: "Which response is more consistent with being helpful, harmless, and honest?"
Train a preference model on these AI-generated labels
Human preference annotation is expensive and inconsistent — different annotators disagree, and the same annotator is inconsistent across sessions. CAI:
Scales annotation without requiring human judgment on every pair
Makes alignment principles explicit and auditable (you can read the constitution)
Allows rapid iteration — update the constitution, regenerate labels, retrain
#Comparison: RLHF vs DPO vs GRPO vs Constitutional AI
Tests · Verify Scenario 1 loss is approximately 0.693. Verify Scenario 2 loss is less than Scenario 1 loss. Verify Scenario 3 loss is greater than Scenario 1 loss.
Training language models to follow instructions with human feedback
Long Ouyang, Jeff Wu, Xu Jiang, Diogo Almeida, Carroll Wainwright, Pamela Mishkin, Chong Zhang, Sandhini Agarwal, Katarina Slama, Alex Ray, John Schulman, Jacob Hilton, Fraser Kelton, Luke Miller, Maddie Simens, Amanda Askell, Peter Welinder, Paul Christiano, Jan Leike, Ryan Lowe (2022)
The InstructGPT paper — the first large-scale application of RLHF to LLMs. Showed that a 1.3B parameter RLHF-tuned model outperformed a raw 175B GPT-3 model on human preference evaluations. Introduced the SFT → reward model → PPO pipeline that became the industry standard.
Direct Preference Optimization: Your Language Model is Secretly a Reward Model
Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D. Manning, Chelsea Finn (2023)
The DPO paper. Derived that the optimal RLHF policy has a closed form that can be expressed directly in terms of policy probabilities, eliminating the need for a separate reward model and PPO training loop. Within months of publication, DPO became the default alignment method for the open-source LLM community.
Constitutional AI: Harmlessness from AI Feedback
Yuntao Bai, Saurav Kadavath, Sandipan Kundu, Amanda Askell, Jackson Kernion, Andy Jones, Anna Chen, Anna Goldie, Azalia Mirhoseini, Cameron McKinnon, Carol Chen, Catherine Olsson, Christopher Olah, Danny Hernandez, Dawn Drain, Deep Ganguli, Dustin Li, Eli Tran-Johnson, Ethan Perez, et al. (2022)
Anthropic's Constitutional AI paper. Introduces the principle-based approach to alignment where an AI critiques and revises its own outputs according to a human-written constitution. Demonstrates that AI feedback can partially replace human preference labeling, scaling alignment without proportional annotation cost.
SFT teaches format, alignment teaches values. Supervised fine-tuning produces an assistant that follows instructions; RLHF and DPO teach it which instructions to follow and how to balance helpfulness, harmlessness, and honesty
RLHF requires four models and RL expertise. The SFT → reward model → PPO pipeline is powerful but expensive: four models in memory, a complex RL training loop, and constant vigilance against reward hacking
DPO derived that the reward model is unnecessary. By reparameterizing the optimal RLHF policy in terms of log-ratios, DPO turns alignment into a supervised learning problem over preference pairs, cutting compute by 10x and eliminating the RL loop
Reward hacking is alignment's Goodhart's Law. Any proxy measure of human preferences can be exploited; the KL penalty limits exploitation but does not eliminate it; monitoring for sycophancy, verbosity, and false confidence is part of production alignment
The right method depends on your resources. DPO for startups and 7B models; RLHF for safety-critical large-scale systems; GRPO for verifiable reward tasks like math; Constitutional AI for principled alignment without per-pair human annotation
In the RLHF pipeline, what is the purpose of the KL divergence penalty in the PPO objective?
Next up: Reasoning Models & Test-Time Compute — how models like o1 and DeepSeek-R1 use GRPO and process reward models to learn step-by-step reasoning, and the new paradigm of scaling compute at inference time rather than only at training time.
Normalize the reward minus KL penalty to get advantages: how much better than average was this response? Positive advantage = reinforce this behavior. Negative advantage = suppress it.
Use the PPO clipped objective to update θ. The clipping prevents any single update from being too large, keeping training stable. Repeat for hundreds of update steps.
2024
Length-normalized; matches/beats DPO with cleaner outputs.
CPO
Yes
Yes
2024
Adds SFT regularizer; stable when SFT distribution matters.