Fine-Tuning and RLHF Explained: How Chatbots Learn to Help

By LatestLLM Editorial · Published · Facts verified · 8 min read

Also called: instruction tuning, supervised fine-tuning, SFT, RLHF, reinforcement learning from human feedback, alignment, DPO, LoRA

Before this: Pre-training and loss

In one line

Fine-tuning and RLHF are the stages after pre-training that teach a base model to follow instructions, first from human-written examples and then from human preference rankings.

Fine-tuning and RLHF, simply and technically

In plain English

A freshly pre-trained model is a brilliant autocompleter, not an assistant. Ask it “How do I boil an egg?” and it may continue with more questions, because that is what such text often looks like. Two further stages turn it into something that answers. First, it is shown thousands of examples of good answers written by people. Then people rank several of its own answers from best to worst, and the model is nudged toward the kinds of answers people prefer.

This second stage is called reinforcement learning from human feedback, or RLHF. It is a large part of why chatbots feel helpful and polite, and it is why a much smaller model can be preferred by users over a far larger one that never went through it.

Think of it like this: Pre-training is like a student reading an entire library. Fine-tuning is the tutor who shows them what a good answer to an exam question looks like, and RLHF is the tutor marking several of their attempts and saying which one is best. The student's knowledge is already there; the tutoring shapes how it is used. The picture breaks where it matters: the marks reward answers people like, which is not always the same thing as answers that are right.

Everyday example: Ranking answers

A person is shown two answers to “Explain gravity to a ten-year-old”.

Input:

Two answers, A and B, to the same question

Output:

Human choice: A is better

What it shows: One answer is clear and friendly; the other is accurate but full of jargon. The person marks the first as better. After tens of thousands of such comparisons, the model learns a general sense of what people prefer and is trained to produce answers like the preferred ones.

Technically

Post-training has three typical stages (Ouyang et al. 2022). In supervised fine-tuning (SFT) the base model is trained with the usual next-token loss on demonstrations of ideal responses; InstructGPT used about 13,000 training prompts. A reward model is then trained on human rankings, about 33,000 prompts in the paper, with the pairwise loss −ln σ(r_chosen − r_rejected), the Bradley–Terry model of preference. Finally the policy is optimised with reinforcement learning (PPO) against the reward model on about 31,000 further prompts, with a penalty β log(π / π_ref) that keeps it close to the SFT model, β = 0.02 in the paper.

The headline result: in human evaluations outputs from the 1.3B-parameter InstructGPT were preferred to those of the 175B-parameter GPT-3, despite having 100 times fewer parameters. Lighter alternatives exist. LoRA (Hu et al. 2021) freezes the base weights and trains small low-rank matrices, which for GPT-3 175B reduces trainable parameters by 10,000 times and GPU memory by 3 times. Direct Preference Optimization (Rafailov et al. 2023) replaces the separate reward model and RL loop with a closed-form loss on preference pairs.

Technical example: The reward-model loss and the KL penalty

The pairwise loss for a reward gap between a chosen and a rejected answer, and an RL reward shaped by a KL penalty with β = 0.02 (the InstructGPT value). The log-probabilities and reward are illustrative.

Code (python):

import math

sigmoid = lambda x: 1 / (1 + math.exp(-x))

for gap in (0.0, 1.0, 2.0, 4.0):
    p = sigmoid(gap)                    # chance the chosen answer is preferred
    print(f"reward gap {gap:>3}: P(chosen preferred) = {p:.3f}, loss = {-math.log(p):.3f}")

beta = 0.02
reward, logp_policy, logp_reference = 1.5, -42.0, -45.0   # illustrative numbers
shaped = reward - beta * (logp_policy - logp_reference)
print("shaped reward:", round(shaped, 3), "= 1.5 - 0.02 * (-42.0 - (-45.0))")

Output:

reward gap 0.0: P(chosen preferred) = 0.500, loss = 0.693
reward gap 1.0: P(chosen preferred) = 0.731, loss = 0.313
reward gap 2.0: P(chosen preferred) = 0.881, loss = 0.127
reward gap 4.0: P(chosen preferred) = 0.982, loss = 0.018
shaped reward: 1.44 = 1.5 - 0.02 * (-42.0 - (-45.0))

What it shows: A gap of zero is a coin flip with loss 0.693; as the reward model separates the pair, the loss shrinks toward zero, which is the training signal. The KL term subtracts 0.06 here because the policy has become more likely than the reference to produce this answer; across many tokens it stops the model from drifting into text that games the reward model.

The four stages of RLHF. Four connected stages (pretrain, supervised fine-tune, reward model, reinforcement learning) can be selected to read the data used and the training goal of each, with dataset sizes from the InstructGPT paper. A slider sets the reward gap between a chosen and a rejected answer and the readout gives the preference probability and loss. Stage descriptions and dataset sizes from Ouyang et al. 2022 (InstructGPT); the preference formula is the standard Bradley–Terry loss.

From completer to assistant

In plain English: A base model predicts text; it has no idea it is supposed to help. Before an assistant can exist, someone has to show the model what a helpful reply looks like. That is the first job of fine-tuning.

Technically:

Ouyang et al. argue that making language models bigger does not inherently make them better at following a user's intent: large models can generate outputs that are untruthful, toxic or simply unhelpful, in other words they are not aligned with their users. Their remedy is fine-tuning with human feedback, starting from labeler-written and API-submitted prompts.

Chat models add structure on top: the conversation is formatted with role markers, and fine-tuning teaches the model to answer as the assistant and to stop after its turn.

Supervised fine-tuning

In plain English: In the first stage people write good answers to a collection of prompts, and the model is trained to imitate them. It is ordinary learning from examples, just with a far smaller and far more carefully chosen set of text than pre-training used.

Technically:

SFT keeps the next-token cross-entropy loss but computes it only on the response tokens, not on the prompt. InstructGPT's SFT dataset contained about 13,000 training prompts, from the API and labeler-written. A model trained this way already follows instructions much better than the base model, but demonstrations are costly to write and cap the model at human-written quality.

Quality beats quantity in this stage: carefully curated sets of a few thousand to a few hundred thousand examples can be enough, since the knowledge is already in the pre-trained weights.

What is instruction tuning?

Supervised fine-tuning on many tasks expressed as instructions with their desired responses, so the model generalises to new instructions.

Learning from human preferences

In plain English: Writing a great answer is hard, but choosing the better of two answers is easy. RLHF uses that: people compare answers, a scoring model learns their taste, and the assistant is trained to earn high scores.

Technically:

The reward model is trained on rankings of several model outputs per prompt; about 33,000 training prompts in InstructGPT. Preference learning of this form was developed for reinforcement learning by Christiano et al. (2017), who showed complex behaviours could be trained from human preferences with feedback on less than one percent of the agent's interactions.

In the RL stage the policy generates answers, the reward model scores them and PPO updates the policy, with a per-token KL penalty toward the SFT model so it does not exploit quirks of the reward model. The risk of over-optimisation, where the reward keeps rising while real quality falls, is why the penalty and early stopping matter.

Go deeper: details for practitioners
  • Reward hacking and sycophancy are known side effects: a model can learn to flatter or sound confident because people tend to rate such answers highly.

Cheaper alternatives

In plain English: Full RLHF is complicated and expensive, so there are simpler ways to adapt a model. One keeps the model frozen and trains a small add-on. Another skips the scoring model and learns directly from the comparisons.

Technically:

LoRA freezes the pre-trained weights and injects trainable low-rank matrices into each layer; compared with fully fine-tuning GPT-3 175B with Adam it can reduce trainable parameters by 10,000 times and GPU memory by 3 times, and performs on par or better in quality on the tasks tested. Adapters can be swapped per task and merged back into the weights.

Direct Preference Optimization observes that RLHF fits a reward model and then optimises a policy against it, which is complex and often unstable. DPO reparameterises the reward so the optimal policy can be extracted in closed form and trained with a simple classification-style loss on preference pairs.

Common misconceptions

  • Myth: Fine-tuning teaches the model new facts.

    Reality: It mainly changes style, format and behaviour. Facts come from pre-training, and fine-tuning on a few examples is an unreliable way to add knowledge.

  • Myth: RLHF makes the model more truthful.

    Reality: It makes the model produce answers people prefer. That correlates with helpfulness, but raters can prefer confident or flattering answers that are wrong.

  • Myth: A bigger model is always preferred by users.

    Reality: InstructGPT 1.3B was preferred to GPT-3 175B in human evaluations. Alignment training can outweigh a hundredfold difference in size.

Where you'll see this

  • gpt-oss architecture: gpt-oss models are open-weight reasoning models that write a chain of thought before answering.
  • DeepSeek-V3 architecture: DeepSeek-V3 was pre-trained and then followed by supervised fine-tuning and reinforcement learning stages.
  • Gemma 4 architecture: Gemma 4 ships instruction-tuned versions alongside its pre-trained ones.

Key takeaways

  • Fine-tuning and RLHF turn a base model that completes text into an assistant that follows instructions.
  • InstructGPT used about 13,000 demonstration prompts, 33,000 ranked prompts for the reward model and 31,000 prompts for RL, a tiny fraction of pre-training data.
  • Humans preferred the 1.3B InstructGPT to the 175B GPT-3, showing that alignment can matter more than size.
  • LoRA and DPO are cheaper variants: train a small adapter, or optimise preferences directly without a separate reward model.

Frequently asked questions

What is RLHF?

Reinforcement learning from human feedback: people rank model answers, a reward model learns their preferences, and the model is trained to produce answers that score highly.

What is the difference between fine-tuning and pre-training?

Pre-training teaches general language from trillions of tokens; fine-tuning adapts the model with a much smaller, curated dataset to a style or task.

What is LoRA?

A method that freezes the base weights and trains small low-rank matrices, cutting trainable parameters by up to 10,000 times for GPT-3 175B.

What is DPO?

Direct Preference Optimization, a way to learn from preference pairs with a simple loss, without a separate reward model or reinforcement learning loop.

Why is there a KL penalty in RLHF?

To keep the model close to its fine-tuned version so it does not drift into odd text that exploits the reward model.

Sources

Facts last verified November 23, 2026.

Next concept: Distillation

Learn next

  • Distillation (Intermediate): Distillation is training a small student model to imitate a larger teacher, using the teacher's output probabilities or generated text as targets instead of only the correct answers.
  • Reasoning models (Intermediate): A reasoning model is a language model trained to write out a long chain of thought before its final answer, spending extra tokens at answer time to solve harder problems.
  • Hallucination (Beginner): A hallucination is a fluent, confident statement from a language model that is not true: the model produces plausible text and is rewarded for guessing over admitting it does not know.