Yuyi Ao

Understanding Policy Gradients in LLMs

Published on Sep 22, 2026

Contents

I’ve tried to learn RL for LLMs several times, but the connection between the intuition and math was not clear to me. Recently, I came across Nathan Lambert’s RLHF Book [1], which helped me connect the pieces. I’m truly thankful for this great material. In this blog, I’ll discuss what a policy gradient is, why it is useful, and how it is used in LLM RL training.

RL for LLMs#

In general, there are two major types of methods in RL.

  • The first one is value-based, meaning that you want to study how much return you get when you take a certain action in a given state.
  • The second one is policy-based, meaning that you want to learn a policy (an LLM) that gets the maximum expected return in the environment.

Most of the RL methods for LLMs we have heard about are policy-based, which means we use rewards to improve the model itself directly. For LLMs, policy-based RL feels like a natural continuation of pretraining: we already have a model that knows how to generate, and now we use rewards to improve what it generates.

In LLM generation, the state is the current token sequence, and an action appends one token to it.

Before we start, let’s introduce the notation we will use.

  • $\tau=(s_0,a_0,r_0,s_1,a_1,r_1,\ldots)$: a sampled trajectory.
  • $s_t$: the current token sequence, with $s_0=\text{prompt}$ and $s_{t+1}=\operatorname{concat}(s_t,a_t)$.
  • $\pi_\theta(a_t\mid s_t)$: the next-token policy.
  • $R(\tau)$: the sequence reward, with no explicit dependence on $\theta$.

The LLM RL Objective#

Our objective is to find a policy that maximizes the expected return, starting from an initial state drawn from some distribution. Ideally, this prompt distribution should reflect the tasks we want the model to perform.

In an RL environment, taking an action can bring you to a new state and give you an immediate reward. So we can write our objective as

$$ J(\theta) =\mathop{\Large\mathbb{E}}\limits_{\tau\sim p_\theta(\cdot)}[R(\tau)] =\sum_\tau p_\theta(\tau)R(\tau) $$

with

$$ p_\theta(\tau) =p(s_0)\prod_{t=0}^{T-1}\pi_\theta(a_t\mid s_t)\,\textcolor{#8e8e93}{p(s_{t+1}\mid s_t,a_t)} $$

Here, $p(s_0)$ is the initial-state distribution and does not depend on $\theta$.

In general RL, every step can also give a reward $r_k$, discounted by $\gamma$:

$$ R(\tau)=\sum_{k=0}^{T-1}\textcolor{#8e8e93}{\gamma^k}r_k. $$

Computing this expectation exactly is impossible because the space of possible responses is too large. At each step, there are $|V|$ possible actions. For a fixed response length of $T$ tokens, there are $|V|^{T}$ possible sequences. So how do we find the optimal policy that maximizes our objective, the expected return?

Deriving the Policy Gradient#

How can we use gradient ascent to maximize this objective? We start by differentiating it with respect to the policy parameters:

$$ \begin{aligned} \nabla_\theta J(\theta) &=\nabla_\theta \mathop{\Large\mathbb{E}}\limits_{\tau\sim p_\theta(\cdot)}[R(\tau)] \\ &=\nabla_\theta\sum_\tau p_\theta(\tau)R(\tau) \\ &=\sum_\tau \nabla_\theta p_\theta(\tau)R(\tau) \\ &=\sum_\tau p_\theta(\tau) R(\tau) \nabla_\theta \log p_\theta(\tau) \\ &\boldsymbol{=\mathop{\Large\mathbb{E}}\limits_{\tau\sim p_\theta(\cdot)}[R(\tau)\nabla_\theta\log p_\theta(\tau)]} \\ &\boldsymbol{=\mathop{\Large\mathbb{E}}\limits_{\tau\sim p_\theta(\cdot)}\left[ R(\tau)\sum_{t=0}^{T-1}\nabla_\theta\log\pi_\theta(a_t\mid s_t) \right]}. \end{aligned} $$

From the derivation above, we can see that the gradient of the objective is the expectation of the reward-weighted gradient of the trajectory log probability. Notice that this is an exact transformation!

Implementing Policy Gradients#

Since the gradient is now written as an expectation over trajectories, we can use sampling to estimate it.

$$ \nabla_\theta J(\theta) \approx\frac{1}{B}\sum_{i=1}^{B} R(\tau_i)\nabla_\theta\log p_\theta(\tau_i), \qquad \tau_i\sim p_\theta. $$

The approximation comes from replacing the expectation with an average over $B$ sampled rollouts.

But why estimate the gradient rather than the objective itself? Think about what happens if we estimate the objective directly by sampling:

$$ \hat J=\frac{1}{B}\sum_{i=1}^{B}R(\tau_i). $$

Once the rollouts are fixed, this is just an average of fixed rewards, with no differentiable path back to $\theta$. In the policy-gradient estimate, however, $\log p_\theta(\tau_i)$ still depends on the policy parameters, giving us a way to do backpropagation.

To compute it with automatic differentiation, we write a loss whose derivative gives that estimate with a negative sign:

$$ L_{\mathrm{PG}}(\theta) =-\frac{1}{B}\sum_{i=1}^{B} R(\tau_i)\log p_\theta(\tau_i). $$

However, this sampled estimate is noisy. Common RL algorithms therefore rewrite the gradient to reduce this noise.

We start from the gradient derived above:

$$ \nabla_\theta J(\theta)=\mathop{\Large\mathbb{E}}\limits_{\tau\sim p_\theta(\cdot)}\left[\sum_{t=0}^{T-1}R(\tau)\nabla_\theta\log\pi_\theta(a_t|s_t)\right]. $$

The following derivations use two facts.

Lemma 1 (linearity). For any random terms $X_t$, the expectation of a sum is the sum of the expectations. With a finite horizon $T$, this always holds:

$$ \mathbb{E}\left[\sum_{t=0}^{T-1}X_t\right]=\sum_{t=0}^{T-1}\mathbb{E}[X_t]. $$

Lemma 2 (zero-mean score). If $X$ does not depend on $a_t$, it moves out of the sum over $a_t$, and what is left is the gradient of a constant:

$$ \begin{aligned} \mathop{\Large\mathbb{E}}\limits_{a_t\sim\pi_\theta(\cdot|s_t)}\left[X\,\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &=X\sum_{a_t}\pi_\theta(a_t|s_t)\frac{\nabla_\theta\pi_\theta(a_t|s_t)}{\pi_\theta(a_t|s_t)} \\ &=X\,\nabla_\theta\sum_{a_t}\pi_\theta(a_t|s_t) \\ &=X\,\nabla_\theta 1=0. \end{aligned} $$

From R(τ) to Gt#

Before reducing the noise, we look at a useful property of the policy gradient: an action can only be credited for what comes after it. Any term that is fixed before $a_t$ is sampled contributes zero to the expected gradient of $\log\pi_\theta(a_t|s_t)$. Past rewards are such a term, so $R(\tau)$ can be replaced by $G_t$.

The idea is to split $R(\tau)$ separately for each step $t$, right before $a_t$. Rewards on the left were given before $a_t$ was sampled. Rewards on the right, starting from $r_t$, can change with $a_t$:

ht: everything beforeatτ′:atand everything afters0a0r0s1⋯rt−1statrtst+1rt+1⋯R<t: fixed oncehtis fixedR≥t: can change withatcut right before the action in ∇log π(at|st)

For each step $t$, we split the trajectory as in the figure and define:

$$ \begin{aligned} R_{\lt t}&=\sum_{k\lt t}\textcolor{#8e8e93}{\gamma^k}r_k,\qquad R_{\ge t}=\sum_{k\ge t}\textcolor{#8e8e93}{\gamma^k}r_k, \\ P_{\text{before}}&=p(s_0)\prod_{k\lt t}\pi_\theta(a_k|s_k)\,\textcolor{#8e8e93}{p(s_{k+1}|s_k,a_k)}, \\ P_{\text{after}}&=\prod_{k\gt t}\pi_\theta(a_k|s_k)\prod_{k\ge t}\textcolor{#8e8e93}{p(s_{k+1}|s_k,a_k)}, \\ G_t&=\sum_{k\ge t}\textcolor{#8e8e93}{\gamma^{k-t}}r_k. \end{aligned} $$

The whole derivation is:

$$ \begin{alignedat}{2} \nabla_\theta J(\theta) &=\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\sum_{t}R(\tau)\nabla_\theta\log\pi_\theta(a_t|s_t)\right] \\ &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[R(\tau)\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &\qquad&\textcolor{#8e8e93}{\footnotesize\text{Lemma 1}} \\ &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[R_{\lt t}\nabla_\theta\log\pi_\theta(a_t|s_t)\right]+\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[R_{\ge t}\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &&\textcolor{#8e8e93}{\footnotesize\text{split }R(\tau)} \\ &=\sum_{t}\sum_{s_0,a_0,\ldots,s_t}P_{\text{before}}\underbrace{\mathop{\Large\mathbb{E}}\limits_{a_t}\left[R_{\lt t}\nabla_\theta\log\pi_\theta(a_t|s_t)\underbrace{\sum_{s_{t+1},a_{t+1},\ldots}P_{\text{after}}}_{1}\right]}_{0}+\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[R_{\ge t}\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &&\textcolor{#8e8e93}{\footnotesize\text{Lemma 2}} \\ &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\textcolor{#8e8e93}{\gamma^t}\,G_t\,\nabla_\theta\log\pi_\theta(a_t|s_t)\right]. &&\textcolor{#8e8e93}{\footnotesize\text{def. of }G_t} \end{alignedat} $$

For LLMs with a single final reward, $R_{\lt t}=0$ and $G_t=R(\tau)$, so this changes nothing by itself. What matters is the rule: the next section uses it to subtract a baseline.

Baseline and Value Function#

We can subtract any function $b(s_t)$ of the state and keep the same expected gradient:

$$ \begin{alignedat}{2} \nabla_\theta J(\theta) &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\textcolor{#8e8e93}{\gamma^t}\,G_t\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &\qquad&\textcolor{#8e8e93}{\footnotesize\text{reward-to-go}} \\ &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\textcolor{#8e8e93}{\gamma^t}\,G_t\nabla_\theta\log\pi_\theta(a_t|s_t)\right]-\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\textcolor{#8e8e93}{\gamma^t}\,b(s_t)\nabla_\theta\log\pi_\theta(a_t|s_t)\right] &&\textcolor{#8e8e93}{\footnotesize b(s_t)\text{ fixed before }a_t} \\ &=\sum_{t}\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\textcolor{#8e8e93}{\gamma^t}\left(G_t-b(s_t)\right)\nabla_\theta\log\pi_\theta(a_t|s_t)\right]. &&\textcolor{#8e8e93}{\footnotesize\text{Lemma 1}} \end{alignedat} $$

A baseline can also reduce the variance. Let $g_b=(G_t-b)\nabla_\theta\log\pi_\theta(a_t|s_t)$ and $w=\|\nabla_\theta\log\pi_\theta(a_t|s_t)\|^2$. Since $g_b$ and $g_0$ have the same mean, their variances differ only in the second moment:

$$ \begin{aligned} \mathrm{Var}(g_b)-\mathrm{Var}(g_0) &=\mathbb{E}\big[(G_t-b)^2w\big]-\mathbb{E}\big[G_t^2w\big] \\ &=\mathbb{E}\big[(b^2-2bG_t)\,w\big] \\ &=b^2\,\mathbb{E}[w]-2b\,\mathbb{E}[wG_t]. \end{aligned} $$

This is negative whenever $b$ lies between $0$ and $2b^*$, where $b^*=\dfrac{\mathbb{E}[wG_t]}{\mathbb{E}[w]}$. The reduction is largest at $b=b^*$.

In practice, we ignore the weight $w$. Then $b^*$ is just the mean return from $s_t$, which is the value function:

$$ b^*(s_t)\approx\mathbb{E}[G_t\,|\,s_t]=V(s_t). $$

$V(s_t)$ is not exactly $b^*$. It is usually close, so it usually reduces the variance as well. $G_t-V(s_t)$ is the advantage: how much better this action did than the policy does on average from $s_t$.

Actor-critic methods learn $V$ with a second network $V_\phi(s_t)$, the critic, trained to predict $G_t$. For LLMs, the critic is usually another large model, so it is expensive to train.

Group Estimate#

Group methods drop the critic and use a baseline that depends only on the prompt, $V(s_0)$. It is fixed before every action, so it is valid for every token. We estimate it from samples: we sample $K$ rollouts $\tau_1,\ldots,\tau_K$ from the same prompt $s_0$, with rewards $R_i=R(\tau_i)$. Each method gives rollout $i$ a weight $A_i$, held fixed during backpropagation, and uses the same loss:

$$ L(\theta)=-\frac{1}{K}\sum_{i=1}^{K}A_i\sum_{t=0}^{T-1}\log\pi_\theta(a_{i,t}|s_{i,t}). $$

With $A_i=R_i$, this is $L_{\mathrm{PG}}$ from above.

RLOO uses the mean reward of the other rollouts, $b_i=\dfrac{1}{K-1}\sum_{j\neq i}R_j$, and $A_i=R_i-b_i$ [2, 3]. The other rollouts are sampled independently of $\tau_i$, so $b_i$ contains none of its actions, and the baseline proof applies directly. RLOO is unbiased.

The group mean $\bar R=\dfrac{1}{K}\sum_{j=1}^{K}R_j$ includes $R_i$, which depends on the actions of $\tau_i$, so Lemma 2 does not apply to it. Splitting it into the rollout itself and the others shows that the effect is only a rescaling:

$$ \bar R=\frac{1}{K}R_i+\frac{K-1}{K}b_i \quad\Longrightarrow\quad R_i-\bar R=\frac{K-1}{K}\left(R_i-b_i\right). $$

The expected gradient becomes $\dfrac{K-1}{K}\nabla_\theta J(\theta)$. The direction is the same, and the learning rate absorbs the constant.

GRPO uses the group mean and also divides by the group standard deviation, $A_i=\dfrac{R_i-\bar R}{\mathrm{std}(R_1,\ldots,R_K)}$ [4]. The std is different for each prompt, so it roughly reweights prompts and changes the objective, not only the scale. Dr. GRPO removes this division [5].

Conclusion#

Every method in this post uses the same form of gradient and only changes the weight in front of $\nabla_\theta\log\pi_\theta(a_t|s_t)$. Following [6], we call this weight $\Psi_t$:

$$ \nabla_\theta J(\theta)=\mathop{\Large\mathbb{E}}\limits_{\tau}\left[\sum_{t}\Psi_t\,\nabla_\theta\log\pi_\theta(a_t|s_t)\right]. $$

It starts as $\Psi_t=R(\tau)$, and each section replaces it with a less noisy choice:

$$ \begin{alignedat}{2} \Psi_t&=R(\tau) &\qquad&\textcolor{#8e8e93}{\footnotesize\text{policy gradient}} \\ \Psi_t&=G_t &&\textcolor{#8e8e93}{\footnotesize\text{reward-to-go}} \\ \Psi_t&=G_t-V_\phi(s_t) &&\textcolor{#8e8e93}{\footnotesize\text{actor-critic (advantage)}} \\ \Psi_i&=R_i-\frac{1}{K-1}\sum_{j\neq i}R_j &&\textcolor{#8e8e93}{\footnotesize\text{RLOO}} \\ \Psi_i&=R_i-\bar R &&\textcolor{#8e8e93}{\footnotesize\text{Dr. GRPO}} \\ \Psi_i&=\frac{R_i-\bar R}{\mathrm{std}(R_1,\ldots,R_K)} &&\textcolor{#8e8e93}{\footnotesize\text{GRPO}} \end{alignedat} $$

All of them keep the expected gradient, except two: the group mean rescales it by $\dfrac{K-1}{K}$, and the std division reweights prompts.

References#

  1. Nathan Lambert. RLHF Book: Policy Gradients and Implementation.
  2. Wouter Kool, Herke van Hoof, and Max Welling. Buy 4 REINFORCE Samples, Get a Baseline for Free!. ICLR Workshop, 2019.
  3. Arash Ahmadian et al. Back to Basics: Revisiting REINFORCE Style Optimization for Learning from Human Feedback in LLMs. ACL, 2024.
  4. Zhihong Shao et al. DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models. arXiv, 2024.
  5. Zichen Liu et al. Understanding R1-Zero-Like Training: A Critical Perspective. arXiv, 2025.
  6. John Schulman et al. High-Dimensional Continuous Control Using Generalized Advantage Estimation. ICLR, 2016.