PPO for Language Models › Worked Example: Tracing a Complete PPO Update
Language AI HandbookAll chapters PPO for Language Models › Worked Example: Tracing a Complete PPO UpdateOne section · Sections in this chapter8 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To solidify the abstract mechanics, let us trace through a complete PPO update for a short response, computing every quantity step by step. We will use a simplified three-token response to keep the numbers manageable.
Setup: Consider a prompt "What is 2+2?" and a response with three tokens: "The", "answer", "is" (we omit the actual number for brevity). The reward model scores this as 0.7 (somewhat helpful but incomplete). The reference model and current policy have slightly different log probabilities for each token.
Step 1: Assign the sparse reward. The terminal reward of 0.7 goes to the final token, while earlier tokens receive zero:
Step 2: Compute the per-token KL divergence. Suppose the log probabilities are:
The KL contribution at each step is the log ratio:
Step 3: Apply KL shaping with . The shaped rewards are:
Token 1 receives a small penalty because the policy moved toward higher probability than the reference. Token 2 receives a small bonus because the policy moved toward lower probability than the reference. Token 3 receives most of the terminal reward, slightly discounted by the KL penalty.
Step 4: Compute TD errors and GAE advantages. Suppose the value function estimates are:
With , the TD errors are:
With , the GAE advantages work backward:
All three advantages are positive, meaning all three tokens led to outcomes better than what the value function predicted. This makes sense: the response received a reward of 0.7 while the value function only predicted around 0.3 to 0.6 for the intermediate states.
Step 5: Normalize advantages. We subtract the mean and divide by the standard deviation:
After normalization, tokens 1 and 2 have positive advantages (they were above average), while token 3 has a negative advantage (it was below average even though the reward was positive, because the value function had already predicted a high value for that state).
Step 6: Compute probability ratios. Suppose during the gradient update epoch, the policy has shifted slightly:
The ratio for token 1 is . The advantage is positive, so the policy is trying to increase this token's probability. The ratio of 1.05 is within the trust region , so clipping does not activate.
Step 7: Apply the clipped objective. For token 1 with advantage :
Both are equal here because the ratio is within the trust region. The gradient flows through the unclipped objective. For a token where the ratio had exceeded 1.2, clipping would have capped the objective at , preventing the gradient from pushing the probability any higher.
This walkthrough shows the complete flow: sparse reward becomes shaped reward through KL penalty, shaped reward becomes advantages through GAE, and advantages become policy gradients through the clipped surrogate objective. Each step has a clear purpose, and together they produce a training signal that is both informative and stable.