Skip to content

RLHF and Constitutional AI: learning from preferences

The InstructGPT pipeline (supervised tuning, a reward model fitted to preference pairs, then RL held near the start by a KL penalty) and Constitutional AI, which swaps human raters for written principles.

Advanced ~15 min9 steps

What runs in CodeSplash

You will be able to

  • Name the three stages of the RLHF pipeline and what each one trains
  • Write the Bradley–Terry loss a reward model is fitted with
  • Explain what the KL penalty to a reference model protects against
  • Describe how Constitutional AI produces preference labels, and what it shifts responsibility onto

Read first

01

Three stages

Read

RLHFReinforcement learning from human feedback (RLHF)Tuning a model with a reward model fit to people's pairwise preferences, then optimizing against it with RL such as PPO., as in InstructGPT, runs in three stages:

  1. Supervised fine-tuning on demonstrations of good answers (instruction tuning).
  2. A reward model fitted to human comparisons: for one prompt, raters say which of two responses is better.
  3. Reinforcement learning: the language model writes answers, the reward model scores them, and PPO raises the score, while a penalty keeps the model near where it started.

Raters find it easier to compare two answers than to write a perfect one, which is why preferences, not more demonstrations, drive stage 2.

02

The reward model

Read

A reward modelReward modelA model trained on human preference pairs to score responses; RLHF then trains the policy to raise that score. rϕr_\phi scores a prompt and response. It is fitted with the Bradley–TerryBradley–Terry modelA model of pairwise judgments: the difference in two items' log-worths sets the log odds that one is preferred to the other. loss on each chosen (ywy_w) and rejected (yly_l) pair:

L(ϕ)=−log⁡σ ⁣(rϕ(x,yw)−rϕ(x,yl)).L(\phi) = -\log \sigma\!\big(r_\phi(x, y_w) - r_\phi(x, y_l)\big).

Only differences matter, so adding a constant to every score changes nothing. In the reference's example, the chosen response scores higher by a margin that gives a loss of 0.371; a larger margin would push it toward zero.

03

Stay near the start

Read

The RL stage maximizes the reward minus a KL penaltyKL penalty to a reference modelA cost for drifting from the starting model's token probabilities, so preference tuning can't trade everything for reward.:

max⁡π  E[rϕ(x,y)]−β KL(π(⋅∣x) ∥ πref(⋅∣x)).\max_\pi\; \mathbb{E}\big[r_\phi(x, y)\big] - \beta\, \mathrm{KL}\big(\pi(\cdot \mid x) \,\Vert\, \pi_{\text{ref}}(\cdot \mid x)\big).

The reward model is an imperfect proxy fitted to limited comparisons. Optimize it without restraint and the policy finds responses the reward model overrates: long, flattering or oddly formatted. The KL term charges for drifting from the supervised model. In the reference's example a policy with KL 0.082 from its reference ends with a penalized utility of 0.752.

DExplorable · Preference optimization: reward models and DPO

How do pairwise preferences turn into a policy, and what does β protect against?

0.50

Small β lets the policy move far from the reference; large β keeps it close.

100

Comparisons simulated from hidden true rewards with a Bradley–Terry model.

500

Full-batch gradient steps on the DPO loss.

0.50

Step size on the implicit reward β(θ − θ_ref), so β doesn't change the speed.

Correct and helpful0.81Correct but curt0.12Polite but vague0.05Confident and wrong0.01Off topic0.00Refuses0.00
  • Reference policy
  • DPO policy
  • True optimum π_ref·exp(r*/β)
  • Reward model + optimum (dots)
KL from the reference (nats)
1.240
Expected true reward
1.802
Largest response probability
0.814
Final DPO loss
0.4230
  • Puzzle: Get within 0.1 of the true optimum's expected reward while staying under 1 nat from the reference.
  • Puzzle: Make the policy put more than 90% on a single response.
After 500 steps on 100 pairs with β = 0.50, the policy gives “Correct and helpful” 81% and sits 1.24 nats from the reference. Its expected true reward is 1.80, against 0.34 for the reference and 1.71 for the true KL-regularized optimum. In this toy, DPO's policy is exactly what a fitted Bradley–Terry reward model plus the KL-regularized optimum would give.

After 500 steps on 100 pairs with β = 0.50, the policy gives “Correct and helpful” 81% and sits 1.24 nats from the reference. Its expected true reward is 1.80, against 0.34 for the reference and 1.71 for the true KL-regularized optimum. In this toy, DPO's policy is exactly what a fitted Bradley–Terry reward model plus the KL-regularized optimum would give.

04

Constitutional AI

Read

Constitutional AIConstitutional AIPreference tuning where a model critiques and ranks responses against written principles instead of human raters. replaces most human preference labels with a model guided by written principles. The model critiques and revises its own answers against the principles (a supervised stage), then a model judges pairs of answers against them to train the reward model (reinforcement learning from AI feedback).

In the reference's example, two responses scored against three weighted principles get ratings of 0.83 and 0.75, so the first would be "chosen". The choice of principles, their weights and the judging model now carry the values that raters carried before. Audit them, and keep human checks on outcomes.

05

What runs here

Read
Reference · LNG063 · Explained hereInstructGPT / RLHF pipelineAligns a pretrained language model with human demonstrations and preference comparisons through supervised fine-tuning, reward modeling, and reinforcement learning.
06

Predict: no penalty

Predict

Predict first

You set β = 0 and train for longer. The reward model's scores keep rising. What happens to the quality people would judge?

Higher true quality, since reward goes upReward goes up while quality can fall: the policy exploits the reward model
Reveal

It can fall. With no anchor, the policy drifts to outputs the reward model wasn't trained on and overrates. Rising proxy reward with falling true quality is reward overoptimization.

07

Common mistakes

Read
08

Check: RLHF and Constitutional AI

Checkpoint

Check yourself

Put the InstructGPT pipeline in order.

  1. 1Fit a reward model to human preference pairs
  2. 2Optimize the policy with PPO against the reward model, with a KL penalty
  3. 3Supervised fine-tuning on demonstrations

Show the answer

The order:

  1. Supervised fine-tuning on demonstrations
  2. Fit a reward model to human preference pairs
  3. Optimize the policy with PPO against the reward model, with a KL penalty

Each stage starts from the previous one's model; the KL penalty is measured against the supervised model.

Check yourself

A reward model scores the chosen response 2.0 and the rejected one 0.0. What is its Bradley–Terry loss, −log σ(2)? (Three decimals.)

Show the answer

Answer: 0.127

σ(2) = 1 / (1 + e⁻²) ≈ 0.881, and −log 0.881 ≈ 0.127. The loss depends only on the difference of the two scores.

Check yourself

What does the KL penalty in RLHF protect against?

Show the answer

Answer: a

The penalty keeps the policy where the reward model's judgments were learned, so its proxy stays meaningful.

Check yourself

In Constitutional AI, where do most preference labels come from?

Show the answer

Answer: a

That is reinforcement learning from AI feedback. Humans write and revise the principles instead of labeling every pair.

Check yourself

A team replaces human raters with Constitutional AI. What must they now audit most carefully?

Show the answer

Answer: a

The values move from the raters into the constitution and the judge. Human checks on outcomes remain necessary.

09

Recap

Recap
  • RLHF: supervised tuning, a Bradley–Terry reward model from preference pairs, then PPO against it.
  • The KL penalty to the reference model keeps the policy where the proxy reward is trustworthy.
  • Constitutional AI produces preferences from written principles and a judging model; audit both.
  • Explained here; Bradley–Terry and PPO run elsewhere in CodeSplash.

Concepts in this lesson