Language Model Alignment: RLHF and Constitutional AI
Module: Natural Language Processing | Difficulty: Advanced
What You'll Learn
- 1Describe the three stage RLHF pipeline and what each stage contributes
- 2Fit a Bradley Terry reward model from pairwise preferences
- 3Balance reward against a KL penalty to a frozen reference policy
- 4Compare human feedback with principled self supervision for safety
A model trained on next token prediction learns what text looks like, not what a person wants to read. Alignment is the process of converting human preferences and written principles into an optimization signal, and this lesson covers the dominant recipe, reinforcement learning from human feedback, its alternative of letting the model critique itself against a constitution, and the failure mode both share when the proxy reward drifts away from the intent it was meant to capture.
The Three Stage Pipeline
Stage one is supervised fine tuning. Annotators write high quality responses to prompts, and the model is trained on them with the same cross-entropy loss used for pretraining:
Stage two trains a reward model. Annotators see two responses to the same prompt and pick the better one, producing pairs where is preferred. The Bradley Terry formulation gives a scalar score per response and turns the score difference into a preference probability:
Fitting the model minimizes the negative log likelihood of the observed comparisons, which is just logistic loss on the score margin.
Stage three optimizes the policy with reinforcement learning while holding it near the starting point:
The first term climbs the reward model, the second punishes divergence from the frozen supervised policy, and sets how much freedom the optimizer gets. The InstructGPT work at 175 billion parameters reports that annotators prefer its outputs over the base model 85 percent of the time, and over the base model with few shot prompting 71 percent of the time, while the same work found that a 1.3 billion parameter aligned model can match a 175 billion parameter aligned model on human preference.
Reward Modeling and Its Failure
The reward model is a stand-in, and optimizers treat stand-ins literally. Gradient ascent on finds responses that maximize the model's score, which includes the phrasing, length, and hedging patterns that annotators happened to prefer during collection. Once those patterns decouple from actual quality, the reward rises while the behavior degrades, which is Goodhart's law applied to a scalar.
Length is the canonical example. Annotators often prefer longer answers, so the reward model learns to associate verbosity with quality, and the policy becomes verbose without becoming more useful. Fixing it requires either normalizing reward by length, adding length as an explicit term, or filtering the preference data for balanced pairs.
| Method | Signal source | Optimizer | Main failure |
|---|---|---|---|
| RLHF | human pairwise choices | PPO with KL penalty | reward hacking of annotator habits |
| Constitutional AI | written principles plus self critique | rejection sampling and RLAIF | principles under specify the real objective |
| Direct preference optimization | human pairwise choices | closed form policy loss | no separate reward to inspect |
| Best of n | human pairwise choices | sampling at inference | cost grows linearly with n |
Constitutional AI
The alternative removes the human from the inner loop. A constitution is a list of written principles, and the model is asked to critique its own draft against one principle at a time and revise. The revised output becomes training data, so the supervision is explicit, auditable, and cheap to extend.
The advantage is provenance: every training signal traces to a numbered principle that a reviewer can read. The disadvantage is that principles are prose, and prose admits interpretation, so the same revision loop that scales safety also scales whatever blind spots the constitution has.
A hybrid is common in practice. Human preferences teach helpfulness on ordinary prompts, principles and red team generated comparisons teach the boundaries, and the reward model is trained on the union with a reported win rate per slice so that a gain in one region cannot hide a regression in another.
Worked Examples
Worked Code
import math
def sigmoid(z):
return 1.0 / (1.0 + math.exp(-z))
pairs = [(1.6, 0.6), (0.4, 1.1), (0.9, 0.8)]
total = 0.0
for rw, rl in pairs:
p = sigmoid(rw - rl)
loss = -math.log(p)
total += loss
print(f"win prob {p:.3f} loss {loss:.3f}")
print("mean loss:", round(total / len(pairs), 3))
def objective(reward, kl, beta):
return round(reward - beta * kl, 3)
reward, kl = 4.2, 3.0
for beta in (0.05, 0.15, 0.50):
print(f"beta {beta:.2f} -> J = {objective(reward, kl, beta)}")
def win_rate(pairs):
return sum(rw > rl for rw, rl in pairs) / len(pairs)
judged = [(1.6, 0.6), (0.4, 1.1), (0.9, 0.8), (1.2, 0.3)]
print("annotator agreement:", win_rate(judged))
Real-World Applications
- Assistant tuning - Preference pairs convert a generic completion model into one that follows instructions, with the win rate reported per prompt category.
- Safety filtering - Red team comparisons and constitution principles train the refusal boundary that keeps a deployed model inside policy.
- Search and ranking - Learned reward models double as rerankers, so a drift in preference data changes both the ranker and the generator.
- Evaluation - Pairwise human judging scales better than absolute scoring, which is why release gates are expressed as win rates against the previous version.
đĄWatch the reward and the behavior separately
Common Mistakes
Skipping the KL term to gain reward
Fix: Without a reference penalty the policy drifts until the reward model is out of distribution and its scores become meaningless.
Reporting a single aggregate win rate
Fix: Slice by prompt type, because a gain on creative writing can mask a regression on factual questions.
Writing a constitution and never evaluating it
Fix: Each principle needs a test set, otherwise the revision loop optimizes an interpretation nobody checked.
Best Practices
- Keep a frozen reference policy and report KL divergence alongside reward every checkpoint
- Balance preference pairs for length so verbosity stops correlating with the reward
- Combine human preferences with principled critiques and evaluate each slice separately
Key Takeaways
- RLHF has three stages: supervised tuning, reward modeling, and policy optimization
- The Bradley Terry model turns a score margin into a preference probability with a sigmoid
- The KL penalty with coefficient beta is what keeps optimization from leaving the reference policy
- Constitutional AI substitutes written principles and self critique for human labels
Frequently Asked Questions
Why not optimize human ratings directly?
Direct optimization on raw ratings overfits the specific annotator pool faster than optimizing a fitted reward model, and it offers no way to separate the quality signal from annotator specific biases such as length preference.
What does the KL coefficient actually control?
How far the policy may move from the supervised model per unit of reward. Too small and the model abandons its pretrained competence to chase quirks of the reward model; too large and alignment has essentially no effect.
Is direct preference optimization a replacement for RLHF?
It removes the separate reward model and the reinforcement learning loop, which is simpler and more stable, but it still needs the same pairwise preference data. The trade is simplicity for the loss of an inspectable reward signal.
Interview Questions
Q1: How would you detect reward hacking during training?
Track a held out human win rate next to mean reward. If reward rises while the win rate stalls, the policy is exploiting the reward model, and I would inspect the highest reward samples for length and format drift.
Q2: Explain the role of the reference policy in one sentence.
It is the anchor that the KL term pulls against, so the optimizer can only improve reward by moving a bounded distance from behavior the supervised stage already established.