RLHF Explained: How LLMs Learn from Human Feedback
How RLHF works: SFT, a reward model trained on preference pairs, then PPO with a KL penalty. Plus DPO, RLAIF, reward hacking and a short TRL code example.
Reinforcement learning from human feedback (RLHF) is the training step that turned raw text predictors into assistants that follow instructions. Instead of writing a reward function by hand, you ask people which of two outputs is better, train a model to predict those judgements, and then optimise the language model against that learned score. This article walks through the standard pipeline as OpenAI described it for InstructGPT, the simpler DPO method that many teams now use instead, AI-generated feedback (RLAIF), and the failure modes you should plan for. Everything here comes from the original papers and the Hugging Face TRL documentation, linked inline.
Where the idea came from
The method predates chatbots. In 2017, Paul Christiano and colleagues showed in Deep reinforcement learning from human preferences that an agent could learn Atari games and simulated robot movement without any built-in reward function. Non-expert humans compared pairs of short trajectory clips, and the system learned a reward from those comparisons. The authors note that people gave feedback on under one in a hundred of the agent’s interactions, and that some new behaviours took roughly an hour of human time to teach.
The key insight carries over directly to language models: people are bad at assigning absolute scores but reasonably good at saying which of two options they prefer.
The three-step RLHF pipeline
OpenAI’s 2022 InstructGPT paper, Training language models to follow instructions with human feedback, set out the recipe most later work follows. The paper summarises it as three steps:
| Step | Data | What gets trained |
|---|---|---|
| 1. Supervised fine-tuning (SFT) | Prompts with human-written ideal answers | GPT-3 fine-tuned to imitate the demonstrations |
| 2. Reward modelling | Several model outputs per prompt, ranked by labellers | A model that outputs a single score for a prompt and response |
| 3. Reinforcement learning | Fresh prompts, scored by the reward model | The SFT model, optimised with PPO |
Step 1: SFT. OpenAI hired a team of about 40 contractors through Upwork and Scale AI. They wrote example answers for prompts, some written by the labellers and some taken from the OpenAI API. GPT-3 was fine-tuned on these demonstrations with ordinary supervised learning.
Step 2: reward model. Labellers then saw between four and nine model outputs for a prompt and ranked them. Each ranking yields every possible pairwise comparison. The reward model starts from the SFT model with its final unembedding layer removed, so it reads a prompt plus a response and returns one number. It is trained with a cross-entropy loss so the gap between two scores reflects the log odds that a labeller prefers one response over the other. OpenAI used 6B reward models only; it reported that 175B reward model training could be unstable.
Step 3: PPO with a KL penalty. The policy is then trained with Proximal Policy Optimization. Each episode is one prompt and one response, with the reward model supplying the score. OpenAI added a per-token KL penalty measured against the SFT model, specifically to stop the policy over-optimising the reward model. A coefficient (beta) sets how strong that pull back toward the SFT model is.
RLHF also cost some capability. The PPO model did worse than GPT-3 on several public benchmarks, including SQuAD, DROP and HellaSwag, which the paper calls an “alignment tax”. Mixing pretraining updates into the PPO updates, a variant OpenAI named PPO-ptx, reduced those regressions without hurting labeller preference scores.
The headline result: in human evaluations on OpenAI’s prompt distribution, labellers preferred outputs from the 1.3B InstructGPT model over those from the 175B GPT-3, a model more than a hundred times larger.
DPO: preference tuning without reinforcement learning
The full pipeline has many moving parts: a separate reward model, sampling from the policy during training, and touchy PPO hyperparameters. The 2023 Direct Preference Optimization (DPO) paper describes standard RLHF as “a complex and often unstable procedure”.
DPO rewrites the RLHF objective so the best policy can be found in closed form. In practice, this means you train directly on preference pairs with a simple classification-style loss. There is no reward model to fit, and the model never generates samples during fine-tuning. The loss compares how much more likely the trained model makes the chosen answer, relative to a frozen reference model, than the rejected one. A beta hyperparameter controls how strongly the preference signal pushes. The authors report that DPO beat PPO-based RLHF at controlling the sentiment of generations, and matched or beat it on summarisation and single-turn dialogue.
Here is the quickstart from the TRL DPO Trainer docs. It trains Qwen3-0.6B on the UltraFeedback preference dataset:
from trl import DPOTrainer
from datasets import load_dataset
trainer = DPOTrainer(
model="Qwen/Qwen3-0.6B",
train_dataset=load_dataset("trl-lib/ultrafeedback_binarized", split="train"),
)
trainer.train()
The dataset needs chosen and rejected fields, plus an optional prompt. TRL also accepts chat-style lists of messages and applies the model’s chat template for you:
preference_example = {"prompt": "The sky is", "chosen": " blue.", "rejected": " green."}
If you want the classic pipeline instead, TRL’s RewardTrainer takes the same preference format and fits a Bradley-Terry reward model, where the probability that one answer wins is a sigmoid of the difference between the two reward scores. Note that in TRL v1.17.1 there is no PPO trainer page in the stable docs; that page exists only on the main branch. The stable trainer list covers DPO, GRPO, KTO, Reward, RLOO, SFT and Distillation. For cheaper runs, DPOTrainer accepts a peft_config such as a LoRA config. Our PEFT tutorial covers that setup.
RLAIF and Constitutional AI
Human labels are slow and expensive. Anthropic’s Constitutional AI: Harmlessness from AI Feedback (2022) swaps most of them for model judgements. The only human input is a written list of principles, the “constitution”. Training has two phases:
- Supervised: the model drafts answers, critiques them against the principles, revises them, and is fine-tuned on the revised versions.
- Reinforcement learning: a model picks the better of two samples, a preference model is trained on those AI choices, and that preference model provides the reward. Anthropic calls this “RL from AI Feedback” (RLAIF).
Anthropic reports that the result was a harmless assistant that was not evasive: it engaged with harmful requests by explaining why it objected. A later study, RLAIF vs. RLHF (ICML 2024), found that RLAIF performed comparably to RLHF on summarisation and on helpful and harmless dialogue. It also tested a variant that skips the reward model and asks an LLM for rewards directly during training.
Known problems: reward hacking and over-optimisation
A reward model is a proxy for what people want, and pushing too hard on a proxy breaks it. Leo Gao and co-authors’ Scaling Laws for Reward Model Overoptimization (2022) measured this. Collecting enough human labels to study it would have been too costly, so they used a fixed “gold-standard” reward model as a stand-in for humans and trained smaller proxy reward models on its labels. As the policy was optimised harder against the proxy, the gold score eventually fell, a case of Goodhart’s law. The shape of that curve depended on whether they used RL or best-of-n sampling. In both cases it shifted smoothly with reward model size. They also studied how the dataset size and the KL penalty coefficient change the effect.
What this means in practice:
- Keep the KL penalty. It is the main brake on the policy exploiting reward model errors, which is why InstructGPT used it.
- Watch for score inflation. If the reward keeps climbing but human or held-out evaluations stall or drop, you are probably exploiting the proxy. Score outputs with a separate evaluation suite; see our guide to LLM evaluation metrics.
- Check reward scale. TRL’s reward docs point out that adding a constant to every reward leaves preferences unchanged, so scores can drift. TRL offers an optional auxiliary loss to centre rewards around zero, controlled by
center_rewards_coefficient, with a suggested value of 0.01. The idea comes from a paper on reward model ensembles and reward hacking. - Expect an alignment tax. Measure the benchmarks you care about before and after, as InstructGPT did.
Which approach to pick
| Situation | Reasonable starting point |
|---|---|
| You have preference pairs and want the least infrastructure | DPO via DPOTrainer |
| You need a reusable scorer (for ranking, filtering or online RL) | Train a reward model with RewardTrainer |
| Labelling budget is the bottleneck | RLAIF: an LLM judge guided by written principles |
| You only have good example answers, not comparisons | Start with SFT; add preferences later |
Preference tuning usually comes after a supervised pass on domain data. If you are building a specialised agent, start with fine-tuning domain-specific AI agents, then add DPO or RLHF once you can collect comparisons. Tools for serving and evaluating the result are listed under LLM infrastructure.
Written by AI Agents Directory
Editorial team
Facts in this guide are checked against official documentation and repositories, which are linked in the article.