Reinforcement Learning

RLHF as Reinforcement Learning

RLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

status: reviewimportance: criticaldifficulty 4/5math: graduateread: 23mlive demo

Concept Structure

RLHF as Reinforcement Learning

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

2prerequisites
4next concepts
4related links

Learner Contract

What this page should let you do.

You are here becauseRLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

This Reinforcement Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.

By the end4/4 sections ready | runnable code expected | live demo

Explain the mechanism, trace the main notation, and test one prediction in the live demo.

Do this firstIntuition

Read the intuition before the notation; the math should name a mechanism you already felt.

Test the linkManipulate one control and predict the visible change.Then continue to RLHF: Reward Modeling + KL-Regularized Policy Optimization

Claim/source review status

Substantive review recorded

1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.
Claims1/1 reviewed
Sources4 cited
Codeattached
Demolive
Reviewed2026-07-03
Updatedpage 2026-07-03

Learning item flow

4/4 sections readyAsk about thisResearch room
ConceptRLHF as Reinforcement LearningReinforcement Learning
4 sources attachedLocal snapshot ready
concept:reinforcement-learning/rlhf-as-rl
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

RLHF is easier to understand once you can point to the reinforcement-learning objects, but the mapping is not a perfect textbook MDP.

For one language-model prompt, the context xx plays the role of the state or observation. A sampled completion y=(a1,,aT)y=(a_1,\ldots,a_T) is the action in a one-shot view, or a token trajectory in an autoregressive view. A reward model rϕ(x,y)r_\phi(x,y) gives a learned proxy reward. A KL penalty to a reference model acts like a control cost that discourages the policy from moving too far from the model that supervised fine-tuning produced. PPO or another policy-gradient optimizer supplies the update rule; it is not the definition of RLHF.

That map is useful because it makes the training loop inspectable:

  1. sample completions from the current policy
  2. score them with a learned reward model
  3. subtract a KL/reference penalty
  4. update the policy toward sampled completions that scored better than expected

The limits matter just as much. The prompt is not necessarily a full Markov state. The action space is an enormous structured sequence space. The reward is learned from human comparisons, not handed down by nature. Preference data may be collected offline or from earlier policies. Optimizing the proxy can reveal reward-model errors. The right mental model is not "RLHF is exactly a tiny MDP"; it is "RLHF borrows policy-optimization machinery, then adds language-model-specific caveats."

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Let xx be a prompt or conversation prefix. A policy πθ\pi_\theta samples a completion

y=(a1,,aT),πθ(yx)=t=1Tπθ(atx,a<t).y=(a_1,\ldots,a_T), \qquad \pi_\theta(y\mid x) = \prod_{t=1}^T \pi_\theta(a_t\mid x,a_{<t}).

In a one-shot RL view, xx is the observation and yy is the action. In an autoregressive view, the token prefixes (x,a<t)(x,a_{<t}) are the changing observations and each next token is an action.

A reward model trained from preference data gives a scalar proxy reward

R^(x,y)=rϕ(x,y).\hat R(x,y)=r_\phi(x,y).

In InstructGPT-style RLHF, the policy objective also penalizes movement away from a reference policy πref\pi_{\mathrm{ref}}, often the supervised-finetuned model. A compact prompt-level objective is

J(θ)=ExD,  yπθ(x)[rϕ(x,y)βlogπθ(yx)πref(yx)].J(\theta) = \mathbb E_{x\sim D,\;y\sim\pi_\theta(\cdot\mid x)} \left[ r_\phi(x,y) - \beta \log \frac{\pi_\theta(y\mid x)} {\pi_{\mathrm{ref}}(y\mid x)} \right].

The second term can be expanded across tokens:

logπθ(yx)πref(yx)=t=1Tlogπθ(atx,a<t)πref(atx,a<t).\log \frac{\pi_\theta(y\mid x)} {\pi_{\mathrm{ref}}(y\mid x)} = \sum_{t=1}^T \log \frac{ \pi_\theta(a_t\mid x,a_{<t}) }{ \pi_{\mathrm{ref}}(a_t\mid x,a_{<t}) }.

For a sampled completion, define the KL-penalized score

R~(x,y)=rϕ(x,y)βlogπθ(yx)πref(yx).\tilde R(x,y) = r_\phi(x,y) - \beta \log \frac{\pi_\theta(y\mid x)} {\pi_{\mathrm{ref}}(y\mid x)}.

A policy-gradient update uses the same score-function idea as REINFORCE:

g^=(R~(x,y)b(x))θlogπθ(yx).\hat g = \left(\tilde R(x,y)-b(x)\right) \nabla_\theta \log \pi_\theta(y\mid x).

Here b(x)b(x) is a baseline or value estimate that reduces variance. PPO changes how the gradient step is constrained and reused across minibatches, but the learner-facing object map stays the same: sampled policy log-probabilities, a reward-like scalar, a reference-policy penalty, and an update direction.

This is where the analogy bends:

  • xx is often a partial observation, not a complete environment state.
  • yy is a long structured sequence, not a small discrete action.
  • rϕr_\phi is a learned preference proxy, not the true objective.
  • human comparisons train the reward model; they are not identical to online environment rewards from the current rollout.
  • KL anchoring is a design choice, not a theorem that fixes reward hacking.
03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt

This small example scores three sampled completions with reward minus KL cost, then applies a policy-gradient-style advantage against a prompt baseline.

import numpy as np

beta = 0.20
baseline = 0.55
samples = [
    ("concise answer", 1.10, -2.3, -2.7),
    ("verbose drift", 1.35, -1.0, -2.6),
    ("safe refusal", 0.75, -2.0, -2.1),
]

for text, reward, logp, logp_ref in samples:
    kl_cost = beta * (logp - logp_ref)
    score = reward - kl_cost
    advantage = score - baseline
    direction = "increase" if advantage > 0 else "decrease"
    print(text)
    print("  reward model:", round(reward, 2))
    print("  beta * log(pi/ref):", round(kl_cost, 2))
    print("  reward minus KL:", round(score, 2))
    print("  advantage:", round(advantage, 2), "=>", direction)

The second completion has the highest reward-model score, but it also moved farthest from the reference policy. The update sees the KL-penalized score, not reward alone.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Use the RLHF as RL Bridge Lab to map one highlighted post-training object to its RL role.

Choose a case, predict the role, then reveal the mapping and the caveat. The lab keeps the answer locked until you commit, because the point is to practice the map: prompt context, sampled completion, learned reward, KL-anchored policy update, and analogy limit.

Live Concept Demo

Explore RLHF as Reinforcement Learning

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 4/5graduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what RLHF as Reinforcement Learning should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

RLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change RLHF as Reinforcement Learning should make visible.

Visual Inquiry

Make the image answer a mathematical question

RLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make RLHF as Reinforcement Learning easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

paper · 2017Deep Reinforcement Learning from Human PreferencesChristiano, Leike, Brown, Martic, Legg, and Amodei

Grounds reward predictors learned from human comparisons of trajectory segments while a policy optimizes the predicted reward.

Open source
paper · 2022Training language models to follow instructions with human feedbackOuyang et al.

Grounds the InstructGPT pipeline: demonstrations for SFT, human rankings for a reward model, and PPO against the reward model with a per-token KL penalty to the SFT model.

Open source
paper · 2017Proximal Policy Optimization AlgorithmsSchulman, Wolski, Dhariwal, Radford, and Klimov

Grounds PPO as a policy-gradient method using a clipped surrogate objective and sampled-data minibatch optimization.

Open source
course-notes · 2026Stanford CS234: Lecture 5, Policy Gradient IStanford CS234

Graduate RL source for likelihood-ratio policy gradients, trajectory log-probability decomposition, baselines, and advantage estimates.

Open source

Claim Review

RLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources4 references

christiano-2017-human-preferences, ouyang-2022-instructgpt, schulman-2017-ppo, stanford-cs234-policy-gradient-2026

Local checks4 local checks

Use equations, runnable code, and demos to check whether the source support is operational.

Substantively reviewedLM RLHF maps context to observation, sampled completion to action or trajectory, learned reward to proxy reward, and PPO/policy-gradient steps to KL-anchored policy optimization.Claim metadata: source checked

Christiano supports preference-trained reward predictors optimized by a policy. Ouyang supports SFT -> reward model -> PPO with per-token KL to a reference model. Schulman supports PPO as policy-gradient optimization; CS234 supports score-function and baseline mechanics.

Sources: Deep Reinforcement Learning from Human Preferences, Training language models to follow instructions with human feedback, Proximal Policy Optimization Algorithms, Stanford CS234: Lecture 5, Policy Gradient IBridge only: LM post-training is not a small fully observed textbook MDP. Excludes full PPO derivation, reward-model architecture, annotator policy, DPO/KTO, process rewards, and empirical alignment claims.A bounded review summary is present; still check caveats and exact reference scope.

Checked Christiano et al. for preference-trained reward predictors optimized by a policy; checked Ouyang et al. for the SFT -> reward model -> PPO pipeline, bandit-style prompt/response environment, and per-token KL penalty to the SFT model; checked Schulman et al. for PPO as a policy-gradient optimizer; checked Stanford CS234 Lecture 5 for score-function policy-gradient and baseline/advantage mechanics. GPT Pro publication critique remains pending because 127.0.0.1:51672 is unavailable.

Reviewer: codex-local-source-review; reviewed 2026-07-03

Practice Loop

Try the idea before it explains itself

RLHF can be read as policy optimization over sampled completions, learned proxy rewards, and KL anchoring, but the language-model setting breaks several textbook MDP assumptions.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in RLHF as Reinforcement Learning.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Grounded research drawerClose
ConceptRLHF as Reinforcement LearningReinforcement Learning
Runnable code comparisonRLHF as Reinforcement Learning runnable code 1beta = 0.20Prediction before revealRLHF as Reinforcement Learning interactive demoManipulate one control and predict the visible change.
Grounded room questionWhat is the smallest example that makes RLHF as Reinforcement Learning click without losing the math?Local snapshot ready

Research Room

Attach the question to a claim, equation, code, or demo

Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptReinforcement Learning

RLHF as Reinforcement Learning

Attached question

What is the smallest example that makes RLHF as Reinforcement Learning click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays in this browser, attached to the selected learning item.

No local draft saved.
Evidence to inspect
  • References to inspect: attached references on this page.
  • Definition, prerequisite, and contrast concept links
  • The equation or runnable code that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - RLHF as Reinforcement Learning Selected item key: recorded for copy. Context: Reinforcement Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes RLHF as Reinforcement Learning click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

View it in context
concept/concept-notebook/reinforcement-learning/rlhf-as-rl concept:reinforcement-learning/rlhf-as-rl