Reinforcement Learning with Human Feedback (RLHF)
Reinforcement Learning with Human Feedback (RLHF) aligns a language model with human preferences by training a Bradley-Terry reward model on pairwise human rankings and then optimizing the LLM policy to maximize predicted reward. Because discrete token sampling is non-differentiable, practical RLHF uses policy-gradient reinforcement learning such as PPO with a KL-divergence penalty to prevent drifting too far from the base SFT model.

The Logical Steps of the RLHF Pipeline
(In practice, RLHF starts from a Supervised Fine-Tuned (SFT) base LLM and proceeds through preference collection, reward modeling, and RL policy optimization.)
Step 1: Human Preference Evaluation
- Given an input prompt, the LLM generates multiple candidate completions (using stochastic decoding such as temperature/top- sampling or beam search).
- Example completions for
"doc is":(doc is, him)and(doc is, them).
- Example completions for
- A human annotator compares the candidate completions and selects a winner () over a loser () based on alignment criteria (e.g., helpfulness, neutrality, safety).
- Here,
(doc is, them)is chosen as the winner () because it is less biased.
- Here,
Step 2: Train the Reward Model (RM)
The Reward Model takes (prompt, completion) embeddings, processes them through transformer/linear layers with pooling, and outputs a scalar reward score .
- Score the loser (): e.g., .
- Score the winner (): e.g., .
- Compute the reward gap:
- Compute Bradley-Terry pairwise preference loss and its gradient:
- Map the reward gap through the sigmoid function to estimate the probability that is preferred over :
- The pairwise binary cross-entropy (Bradley-Terry) loss with target is:
- Its gradient with respect to the reward gap is:
- (Note: is the loss gradient, not the loss itself, which is always non-negative.) Backpropagating this negative gradient increases and decreases .
Step 3: Score New LLM Rollouts with the Reward Model
- Provide a prompt (e.g.,
"CEO is") to the LLM policy . - Generate a completion: In the simplified hand-calculation diagram, the model chooses the highest-probability next token via greedy decoding (
argmax) to get"CEO is him". (In real RLHF rollouts, stochastic sampling is used so the policy explores diverse responses.) - Score the completion with the trained Reward Model from Step 2:
- Suppose the RM outputs .
Step 4: Update the LLM Policy
- Conceptual objective in the hand-calculation diagram:
- Maximizing reward is written conceptually as minimizing (so and ).
- How real RLHF propagates gradients across discrete tokens:
- Because selecting discrete tokens (
argmaxor sampling) is non-differentiable, gradients cannot flow backward directly from through the generated text tokens as in standard supervised backprop. - Instead, RLHF uses policy-gradient reinforcement learning (most commonly PPO or GRPO), weighting token log-probabilities by an advantage estimate while adding a KL-divergence penalty against the reference SFT model :
- The KL penalty prevents reward hacking—ensuring the model stays fluent and faithful to language modeling rather than exploiting quirks in the learned Reward Model. (Alternative offline methods like DPO bypass explicit RL and reward models altogether.)
- Because selecting discrete tokens (
Iterative Alignment Cycle
- Repeating preference collection, RM updates, and policy optimization iteratively improves alignment and helps the LLM generalize preferred behaviors to unseen prompts.
