$npx -y skills add itsmostafa/llm-engineering-skills --skill rlhfUnderstanding Reinforcement Learning from Human Feedback (RLHF) for aligning language models. Use when learning about preference data, reward modeling, policy optimization, or direct alignment algorithms like DPO.
| 1 | # Understanding RLHF |
| 2 | |
| 3 | Reinforcement Learning from Human Feedback (RLHF) is a technique for aligning language models with human preferences. Rather than relying solely on next-token prediction, RLHF uses human judgment to guide model behavior toward helpful, harmless, and honest outputs. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | |
| 7 | - [Core Concepts](#core-concepts) |
| 8 | - [The RLHF Pipeline](#the-rlhf-pipeline) |
| 9 | - [Preference Data](#preference-data) |
| 10 | - [Instruction Tuning](#instruction-tuning) |
| 11 | - [Reward Modeling](#reward-modeling) |
| 12 | - [Policy Optimization](#policy-optimization) |
| 13 | - [Direct Alignment Algorithms](#direct-alignment-algorithms) |
| 14 | - [Challenges](#challenges) |
| 15 | - [Best Practices](#best-practices) |
| 16 | - [References](#references) |
| 17 | |
| 18 | ## Core Concepts |
| 19 | |
| 20 | ### Why RLHF? |
| 21 | |
| 22 | Pretraining produces models that predict likely text, not necessarily *good* text. A model trained on internet data learns to complete text in ways that reflect its training distribution—including toxic, unhelpful, or dishonest patterns. RLHF addresses this gap by optimizing for human preferences rather than likelihood. |
| 23 | |
| 24 | The core insight: humans can often recognize good outputs more easily than they can specify what makes an output good. RLHF exploits this by collecting human judgments and using them to shape model behavior. |
| 25 | |
| 26 | ### The Alignment Problem |
| 27 | |
| 28 | Language models face several alignment challenges: |
| 29 | |
| 30 | - **Helpfulness**: Following instructions and providing useful information |
| 31 | - **Harmlessness**: Avoiding toxic, dangerous, or inappropriate outputs |
| 32 | - **Honesty**: Acknowledging uncertainty and avoiding fabrication |
| 33 | - **Intent alignment**: Understanding what users actually want, not just what they say |
| 34 | |
| 35 | RLHF provides a framework for encoding these properties through preference data. |
| 36 | |
| 37 | ### Key Components |
| 38 | |
| 39 | 1. **Preference data**: Human judgments comparing model outputs |
| 40 | 2. **Reward model**: A learned function approximating human preferences |
| 41 | 3. **Policy optimization**: RL algorithms that maximize expected reward |
| 42 | 4. **Regularization**: Constraints preventing deviation from the base model |
| 43 | |
| 44 | ## The RLHF Pipeline |
| 45 | |
| 46 | The standard RLHF pipeline consists of three main stages: |
| 47 | |
| 48 | ### Stage 1: Supervised Fine-Tuning (SFT) |
| 49 | |
| 50 | Start with a pretrained language model and fine-tune it on high-quality demonstrations. This teaches the model the desired format and style of responses. |
| 51 | |
| 52 | **Input**: Pretrained model + demonstration dataset |
| 53 | **Output**: SFT model that can follow instructions |
| 54 | |
| 55 | ### Stage 2: Reward Model Training |
| 56 | |
| 57 | Train a model to predict human preferences between pairs of outputs. The reward model learns to score outputs in a way that correlates with human judgment. |
| 58 | |
| 59 | **Input**: SFT model + preference dataset (chosen/rejected pairs) |
| 60 | **Output**: Reward model that scores any output |
| 61 | |
| 62 | ### Stage 3: Policy Optimization |
| 63 | |
| 64 | Use reinforcement learning to optimize the SFT model against the reward model, while staying close to the original SFT distribution. |
| 65 | |
| 66 | **Input**: SFT model + reward model |
| 67 | **Output**: Final aligned model |
| 68 | |
| 69 | ### Alternative: Direct Alignment |
| 70 | |
| 71 | Direct alignment algorithms (DPO, IPO, KTO) skip the reward model entirely, optimizing directly from preference data. This simplifies the pipeline but trades off some flexibility. |
| 72 | |
| 73 | ### Modern Post-Training Variants |
| 74 | |
| 75 | Current post-training stacks often mix these stages rather than using a single linear pipeline: |
| 76 | |
| 77 | | Method | Data Signal | Best Fit | |
| 78 | |--------|-------------|----------| |
| 79 | | SFT | Demonstrations | Format, style, instruction following | |
| 80 | | DPO/IPO/KTO/ORPO | Offline preferences or binary feedback | Simpler alignment without online rollouts | |
| 81 | | PPO/RLOO | Reward model scores on sampled responses | Reward-model RL with explicit KL control | |
| 82 | | GRPO | Grouped completions scored by reward functions/models | Reasoning and verifiable-task optimization | |
| 83 | |
| 84 | ## Preference Data |
| 85 | |
| 86 | Preference data encodes human judgment about model outputs. The most common format is pairwise comparisons. |
| 87 | |
| 88 | ### Pairwise Preferences |
| 89 | |
| 90 | Given a prompt, collect two or more model outputs and have humans indicate which is better: |
| 91 | |
| 92 | ``` |
| 93 | Prompt: "Explain quantum entanglement" |
| 94 | |
| 95 | Response A: [technical explanation] |
| 96 | Response B: [simpler explanation with analogy] |
| 97 | |
| 98 | Human preference: B > A |
| 99 | ``` |
| 100 | |
| 101 | This creates (prompt, chosen, rejected) tuples for training. |
| 102 | |
| 103 | ### Collection Methods |
| 104 | |
| 105 | **Human annotation**: Trained annotators compare outputs according to guidelines. Most reliable but expensive and slow. |
| 106 | |
| 107 | **AI feedback**: Use a capable model to generate preferences. Faster and cheaper but may propagate biases. This is the basis for Constitutional AI (CAI) and RLAIF. |
| 108 | |
| 109 | **Implicit signals**: User interactions like upvotes, regeneration requests, or conversation length. Noisy but abundant. |
| 110 | |
| 111 | ### Data Quality Considerations |
| 112 | |
| 113 | - **Annotator agreement**: Low agreement suggests amb |