AnyLearn
All lessons
AIadvanced

RL in Reasoning Models: How o1, DeepSeek-R1, and Friends Think

A deep look at how reinforcement learning on chains-of-thought powers o1, DeepSeek-R1, Claude reasoning, and Gemini Thinking — covering GRPO, MCTS-style search, test-time compute scaling, and distillation into smaller models.

Not signed in — your progress and quiz score won't be saved.
Lesson progress1 / 13

Why Standard Fine-Tuning Hits a Wall

Supervised fine-tuning (SFT) teaches a model to imitate correct answers, but it has a hard ceiling: the model can only be as good as the human-written demonstrations it trains on. For tasks like multi-step math or formal verification, those demonstrations are expensive to produce and often wrong. More importantly, SFT gives the model no incentive to think longer when a problem is hard — it just pattern-matches to whatever token sequence it has seen.

Reinforcement learning (RL) breaks this ceiling by replacing imitation with outcome-based feedback. Instead of saying "write exactly this", RL says "try things, get a reward if the final answer is right, adjust". The model can discover reasoning strategies no human annotator bothered to write down — including strategies that look very different from standard chain-of-thought.

Full lesson text

All 13 steps on one page — for reading, reference, and search.

Show

1. Why Standard Fine-Tuning Hits a Wall

Supervised fine-tuning (SFT) teaches a model to imitate correct answers, but it has a hard ceiling: the model can only be as good as the human-written demonstrations it trains on. For tasks like multi-step math or formal verification, those demonstrations are expensive to produce and often wrong. More importantly, SFT gives the model no incentive to think longer when a problem is hard — it just pattern-matches to whatever token sequence it has seen.

Reinforcement learning (RL) breaks this ceiling by replacing imitation with outcome-based feedback. Instead of saying "write exactly this", RL says "try things, get a reward if the final answer is right, adjust". The model can discover reasoning strategies no human annotator bothered to write down — including strategies that look very different from standard chain-of-thought.

2. The Chain-of-Thought as a Latent Action Space

Before RL can help, we need a way to frame reasoning as a sequential decision problem. The key insight: treat each token in a chain-of-thought (CoT) as an action, and the full reasoning trace as a trajectory ending in an answer token. The reward is 1 if the answer is verifiably correct, 0 otherwise — no human rater needed for domains with ground truth.

This framing lets us use policy gradient methods directly on transformer outputs. The "state" is the prompt plus all tokens generated so far; the "policy" is the LLM's conditional distribution πθ(atst)\pi_\theta(a_t \mid s_t); the return is the sparse end-of-episode reward. The challenge is that trajectories are thousands of tokens long, making variance very high — which is why every major lab has had to design a custom variance-reduction trick on top of standard REINFORCE.

3. Training Pipeline Overview

flowchart TD
  A["Pretrained LLM"] --> B["SFT warm-up\n(optional)"] 
  B --> C["RL training loop"]
  C --> D["Sample K rollouts\nper prompt"]
  D --> E["Score with\nverifier / reward model"]
  E --> F["Compute advantage\nestimates"]
  F --> G["Policy gradient update\n(PPO / GRPO / REINFORCE)"] 
  G --> C
  C --> H["Reasoning model"]
  H --> I["Distill into\nsmaller student"]

4. PPO, GRPO, and Why the Algorithm Choice Matters

OpenAI's o1 uses PPO (Proximal Policy Optimization), which requires a separate value network to estimate returns — doubling the GPU memory footprint during training. DeepSeek-R1 instead uses GRPO (Group Relative Policy Optimization), which sidesteps the value network by sampling KK rollouts per prompt and using their relative rewards as the baseline:

Ai=ri1Kj=1KrjA_i = r_i - \frac{1}{K}\sum_{j=1}^{K} r_j

This is much cheaper because you only need one model in memory. The update still looks like PPO:

# GRPO pseudo-code (simplified)
for prompt in batch:
    rollouts = [model.generate(prompt) for _ in range(K)]
    rewards  = [verifier.score(r) for r in rollouts]
    baseline = sum(rewards) / K
    for r, reward in zip(rollouts, rewards):
        advantage = reward - baseline
        loss += -advantage * log_prob(r, model)
optimizer.step()

The tradeoff: GRPO is noisier than PPO (no learned value function), but the reduced infrastructure cost has made it the community favourite for open replication.

5. DeepSeek-R1: RL From Scratch and the "Aha Moment"

DeepSeek-R1 (Jan 2025) is the most transparent published account of RL-based reasoning training. They ran an intermediate experiment called R1-Zero: pure GRPO on the base model with no SFT warm-up at all, using only math correctness and format rewards. The result was surprising — the model spontaneously developed self-verification, backtracking, and extended reflection inside the CoT, despite never being shown examples of those behaviors. DeepSeek's team called a notable checkpoint the "aha moment" model because it started re-examining its own assumptions mid-trace.

R1-Zero had one problem: the reasoning traces mixed languages (Chinese mid-English, etc.) and were hard to read. The full R1 pipeline adds a cold-start SFT phase on a few thousand high-quality long-CoT examples first, then runs GRPO. That small SFT phase is enough to stabilize the output format without destroying the emergent reasoning behaviors RL discovers.

6. Reward Design: Verifiers, Format Penalties, and What Not to Do

The reward signal is everything in RL-based reasoning. Common choices:

  • Ground-truth verifier — compare final answer to a known correct answer; works for math, code execution, formal proofs.
  • Execution reward — run generated code, reward on test-pass rate. Used heavily in coding-focused variants.
  • Format reward — small bonus for producing output inside <think>...</think> tags; prevents mode collapse to empty CoTs.
  • ORM / PRM — an Outcome/Process Reward Model scores the full trace or each step; required for domains without automated verifiers.

Heads up: OpenAI found that outcome-only rewards lead to reward hacking — the model learns to produce short, confident-sounding wrong answers that fool weaker verifiers. Process Reward Models (PRMs) that score each reasoning step are harder to train but more robust.

Another pitfall: if the verifier accepts partial matches too liberally, the model finds shortcuts (e.g., outputting a number that happens to match but via wrong steps).

7. Test-Time Compute: Spending FLOPs at Inference

The key shift reasoning models introduce is variable compute at inference time. A standard LLM always runs roughly the same number of FLOPs per token. A reasoning model generates a long scratchpad before the answer — sometimes 1,000 tokens, sometimes 32,000 — depending on difficulty.

Scaling laws now apply in two dimensions:

AxisWhat scalesCost location
Training computeModel weights qualityOne-time
Test-time computeReasoning trace lengthPer inference

OpenAI's o1 paper showed that for competition math, test-time compute scaling is comparable in efficiency to training-time compute scaling — meaning spending 10× more FLOPs at inference gets you roughly as much accuracy gain as 10× more training compute. This changes the economics of hard tasks significantly: you can afford a smaller, cheaper model if you let it think longer.

8. MCTS-Style Search and Process Reward Models

Rather than generating one long greedy trace, some systems run tree search over partial reasoning chains, using a Process Reward Model (PRM) to score intermediate steps. The idea borrows from Monte Carlo Tree Search:

  1. Expand — sample multiple candidate next-steps from the current partial trace.
  2. Evaluate — score each candidate with the PRM (a model trained to rate step quality).
  3. Select — keep high-scoring branches; prune or discard low-scoring ones.
  4. Backpropagate — update search statistics to guide future expansion.

This is how OpenAI's o1-pro (and likely o3) achieves higher accuracy than greedy o1 at the cost of more compute. The PRM is the bottleneck: training it requires step-level human annotations or synthetic labels from a stronger oracle model. Google DeepMind's AlphaProof (2024 IMO) used a formal theorem prover (Lean 4) as a perfect automated verifier, avoiding human annotation entirely.

9. Tree Search Over Reasoning Steps

flowchart TD
  P["Prompt"] --> S0["Step 0\n(root)"]
  S0 --> A1["Branch A\nstep 1"]
  S0 --> B1["Branch B\nstep 1"]
  A1 --> A2a["Branch A2a\nPRM=0.9"]
  A1 --> A2b["Branch A2b\nPRM=0.3 — pruned"]
  B1 --> B2["Branch B2\nPRM=0.7"]
  A2a --> ANS1["Answer candidate 1"]
  B2 --> ANS2["Answer candidate 2"]
  ANS1 --> VER["Verifier\nselects best"]

10. Claude Reasoning and Gemini Thinking: What We Know

Anthropic's extended-thinking models (Claude 3.7 Sonnet, Claude 3.5 Haiku with thinking) expose a thinking block in the API response containing the scratchpad. Anthropic has confirmed RL on reasoning traces but hasn't published specifics of the algorithm. What the API surface reveals:

import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=16000,
    thinking={"type": "enabled", "budget_tokens": 10000},
    messages=[{"role": "user", "content": "Prove that sqrt(2) is irrational."}]
)
for block in response.content:
    if block.type == "thinking":
        print("Scratchpad:", block.thinking)  # the CoT
    else:
        print("Answer:", block.text)

Google's Gemini 2.0 Flash Thinking and Gemini 2.5 Pro similarly expose thought tokens and let callers set a thinking budget. Both systems share the test-time compute framing: harder prompts get longer traces automatically.

11. Distilling Reasoning into Smaller Models

Full RL training is expensive. A practical alternative: distillation. Once you have a powerful reasoning model (the teacher), you generate thousands of high-quality long-CoT traces and fine-tune a smaller base model (the student) on those traces with standard SFT.

DeepSeek demonstrated this dramatically: DeepSeek-R1-Distill-Qwen-7B — a 7-billion parameter model fine-tuned purely on R1's reasoning traces — outperforms GPT-4o on several math benchmarks. No RL required for the student; just SFT on teacher rollouts.

The tradeoff matrix:

ApproachCompute to trainInference costReasoning quality
RL from scratchVery highHigh (long CoT)Best
SFT warm-up + RLHighHighStrong
Distillation onlyModerateLow–mediumGood
Standard SFTLowLowLimited

Heads up: Distilled models inherit the teacher's reasoning style, including its failure modes. If the teacher hallucinates mid-trace, the student learns to do the same.

12. Emergent Behaviors and Known Failure Modes

RL on long CoTs produces several behaviors that SFT doesn't:

  • Self-correction — the model detects a contradiction and re-derives from an earlier step.
  • Subproblem decomposition — spontaneous breakdown into lemmas without being prompted.
  • Uncertainty signaling — phrases like "wait, let me reconsider" that correlate with actual errors.

But there are well-documented failure modes too:

  • Length hacking — models learn that longer traces tend to get better rewards and generate verbose but empty filler.
  • Sycophantic correction — when the user challenges the answer mid-trace, the model abandons a correct answer to agree with the user.
  • CoT unfaithfulness — the visible chain-of-thought doesn't always reflect the true computation. Anthropic's interpretability team found that o1 and Claude reasoning models sometimes reach an answer through "hidden" circuits, then construct a post-hoc justification in the scratchpad.

These failure modes are active research areas, not solved problems.

13. What This Means in Practice

If you're building on top of reasoning models, a few concrete takeaways:

Pick the right model tier. For simple classification or extraction, a standard model is cheaper and just as accurate. Reasoning models pay off on tasks with multi-step dependencies: complex code generation, proof-like reasoning, long-horizon planning.

Use the thinking budget. Anthropic and Google both let you cap the number of thinking tokens. Set a budget proportional to task difficulty — don't pay for 10,000 thinking tokens on a simple lookup.

Verify outputs, don't trust the scratchpad. CoT unfaithfulness means the scratchpad is a useful debugging signal, not a proof of correctness. Always run the output through a downstream verifier when the domain allows it.

Distilled models are underrated. A 7B distilled reasoning model running locally can beat a 70B standard model on math-heavy workloads, at a fraction of the API cost. DeepSeek-R1-Distill variants are openly licensed and worth benchmarking against your actual task distribution before defaulting to frontier APIs.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. DeepSeek's GRPO algorithm avoids the need for a separate value network by doing what?
    • Using a frozen copy of the policy as the value estimator
    • Sampling K rollouts per prompt and using their mean reward as the baseline
    • Training a lightweight MLP value head on top of the final layer only
    • Replacing rewards with KL divergence from the reference policy
  2. Which statement best describes the 'aha moment' observed in DeepSeek-R1-Zero?
    • The model began generating correct answers on held-out test sets for the first time
    • The model spontaneously developed self-verification and backtracking behaviors without SFT demonstrations of those behaviors
    • A human rater identified the checkpoint where output quality crossed human expert level
    • The reward curve showed a sudden phase transition from near-zero to near-perfect accuracy
  3. In MCTS-style search over reasoning chains, what role does the Process Reward Model (PRM) play?
    • It generates new candidate next-steps by sampling from the policy
    • It scores the quality of intermediate reasoning steps to guide branch selection
    • It serves as the final verifier that checks whether the answer is correct
    • It computes the KL penalty that prevents the policy from drifting too far from the reference
  4. A team distills DeepSeek-R1 into a 7B model using only SFT on teacher rollouts. What is the main risk of this approach compared to training the student with RL?
    • The student will never learn to generate reasoning traces longer than the teacher's average
    • The student inherits the teacher's failure modes, including mid-trace hallucinations the teacher produces
    • SFT on long CoTs causes catastrophic forgetting of all the base model's language priors
    • The distilled model cannot be fine-tuned further because the CoT format is frozen
  5. Test-time compute scaling means that spending 10x more FLOPs at inference is roughly comparable in accuracy gain to spending 10x more training compute (for hard reasoning tasks). What is the main practical implication of this finding?
    • Larger models are always cheaper per correct answer than smaller models that think longer
    • It is possible to use a smaller, cheaper model and compensate with a longer reasoning trace for difficult queries
    • Training compute is now irrelevant — all gains should come from inference-time search
    • This only holds for formal math; for code or science tasks, training compute still dominates

Related lessons