LESSON 13 · UNDERSTANDING LLMs

RLHF

Reinforcement Learning from Human Feedback (RLHF) is a family of alignment techniques that uses human preference signals to make model behavior more useful, safer, and better matched to what people want.

25 min readIntermediateGenerative AI

By the end of this lesson, you will be able to:

  • Explain why instruction tuning alone may not fully determine response quality.
  • Read a preference pair and identify what humans are rewarding.
  • Explain what a reward model does.
  • Understand the classic RLHF pipeline at a high level.
  • Build a small Python simulation of preference-based scoring.
  • Distinguish RLHF from pre-training, fine-tuning, prompting, and RAG.
1

Why do we need RLHF?

Instruction tuning teaches a model to follow examples, but many possible answers can satisfy the same instruction. People may prefer one answer because it is clearer, more complete, more helpful, more concise, or safer.

Simple idea: instruction tuning teaches a model patterns of good responses. Human preference data adds a signal about which response people prefer when several answers are possible.
Base modelBroad language capability
Instruction tuningFollows requested tasks
Human preferencesBetter response wins
Aligned behaviorBehavior shifts toward preferences
2

Start with a real preference decision

Imagine a customer asks: “My order is five days late. What should I do?” Both answers are understandable, but a support team may prefer one.

RESPONSE A

Helpful and complete

“I’m sorry your order is delayed. Please share the order number and I’ll help you check the latest shipping status.”

RESPONSE B

Too abrupt

“Give me your order number.”

Human preference: A > B. The preferred answer acknowledges the problem, stays polite, and asks for the information needed to help.

Notice that the human is not writing a new answer. They are providing a preference signal between candidate answers.

3

What is human preference data?

A common setup gives a reviewer the same prompt and two or more candidate responses. The reviewer ranks them or chooses the preferred response.

</> Python
example = {
    "prompt": "Explain a refund policy simply.",
    "response_a": "You can request a refund within 30 days.",
    "response_b": "Refunds may be available under the policy.",
    "preferred": "response_a",
}

print("Human preferred:", example["preferred"])
OutputHuman preferred: response_aThe important signal is the relationship: response A was judged better than response B for this prompt.
4

Step 1: start from an instruction-following model

In the classic RLHF pipeline, the model usually begins with broad pre-training and supervised fine-tuning (SFT). SFT provides examples of instructions and high-quality responses so the model has a useful assistant-like starting point.

Instruction“Summarize this complaint in one sentence.”
Target response“The customer reports that their payment was charged twice.”

RLHF is therefore not a replacement for pre-training. It is a later stage that uses preference information to further shape behavior.

5

Step 2: train a reward model

A reward model learns to predict which candidate response would receive a higher human preference score. Instead of asking a person to judge every response during optimization, the learned reward model provides a scalable approximation of those judgments.

Prompt“Explain a refund policy.”
Candidate AClear, specific answerReward: 0.86
Candidate BVague answerReward: 0.31
Important: a reward model is a learned proxy for human preferences. It is not the human itself, and a poorly designed preference dataset or reward model can produce undesirable incentives.
6

Step 3: optimize the language model with the reward signal

In the classic RLHF formulation, the language model generates responses, the reward model scores them, and an RL algorithm updates the policy while regularizing it so it does not move too far from the starting model.

PromptGive the model a task
GenerateModel produces a response
ScoreReward model estimates preference
UpdateOptimization changes the policy

The loop is repeated over many examples. The goal is not simply to maximize any arbitrary score; practical systems also use constraints and evaluation to reduce reward hacking and preserve useful model capabilities.

7

Build a tiny preference scorer in Python

Training a production reward model requires a neural network and a large preference dataset. We can still understand the core idea with a tiny transparent experiment: define a few measurable signals and combine them into a simple score.

</> Python
responses = [
    {"name": "A", "helpful": 0.9, "clear": 0.8},
    {"name": "B", "helpful": 0.6, "clear": 0.9},
]

def score(response):
    return 0.7 * response["helpful"] + 0.3 * response["clear"]

for response in responses:
    response["reward"] = score(response)

best = max(responses, key=lambda item: item["reward"])
print(best)

What this demonstrates: a scoring function can turn several desired properties into one ranking signal. A real reward model learns such a relationship from preference data rather than using hand-written weights like this toy example.

8

Why RLHF is difficult

Preference qualityReviewers need clear criteria and representative examples.
Reward hackingA model can find ways to score well without truly improving the intended behavior.
Distribution shiftPreferences collected on one task mix may not cover every future use.
Capability trade-offsOptimization can change behavior in ways that need careful evaluation.

Modern alignment systems can use methods beyond the classic RLHF pipeline, including direct preference optimization and other preference-learning approaches. The common idea is still to use information about preferred versus less-preferred outputs.

9

RLHF vs other techniques

Pre-trainingLearn broad patterns from large text or multimodal datasets.
Fine-tuningAdapt a pre-trained model to a narrower dataset or behavior.
Instruction tuningTrain on instruction–response examples to improve task following.
RLHFUse human preference information to optimize behavior toward preferred outputs.
PromptingChange model instructions at inference time without changing model weights.
RAGProvide retrieved external context at inference time to ground answers.

Practice: think like a preference reviewer

Try each question before revealing the answer.

1Why might a human prefer response A even when both answers are factually correct?Because preference can include helpfulness, clarity, tone, completeness, safety, and usefulness—not only factual correctness.
2What does the reward model provide during optimization?It provides a learned score or preference signal estimating how well a generated response matches the patterns found in human preference data.
3Is the tiny Python scorer above a real reward model?No. It is a toy model with hand-written features and weights used only to make the ranking idea easy to see.

Quick quiz

What is the central idea behind RLHF?

30-second recap

  • Instruction tuning teaches a model to follow examples, while RLHF uses preference information to further shape behavior.
  • Humans compare candidate responses and create preference data.
  • A reward model learns to approximate those preference judgments.
  • In classic RLHF, the model is optimized using the reward signal with constraints and evaluation.
  • RLHF is one alignment approach; it is different from prompting and RAG, which do not directly change model weights.