Policy gradients to GRPO
From REINFORCE to group-relative advantages, and how it extends to multi-call agent trajectories.
Skip if You can derive REINFORCE, explain why a baseline keeps it unbiased, and know what GRPO's group baseline does.
The setup
Treat the language model as a policy : given a prompt , it samples a response . A reward function scores the response; for deep research, that’s 1 if the final answer is judged correct and 0 otherwise. We want to maximize the expected reward:
The difficulty is that is sampled, so we can’t backpropagate through the sampling step.
The log-derivative trick (REINFORCE)
For a fixed prompt, write the expectation as a sum and move the gradient inside:
The middle step uses . Now the gradient is an expectation we can estimate by sampling: generate responses, score them, and push up the log-probability of each response in proportion to its reward. For a language model, the log-probability of a response is a sum over its tokens:
so every token in a response receives the same scalar weight.
Baselines: same expectation, less variance
Raw rewards make a noisy signal. For example, if every sample scores 1, every response gets pushed up, even though nothing distinguishes them. The fix is to subtract a baseline that doesn’t depend on . It leaves the gradient unbiased because
The weight becomes the advantage , which measures how much better this response was than expected.
PPO: reusing samples safely
Sampling is expensive, so PPO takes several gradient steps on each batch of samples. It corrects for the policy drifting away from the one that generated the samples with an importance ratio , and clips that ratio so no single update moves the policy too far:
Classic PPO estimates with a learned value network (a critic), which is a second model roughly as large as the policy.
GRPO: the group is the baseline
GRPO (Shao et al., 2024, introduced in DeepSeekMath) drops the critic. For each prompt, it samples a group of responses with rewards and normalizes within the group:
Every token of response gets . A KL penalty keeps the policy close to a reference model.
Binary rewards make this concrete. If a fraction of the group succeeds, the mean is and the standard deviation is , so
On a hard prompt where only 2 of 32 attempts succeed, each success gets an advantage of about , a strong push. On an easy prompt where 30 of 32 succeed, each success gets only about .
Three practical add-ons, all of which the CLM paper uses:
- Dynamic sampling. If every rollout in a group succeeds or every one fails, the standard deviation is zero and the group carries no signal, so these groups are dropped.
- Clip-higher. An asymmetric clip range, in the paper, lets rare good tokens gain probability faster than the default symmetric range would.
- Truncated importance sampling. Rollouts come from a fast inference engine (SGLang or vLLM) whose token probabilities differ slightly from the trainer’s. Capping the correction ratio keeps that mismatch from blowing up the gradient.
From one response to an agent trajectory
An agent doesn’t produce one response; it makes many model calls. Call sees an input (the context at that moment) and produces an output . Between calls, the environment (tools, files) responds, but the environment doesn’t depend on . So the trajectory’s log-probability still decomposes into per-call terms:
In an append-only agent, each is a prefix of the final transcript, so you can train on the final transcript and simply mask out the tool outputs. When the model edits its own context, that shortcut breaks: the final transcript no longer contains what earlier calls actually saw. The fix is bookkeeping, not new math. Store every pair as its own segment, and give each segment the trajectory’s group-relative advantage. That’s stepwise GRPO, which we dissect in §4.2.