RLHF: reinforcement learning from human feedback

In one sentence

Humans pick the better of two answers, a reward model learns those preferences, and PPO pushes the model toward high reward while a KL penalty keeps it near the original. It turned GPT-3 into InstructGPT.

Analogy: Training a chef with a food critic: diners rate pairs of dishes, the critic learns their taste, and the chef cooks to please the critic. Watch for the chef who just adds more salt because the critic loves it.

Humans label once to build the reward model; the RL loop then queries the RM thousands of times. The KL term is the leash.

How it works

  • Use it for qualities easy to judge but hard to write: helpfulness, tone, when to refuse.
  • Labelers rank sampled responses instead of writing gold answers; ranking is faster and more consistent.
  • Without the KL penalty, the policy drifts into text that fools the reward model.
  • The policy still exploits the reward model's blind spots: reward hacking (padding, confident tone) and sycophancy.
  • PPO keeps four models busy and its hyperparameters are touchy, so many teams now prefer DPO or RLVR.

Check yourself

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

Mid-PPO, the reward-model score rises at every checkpoint. Fresh human win rate against the SFT model peaked two checkpoints ago, and average answer length is up 40%. Diagnosis and fix?

Go deeper

Full explanation

RLHF tunes a model toward what people prefer, for qualities that are easy to judge but hard to write as one ideal answer: helpfulness, tone, when to refuse. It starts from an Supervised fine-tuning (SFT) model and adds three steps:

  1. Collect comparisons. Sample several responses per prompt; labelers rank them. Ranking is faster and more consistent than writing gold answers.
  2. Train a reward model (RM). Usually the SFT model with a scalar head, trained so the preferred response scores higher (a Bradley–Terry loss on pairs).
  3. Optimize the policy with RL. The model generates, the RM scores, PPO updates the weights. The reward subtracts a KL penalty to a frozen reference (the SFT model): r = RM(x, y) − β·KL(π ‖ π_ref). Without it, the policy drifts into text that fools the RM.

InstructGPT (2022) showed the payoff: labelers preferred outputs of the 1.3B RLHF model over 175B GPT-3.

The costs are real. PPO keeps four models busy (policy, reference, reward, value), hyperparameters are touchy, human labels are expensive, and the policy learns to exploit the RM's blind spots: reward hacking (padding, confident tone) and sycophancy (agreeing with the user). That's why many teams now use DPO & preference optimization for preferences and RL with verifiable rewards (GRPO) where answers can be checked.

Code Reward-model loss and the KL-penalized reward
pythonReward-model loss and the KL-penalized reward
import torch.nn.functional as F

# Step 2: reward model. The preferred answer must score higher (Bradley-Terry).
def reward_model_loss(rm, prompt, chosen, rejected):
    r_chosen = rm(prompt, chosen)        # one scalar per example
    r_rejected = rm(prompt, rejected)
    return -F.logsigmoid(r_chosen - r_rejected).mean()

# Step 3: RL reward. RM score minus a KL penalty to the frozen SFT model.
BETA = 0.05  # too low -> reward hacking; too high -> policy barely moves

def rlhf_reward(rm, policy, ref, prompt, answer):
    score = rm(prompt, answer)
    logp = policy.token_logprobs(prompt, answer)   # helper: per-token log-probs
    logp_ref = ref.token_logprobs(prompt, answer)  # ref = frozen copy of SFT model
    kl = (logp - logp_ref).sum(-1)                 # sample estimate of KL
    return score - BETA * kl

# PPO loop (TRL, OpenRLHF, verl): sample answers -> rlhf_reward ->
# clipped policy update, with a value model (critic) for advantages -> repeat
Real example

After SFT, a support assistant is accurate but stiff and refuses too often. The team collects ~20,000 comparisons from trained raters, trains a reward model and runs PPO. Win rate against the SFT model climbs to 68% on held-out prompts, but average answer length grows 40% and raters flag padded openings: the RM had learned that longer looks more helpful. They add length-controlled comparisons, raise β and keep an earlier checkpoint: 63% win rate at normal length.

Trade-offs +4 · −4

+ You gain

  • Optimizes what humans prefer, including qualities with no single gold answer (helpfulness, tone, refusals).
  • Comparisons are cheaper and more consistent to collect than written ideal answers.
  • On-policy: the RM scores whatever new answers the model produces, so it learns beyond the labeled set.
  • Proven at scale: InstructGPT, ChatGPT, Llama 2-Chat.

− You pay

  • Complex and unstable: four models in memory during PPO and many sensitive hyperparameters.
  • Reward hacking: the policy exploits RM blind spots (length, confident tone) instead of getting better.
  • Sycophancy: people reward agreement and flattery, so the model learns to tell them what they want to hear.
  • Expensive human labeling, with rater disagreement baked in as noise.

Use it when

Large-scale assistant training with a big preference-data budget and an RL infrastructure team, where the target is a fuzzy human judgment you can't write as gold answers.

Avoid it when

Most product teams: try SFT, then DPO & preference optimization on the same preference pairs (simpler, stable, no RM). For checkable tasks like math, code or SQL, use verifiable rewards (RL with verifiable rewards (GRPO)).

Numbers to know 5
  • InstructGPT: labelers preferred the 1.3B RLHF model over 175B GPT-3, with 100× fewer parameters.
  • InstructGPT data: ~13k SFT prompts, ~33k reward-model prompts, ~31k PPO prompts, ~40 contract labelers.
  • InstructGPT ranked 4–9 responses per prompt, giving up to 36 pairs per ranking.
  • Llama 2-Chat: over 1M human binary comparisons collected by Meta.
  • PPO-based RLHF holds 4 models: policy, reference, reward and value (critic). DPO needs 2.
Common pitfalls 4
  • Trusting the RM score: it keeps rising while real quality falls (overoptimization). Track fresh human or judge win rate.
  • A KL coefficient that's too low: the policy drifts into verbose, degenerate or reward-hacking text.
  • Not controlling for length: raters and RMs favor longer answers, so the model learns to pad.
  • Keeping a stale RM: as the policy improves it leaves the RM's training distribution. Refresh comparisons on new samples.
Interview questions 3

Answer out loud first, then reveal.

Walk me through RLHF.Show answer

Three stages on top of an SFT model. First, sample several answers per prompt and have humans rank them, because comparing is easier than writing ideal answers. Second, train a reward model on those pairs so the preferred answer scores higher. Third, run PPO: the policy generates, the RM scores, and the reward carries a KL penalty to the frozen SFT model so the policy can't drift into text that games the RM. InstructGPT showed it works: a 1.3B RLHF model beat 175B GPT-3 with labelers. The catch is cost and instability: four models in memory and a reward that can be hacked.

What is reward hacking and how do you catch it?Show answer

The reward model is a proxy trained on limited data, and RL finds whatever raises the proxy: longer answers, confident tone, agreeing with the user. Gao et al. showed true quality rises, then falls as you optimize harder against a fixed RM. So I never trust the RM score alone: I track win rate from fresh human or held-out judge evaluations, plus length and refusal rates. To mitigate: the KL penalty, length-controlled data, early stopping, RM ensembles and refreshing the RM on new policy samples.

Would you use RLHF or DPO for your product?Show answer

Almost always DPO first. It learns from the same preference pairs with no reward model and no RL loop: one stable, supervised-style run with two models in memory instead of four. I'd reach for full RLHF only if I needed on-policy exploration or a reward model reused across many runs, and had an RL infrastructure team. If the task is checkable, like code that must pass tests, I'd use verifiable rewards instead of a learned one, which removes the RM's blind spots.