Skip to content
King Jin

27 July 2026

PPO for LLM Alignment

While learning RLHF, I kept mixing up two things: RLHF is the training framework, and PPO is the optimization algorithm that does the actual update inside it. PPO is a general reinforcement learning algorithm, and you could swap it for something else. I also had a wrong picture of what PPO does: I thought the new model answers the old questions again and we compare the two rewards. It doesn't. Today I want to get the real picture down.

In one sentence: PPO takes a fixed batch of generated answers, uses the advantage to push the probability of each old token up or down, and clips the probability ratio so the same batch can be reused without the policy moving too far.

Mapping RL words to LLM words

RL conceptIn an LLM
Policy / Actor πθ\pi_\thetathe language model generating tokens
State sts_tprompt + tokens generated so far
Action ata_tthe next token
Trajectorythe full response
Rewardscore for the full answer, plus a per-token KL penalty
Critic Vϕ(st)V_\phi(s_t)predicts the total future return from this prefix

The models involved

ModelUpdated?Job
Actor πθ\pi_\thetayesthe model we want to improve
Critic VϕV_\phiyesestimates the return, gives a low-variance baseline
Reward modelnoscores a full response by human preference
Reference πref\pi_{ref}nostops the actor drifting too far from the SFT model
Actor_old πold\pi_{old}frozen within one batchthe policy that generated this batch

Actor_old is not a fifth permanent model. Every time we collect a new batch, the current actor becomes Actor_old for that batch.

Reward, return, value, advantage

The way I remember it: reward is how many points you got, value is how many points the critic expected, and advantage is how much better you did than expected.

Gt=∑k=0T−tγkrt+kG_t=\sum_{k=0}^{T-t}\gamma^k r_{t+k} At=Gt−Vϕ(st)A_t=G_t-V_\phi(s_t)
  • At>0A_t>0: make the old action more likely.
  • At<0A_t<0: make the old action less likely.

GAE

The reward belongs to the whole answer, so GAE spreads the credit backwards from the end:

δt=rt+γVϕ(st+1)−Vϕ(st)\delta_t=r_t+\gamma V_\phi(s_{t+1})-V_\phi(s_t) AtGAE=δt+γλAt+1GAEA_t^{GAE}=\delta_t+\gamma\lambda A_{t+1}^{GAE}

Rewards per token

The reward model usually gives one score for the whole answer. The KL penalty against the reference model turns into a small reward on every token:

rt=−β[log⁡πold(at∣st)−log⁡πref(at∣st)]+1[t=T] rRM(x,y)r_t=-\beta\left[\log\pi_{old}(a_t|s_t)-\log\pi_{ref}(a_t|s_t)\right]+\mathbb 1[t=T]\,r_{RM}(x,y)

The reward model's score is added only on the last token.

How PPO updates

1. Reuse the batch

Generating answers, scoring them and running the reference and critic are all expensive. So PPO fixes one batch of trajectories and trains on it for several ppo_epochs. In each epoch, it does not generate new answers. It just runs the current actor over the same old tokens (teacher forcing) and recomputes their log-probs.

2. The probability ratio

rt(θ)=πθ(at∣st)πold(at∣st)r_t(\theta)=\frac{\pi_\theta(a_t|s_t)}{\pi_{old}(a_t|s_t)}
  • rt=1r_t=1: the new and old actor give the old token the same probability.
  • rt=1.1r_t=1.1: the current actor made it about 10% more likely.
  • rt=0.8r_t=0.8: about 20% less likely.

3. The clipped objective

Lclip(θ)=Et[min⁡(rt(θ)At, clip⁡(rt(θ),1−ϵ,1+ϵ)At)]L^{clip}(\theta)=\mathbb E_t\left[\min\left(r_t(\theta)A_t,\ \operatorname{clip}(r_t(\theta),1-\epsilon,1+\epsilon)A_t\right)\right]

With ϵ=0.2\epsilon=0.2:

  • Positive advantage: pushing the probability up is right, but there is no extra reward for going past 1.2.
  • Negative advantage: pushing it down is right, but no extra reward for going below 0.8.
  • If the policy moves in the wrong direction, the objective still punishes it. Clipping does not switch off every gradient.

One round of training

  1. Actor_old generates answers for a batch of prompts.
  2. The reward model scores them, the reference model gives log-probs, the critic gives values.
  3. Compute rewards, then GAE advantages and returns.
  4. Freeze this batch and train the actor and critic on it for several epochs.
  5. Collect a fresh batch with the updated actor and repeat.

Fixed inside one batch: prompts, response tokens, old log-probs, old values, rewards, advantages, returns. Changing in each epoch: the actor and critic weights, new log-probs, the ratio, new values, the loss.

The critic learns to predict the fixed target return:

LV=12(Vϕ(st)−G^t)2\mathcal L_V=\frac12\left(V_\phi(s_t)-\hat G_t\right)^2

Two kinds of "don't move too far"

ComparePurposeHow long
Actor_new vs Actor_oldreuse one batch safelyrefreshed every batch
Actor vs Referencelimit long-term drift during RLHFfixed the whole time

The PPO ratio and clipping are not the same as the reference KL. The first keeps one batch's optimization stable, and the second keeps the model close to the SFT model overall.

Things I got wrong at first

  • Inside a PPO epoch the model does not answer again, and there is no reward_new. New answers and new rewards only come in the next batch.
  • PPO looks at the advantage, not whether the reward is positive. A positive reward that is lower than the critic expected still gives a negative advantage.
  • Clipping only affects the tokens that were sampled. It is not a hard limit on the whole vocabulary distribution, so people still monitor approximate KL.

Why it works, and why it is hard

It works because:

  • it turns a non-differentiable reward score into token probability updates
  • one expensive batch can be reused for several epochs
  • the ratio and clipping stop the old data from being overused
  • the critic and GAE reduce the variance of the policy gradient

It is hard because:

  • the reward model can be exploited, and spreading one score over a whole answer is only an approximation
  • four models run at the same time, so memory and throughput are expensive
  • the KL coefficient, reward scale, advantage normalisation, clip range and GAE settings all affect stability

References

OlderHow an LLM Gets Trained →