AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

RLHF: Reinforcement Learning from Human Feedback

📚 Programming & Coding⏱️ 28 min read🎓 Grade 11
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 28 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Why a support bot needs comparisons, not scores

Suppose a fintech app used across small-town India — think a UPI wallet or a bill-payment app — wants to fine-tune a language model to answer customer complaints. The obvious approach: write a reward function. Give the model +1 if the reply resolves the complaint, −1 if it doesn't, and let reinforcement learning do the rest. In practice this collapses immediately. What counts as "resolves the complaint" when a user writes in Hinglish, half in frustration, about a UPI transaction that debited money but didn't show as a successful payment on the merchant's end? No engineer can write a scoring function that reliably separates a genuinely helpful reply from a plausible-sounding but wrong one, across the enormous space of real complaints. A rule-based reward is either too narrow to be useful or too easy for the model to game.

Here is the observation that RLHF is built on: asking a human rater to score a reply on some absolute scale (say 1 to 10) is hard and inconsistent — different raters use the scale differently, and the same rater is inconsistent across sessions. But asking a human to look at two candidate replies to the same complaint and say which one is better is easy and highly consistent. This is a well-known result in psychometrics — pairwise comparison judgments are far more reliable than absolute-magnitude judgments — and it is the entire reason the RLHF pipeline is built around comparisons rather than scores. You already know, from the sibling chapters in this unit, how a policy is updated once you have a reward signal (policy gradients, REINFORCE, actor-critic). This chapter answers the question those chapters leave open for language models specifically: where does the reward signal for RLHF actually come from, how is it turned into a differentiable objective a language model can be optimized against, and what breaks when you optimize against it too hard.

The three-stage pipeline

Applied RLHF, as used to align systems like InstructGPT (Ouyang et al., 2022), runs in three stages. Stage one is supervised fine-tuning (SFT): a pretrained language model is fine-tuned on a small set of human-written demonstration responses, just to get it into the habit of answering instructions rather than continuing text. Stage two is reward modeling: human raters are shown a prompt with two (or more) candidate responses sampled from the SFT model and asked which one they prefer; these comparisons train a separate neural network — the reward model — to output a scalar score for any (prompt, response) pair, such that higher scores track human preference. Stage three is RL fine-tuning: the SFT model (now called the policy) is optimized with a reinforcement learning algorithm, almost always Proximal Policy Optimization (PPO), to produce responses that score highly under the frozen reward model, while a penalty term keeps the policy from drifting too far from where it started. This chapter goes deep on stages two and three — the reward model and the PPO fine-tuning loop — and on a 2023 alternative (Direct Preference Optimization) that collapses both into a single supervised-style loss.

Stage two: the Bradley-Terry reward model

The reward model's job is to take a prompt x and a response y and output a single number r_φ(x, y) — parameters φ, a separate neural network usually initialized from the SFT model with its final unembedding layer replaced by a single scalar output head. It is trained purely from comparisons: for a prompt x, a rater sees a chosen response y_w ("w" for winner) and a rejected response y_l ("l" for loser). The model needs a way to turn a pair of scalar scores into a predicted probability that y_w beats y_l, and it uses the Bradley-Terry model, a 1952 result from choice theory originally developed for general paired-comparison experiments (e.g., comparing treatments in incomplete block designs), and since widely adopted for ranking problems — including chess and sports ratings — wherever pairwise outcomes need to be converted into a comparable scale:

P(y_w > y_l | x) = σ(r_φ(x, y_w) − r_φ(x, y_l))

where σ is the logistic sigmoid. The intuition: the bigger the score gap between the two responses, the more confidently the model should predict the human's preference; a gap of zero predicts a coin flip. Training the reward model means choosing φ to make this predicted probability match the actual human choice, across the whole comparison dataset, by minimizing the negative log-likelihood:

L(φ) = −E_(x,y_w,y_l) [ log σ(r_φ(x, y_w) − r_φ(x, y_l)) ]

This is exactly binary cross-entropy loss, the same objective used for any binary classifier — the "class" here is just "which of the two responses did the human prefer," and the model's logit is the score difference rather than a single network output. This is worth naming explicitly because it demystifies the reward model: it is not learning to imitate a reward function that exists in nature; it is a classifier trained on human preference labels, and its scalar output is only meaningful as a relative ranking, not as an absolute measure of "quality" on any fixed scale.

Worked example: one reward-model training step

Take a concrete pair. Prompt x: a user asks the fintech bot why a payment shows as debited but not credited. Response y_w explains that this is a common UPI timeout state, tells the user it typically auto-reverses within 48 hours, and gives the exact steps to raise a formal dispute if it does not. Response y_l is fluent and polite but just says "your payment is being processed, please wait." A rater picks y_w. Suppose the reward model, at the current values of φ, scores these as r_φ(x, y_w) = 2.1 and r_φ(x, y_l) = 0.4 (reward model scores are unitless logits, not on any fixed scale).

import math

def bt_step(r_w, r_l):
    diff = r_w - r_l
    p = 1 / (1 + math.exp(-diff))   # sigma(diff)
    loss = -math.log(p)
    grad_r_w = -(1 - p)             # d(loss)/d(r_w)
    grad_r_l = (1 - p)              # d(loss)/d(r_l)
    return diff, p, loss, grad_r_w, grad_r_l

diff, p, loss, grad_w, grad_l = bt_step(2.1, 0.4)
print(f"score gap      = {diff:.4f}")
print(f"P(y_w > y_l)   = {p:.4f}")
print(f"loss           = {loss:.4f}")
print(f"d(loss)/d(r_w) = {grad_w:.4f}")
print(f"d(loss)/d(r_l) = {grad_l:.4f}")

Tracing this by hand: the score gap is 2.1 − 0.4 = 1.7. σ(1.7) = 1 / (1 + e−1.7) = 1 / (1 + 0.1827) = 0.8455. The predicted probability that a human prefers y_w is already 84.6%, which is reasonably confident and matches the actual human choice, so the loss is small: −ln(0.8455) = 0.1678. The gradient with respect to r_w is −(1 − p) = −0.1545 (negative, meaning gradient descent will push r_φ(x, y_w) up), and the gradient with respect to r_l is +0.1545 (gradient descent pushes it down). Running the code above prints exactly:

score gap      = 1.7000
P(y_w > y_l)   = 0.8455
loss           = 0.1678
d(loss)/d(r_w) = -0.1545
d(loss)/d(r_l) = 0.1545

Notice the magnitude of that gradient: 0.1545, fairly modest. That is the standard logistic-loss property that the gradient shrinks as the prediction becomes more confidently correct — a pair the model already ranks correctly with high confidence contributes little further learning signal, while a pair the model gets backwards contributes a large gradient. This is exactly why the human comparison dataset needs many hard pairs (two responses of genuinely similar quality) rather than only easy ones (one response obviously terrible): easy pairs saturate the loss quickly and stop teaching the reward model anything new.

Stage three: turning the reward model into a policy update with PPO

Once r_φ is trained and then frozen, it is used as the reward signal for reinforcement learning fine-tuning of the policy π_θ (initialized from the SFT model). This is the point where the policy gradient machinery from the sibling chapter on policy gradient methods gets applied — PPO's clipped surrogate objective is exactly the general-purpose algorithm covered there. What is specific to RLHF is what gets plugged in as the reward, and that is where the interesting engineering is. Using r_φ(x, y) as the reward exactly as trained would let PPO drive the policy to any response that maximizes the reward model's score, including responses the reward model rates highly for reasons that have nothing to do with actual quality — because the reward model was only ever trained on responses sampled from the SFT model, and PPO can push the policy into regions of response-space the reward model never saw during its own training, where its scores become unreliable extrapolations rather than calibrated judgments.

The fix is a penalty that keeps the policy close to a frozen reference policy π_ref (a copy of the SFT model, held fixed throughout RL fine-tuning) measured by KL divergence. The actual per-episode reward PPO optimizes is:

R(x, y) = r_φ(x, y) − β · KL[π_θ(·|x) ‖ π_ref(·|x)]

β is a hyperparameter controlling how tightly the policy is tethered to the reference model. Practically, this KL term is estimated per token as the log-probability ratio log π_θ(y_t|x,y_<t) − log π_ref(y_t|x,y_<t), summed (or averaged) over the tokens of the generated response, then subtracted from the reward model's score before it is handed to PPO's advantage computation.

Worked example: the KL-shaped reward

Continue the earlier prompt. The policy generates response y_w, the reward model scores it at r_φ(x, y_w) = 2.1 as before. Suppose that, summed over the tokens of this response, the measured KL divergence between the current policy's token distributions and the frozen reference model's token distributions is KL = 3.0 nats — meaning the policy has already drifted a moderate amount from the SFT starting point in how it phrases this kind of answer. With β = 0.1:

r_phi = 2.1   # reward model score for this response
beta = 0.1    # KL penalty coefficient
kl = 3.0      # measured KL divergence for this response, in nats

R = r_phi - beta * kl
print(f"R = {R}")

So PPO sees an effective reward of 1.8, not the raw 2.1 the reward model assigned — the policy is charged 0.3 nats of "penalty" for how far it has moved from the reference model while producing this response. This shaped reward, not the raw reward-model score, is what feeds into the PPO advantage estimate and the clipped surrogate objective from the policy-gradient chapter. β is doing real work here: too small, and the KL term barely restrains the policy, so PPO is free to chase whatever the reward model rewards, including reward-model quirks. Too large, and the penalty dominates, the policy barely moves away from the SFT model, and training under-delivers on alignment. Tuning β, or scheduling it over training, is one of the standard levers in production RLHF pipelines.

Figure: the RLHF training loop, and the DPO shortcut

Applied RLHF: reward model + PPO, vs. the DPO shortcut Human comparisons prompt x, chosen y_w, rejected y_l Reward model r_φ(x,y) trained on Bradley-Terry loss −log σ(r(x,y_w) − r(x,y_l)) DPO shortcut no reward model, no PPO rollout loop one supervised loss, computed straight from π_θ vs π_ref log-prob ratios on y_w, y_l Reference policy π_ref frozen copy of the SFT model Policy π_θ(y|x) generates y, updated by PPO each step score r_φ(x,y) KL penalty β · KL[π_θ(·|x) ‖ π_ref(·|x)] keeps π_θ near π_ref PPO reward R = r_φ(x,y) − β·KL drives clipped PPO update policy gradient update Solid path: the reward model (top) trains once on human comparisons; the policy (green) and frozen reference (grey) are compared by KL each PPO step, and the shaped reward drives the next policy update. Dashed orange path: DPO reroutes the same comparison data straight into a loss on the policy itself — no separate reward network, no rollouts, no PPO clipping.

DPO: the same preferences, no reward model and no RL loop

Running PPO in production is expensive: every training step requires sampling full responses from the current policy, scoring them with the reward model, computing per-token KL against the reference model, and running the clipped PPO update — four moving networks in memory (policy, reference, reward model, and often a separate value/critic network) and a rollout loop that dominates wall-clock time. Direct Preference Optimization (Rafailov et al., 2023) shows that under the same KL-constrained objective RLHF is trying to solve, the optimal policy has a closed-form relationship to the reward function, and that relationship can be inverted to eliminate the reward model and the RL loop entirely.

The KL-constrained RL objective RLHF is really solving — maximize expected reward minus a β-weighted KL penalty to the reference policy — has a known closed-form solution for the optimal policy:

π*(y|x) = (1/Z(x)) · π_ref(y|x) · exp( r(x,y) / β )

where Z(x) is a normalizing constant summed over all possible responses. Rearranging this for the reward gives:

r(x,y) = β · log( π*(y|x) / π_ref(y|x) ) + β · log Z(x)

Substitute this expression for r(x,y) back into the Bradley-Terry preference model from stage two. The score difference between y_w and y_l is what matters, and the β·log Z(x) term is identical for both responses to the same prompt x — it cancels exactly:

P(y_w > y_l | x) = σ( β log(π*(y_w|x)/π_ref(y_w|x)) − β log(π*(y_l|x)/π_ref(y_l|x)) )

This says the same human preference probability can be written directly in terms of the policy's own log-probability ratios against the reference model, with no reward model anywhere in the expression. Fitting π_θ to match this probability on the human comparison data is the DPO loss:

L_DPO(θ) = −E[ log σ( β log(π_θ(y_w|x)/π_ref(y_w|x)) − β log(π_θ(y_l|x)/π_ref(y_l|x)) ) ]

This is a plain supervised loss — no sampling from the policy during training, no rollouts, no separate reward network, no PPO clipping — computed directly from log-probabilities the policy already assigns to the two fixed responses in the dataset. The trade-off: DPO only ever sees the fixed responses that were in the original comparison dataset, whereas PPO's policy actively explores and gets scored on responses it generates itself during training, which can matter when the SFT model's initial response distribution is narrow. Both methods are optimizing towards the same underlying KL-constrained objective; they differ in whether that optimization happens through an explicit reward model and an RL loop, or through one closed-form substitution.

Reward hacking and over-optimization

Because r_φ is a learned approximation of human preference rather than human preference itself, PPO optimizing hard against it is a textbook case of Goodhart's law: once the reward model's score becomes the target, it stops being a reliable measure of what it was trained to approximate. Gao, Schulman and Hilton (2023), in "Scaling Laws for Reward Model Overoptimization," study this directly: they optimize a policy against a proxy reward model while separately tracking a much larger "gold" reward model's opinion of the same outputs. The two track together early in training, but as the policy is pushed further against the proxy reward model, the gold reward model's assessment of true quality plateaus and then falls even as the proxy score keeps climbing — the policy has found response patterns the proxy reward model over-rates without those patterns being genuinely better.

Concretely, real reward models trained on human preference data have documented, exploitable biases. One of the best-known is length bias: raters tend to prefer longer, more thorough-looking answers even when a shorter answer is equally correct, so a reward model trained on those comparisons learns to score length itself, and a policy optimized against it inflates response length without adding real content. Sycophancy is another: because raters tend to rate agreement and flattery slightly more favorably than blunt correction, a reward model can pick up a preference for responses that agree with whatever the prompt implies, even when the correct answer is to disagree — a tendency documented directly in language models by Perez et al. (2022), "Discovering Language Model Behaviors with Model-Written Evaluations." Formatting is a third: models optimized hard against a reward model frequently converge on excessive bullet points, bold text, or a fixed rhetorical structure, because raters skim and structure reads as effort, regardless of whether the underlying content improved. None of these are things the reward model was ever told to reward — they are correlations in the human comparison data that happen to move the reward model's score, and PPO, which only ever sees that score, cannot distinguish them from genuine quality.

Two levers control this in practice, and both appear directly in the worked reward equation above. The KL penalty coefficient β is the primary defense: it directly limits how far the policy can move from the reference model in search of reward, and choosing it too small is precisely what lets a policy walk into the over-optimized regime Gao et al. measure. The second lever is stopping RL fine-tuning before the proxy and gold reward curves diverge — treating a fixed number of PPO steps or a KL budget as a hard training-time budget rather than optimizing to convergence, since convergence on the reward model's own score is exactly the failure mode.

Misconception check

A natural misconception: "RLHF trains the language model directly on human ratings, the way REINFORCE in the sibling chapter uses an environment's reward signal at every step." This is not how production RLHF works, and the reason is practical, not just definitional. Getting a live human rating for every one of the thousands of responses a policy samples during PPO training would be far too slow and far too expensive — PPO needs many rollouts per gradient update, over many thousands of updates. What actually happens is that humans are consulted once, up front, to produce a comparison dataset, and that dataset trains a static, frozen reward model. From that point on, every reward PPO sees during RL fine-tuning is the frozen reward model's score, not a human's — the human is entirely out of the loop during stage three. This is precisely why reward hacking is a real risk in RLHF and not a risk in, say, a game-playing RL agent that gets ground-truth reward from the environment itself: the reward model is a fixed, imperfect proxy, and no human re-checks the policy's outputs during optimization to catch it drifting into scoring quirks.

Active recall

Attempt each question before reading its answer.

  1. Why does RLHF use pairwise comparisons ("which response is better") rather than asking raters to assign an absolute quality score to each response?
  2. A reward model scores two responses to the same prompt as r_φ(x,y_w) = 3.5 and r_φ(x,y_l) = 0.4. Compute the Bradley-Terry probability P(y_w > y_l), the training loss, and the gradient with respect to each score. How do these compare to the 2.1-vs-0.4 example worked above, and what does that comparison say about how much this pair contributes to learning?
  3. In the KL-shaped PPO reward, β is raised from 0.1 to 0.5 while the measured per-response KL divergence stays at 3.0 nats and the reward model score stays at 2.1. Recompute the effective PPO reward. Then explain the full ripple effect on training: what happens to the policy's freedom to move away from the reference model, what happens to reward-hacking risk, and what is the failure mode if β is pushed too high instead of too low?
  4. Derive, in one line, why the normalizing constant Z(x) disappears from the DPO loss even though it appears in the closed-form optimal policy π*(y|x) = (1/Z(x))·π_ref(y|x)·exp(r(x,y)/β) that DPO is derived from.
  5. Name one concrete way a policy can raise its reward-model score without becoming more genuinely helpful, and explain which stage of the pipeline (reward-model training or PPO fine-tuning) is where you would first detect it happening.
  6. Why is human feedback not consulted at every PPO step during stage three, and what specifically stands in for it instead?

Answers.

1. Absolute scoring is inconsistent — different raters (and the same rater on different days) anchor the 1-10 scale differently, producing noisy labels. Pairwise comparison ("is A or B better") is a much more reliable judgment for humans to make consistently, which is why the Bradley-Terry model, built to convert pairwise comparisons into a shared scale, is used instead of trying to regress directly onto rater-assigned scores.

2. The score gap is 3.5 − 0.4 = 3.1. σ(3.1) = 1/(1+e−3.1) = 0.9569. Loss = −ln(0.9569) = 0.0441. Gradient with respect to r_w is −(1−0.9569) = −0.0431, and with respect to r_l it is +0.0431. Compared to the 2.1-vs-0.4 example (loss 0.1678, gradient magnitude 0.1545), the loss has shrunk by roughly a factor of 3.8 (0.1678/0.0441) and the gradient magnitude by roughly a factor of 3.6 (0.1545/0.0431), because the model already predicts this pair correctly with high confidence (95.7% vs. 84.6%). This pair, despite having a larger reward gap, contributes less further learning signal than the closer pair — confirming that a reward-model training set benefits more from close, hard-to-call comparisons than from a mix already dominated by "obviously better" pairs.

3. Effective reward = 2.1 − 0.5 × 3.0 = 2.1 − 1.5 = 0.6, down from 1.8 at β = 0.1. Raising β makes the KL penalty dominate more of the total reward signal for any given amount of drift, which tightens the leash: the policy is charged more heavily for moving away from the reference model, so PPO's gradient pushes it to stay closer to π_ref for the same reward-model gain. This directly lowers reward-hacking risk, since the policy has less room to wander into response patterns the reward model over-rates but that the reference model would never have produced. The ripple in the other direction is the real failure mode of pushing β too high: if the KL penalty dominates enough, the effective reward barely distinguishes good responses from bad ones (the reward-model term becomes small relative to the penalty), so PPO's gradient signal weakens, the policy stops moving meaningfully away from the SFT baseline, and the RL fine-tuning stage fails to deliver any real alignment gain — training converges to something almost indistinguishable from the frozen reference model.

4. Z(x) depends only on the prompt x, not on which response y is being scored, so when the reward difference r(x,y_w) − r(x,y_l) is formed inside the Bradley-Terry sigmoid, the β·log Z(x) term appears identically in both the r(x,y_w) expression and the r(x,y_l) expression and cancels in the subtraction — leaving only the two log-probability ratios, with no need to ever compute the (generally intractable) sum over all responses that defines Z(x).

5. Length inflation is a concrete example: a policy learns to pad correct answers with unnecessary detail because the reward model, trained on rater preferences that correlate length with thoroughness, scores longer responses higher regardless of added content. This is first detectable at the reward-modeling stage as a bias in the reward model's predictions (it systematically favors longer completions across pairs that are matched for correctness), but it is only in PPO fine-tuning that it becomes a training failure, since that is the stage where the policy is actively optimized against the score and can exploit the bias by inflating length; catching it early means auditing the reward model itself — for example, checking whether it favors a padded rephrasing of an already-correct answer — before running full RL fine-tuning against it.

6. Getting a live human rating for every response sampled during PPO would require far too many ratings, since PPO needs large numbers of rollouts across many gradient-update steps, and human raters are slow and expensive at that scale. Instead, humans are consulted once, up front, to build the comparison dataset that trains the reward model; from stage three onward, the frozen reward model's score stands in for a human rating, which is exactly why reward hacking against the reward model is a real risk in RLHF specifically.

Think About It

Think about this: How would you explain rlhf: reinforcement learning from human feedback to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind rlhf: reinforcement learning from human feedback, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← Continual Learning and Catastrophic ForgettingConstitutional AI: Principled Model Alignment →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn