4.4.3 · D1Alignment, Prompting & RAG

Foundations — Proximal Policy Optimization for LLMs

3,188 words14 min readBack to topic

This page assumes nothing. If the parent note Proximal Policy Optimization for LLMs used a symbol, we build it here from a picture first. Read top to bottom — each item is a brick for the next.


The cast of characters, in build order

1. A "token" — the atom of text

A language model does not write whole sentences at once. It writes one piece at a time: a word, or part of a word, called a token. Think of a train being built one carriage at a time.

Figure 1 below shows this: five boxes, one per token, with an arrow marking the next token as the thing we're about to choose.

Figure — Proximal Policy Optimization for LLMs

Why the topic needs it: everything PPO does is decided per token. The reward, the leash, the clip — all act on individual carriages of the train.


2. State and action — the fork in the road

At any moment the model has written some tokens already. That "everything written so far, including the prompt" is the state . The next token it is about to choose is the action .

In Figure 2 the blue road is the state ; the three coloured arrows leaving the white "fork" dot are the possible actions . Picking one action lands you in a new state (old road + one more token) — so states and actions chain into a whole path, called a trajectory .

Why "state" and "action"? These are borrowed from reinforcement learning, where an agent in a state chooses an action. Framing text generation this way is exactly what lets us use RL tools. See Policy Gradient Methods & REINFORCE.


3. The policy — a probability, not a decision

At the fork, the model does not pick one branch with certainty. It assigns a probability to each possible next token. That whole rulebook — "given state , how likely is each action ?" — is the policy, written .

Let us earn every piece of that notation:

Why the topic needs it: PPO's entire job is to adjust so good tokens get higher probability. The policy is the thing being trained.


4. The reward — per-token vs the response score

We want to know: was this good? A reward is a single number saying "how much we liked it". But when is it handed out? This trips people up, so we pin it down.

How partial trajectories are handled: because the RM only judges finished text, intermediate tokens get no task reward of their own — only the KL term. Credit for the final score is spread back over earlier tokens through the advantage (§7), not by the reward itself.

Why a learned judge? We cannot differentiate "a human liked this." A learned turns fuzzy human taste into a computable number. The catch, flagged later, is Reward Hacking & Specification Gaming: a learned judge can be fooled.


5. Expectation and the objective

The model is random (it samples tokens), so a single response tells us little. We care about the average score across many samples. That average is written (a fancy "E" for Expectation).

Now we can state precisely what PPO maximizes — the average cumulative reward over a full generation:

Why the topic needs it: is the mountain we climb. Everything after this — gradients, advantage, clipping — is machinery for climbing it safely.


6. The gradient and the policy-gradient estimator

We want to increase . In which direction should we nudge the knobs ? The gradient is an arrow that points in the direction of steepest increase of . Follow it a little, and goes up (Figure 3).

Putting the trick together with the advantage (defined next) gives the estimator every policy-gradient method — and PPO — is built on:

Why calculus here and not guess-and-check? With billions of knobs, random guessing is hopeless. The gradient gives the single best direction to step. Foundations live in Policy Gradient Methods & REINFORCE.


7. Value , action-value , and advantage

Raw reward is noisy. We want a sharper question: was this token better or worse than what we usually expect from here?

  • The value = the average discounted reward we expect from state onward, before choosing an action. It is estimated by a learned network, the critic, whose own knobs we call (Greek "psi") — hence the subscript, to keep it separate from the policy's and the reward model's .
  • The action-value = expected discounted reward if we take action in state , then continue.
  • The advantage = "how much better than average was this action."

Figure 4 draws this: the dashed blue line is the baseline ; each coloured stem is a token's , and the gap between stem and line is its advantage — green above (good), red below (bad), yellow flush (average).

Why subtract (the "baseline")? Subtracting a quantity that doesn't depend on the action leaves the average gradient unchanged but shrinks its jitter enormously. The model learns "better than usual" instead of "big raw number." Practical estimation uses Generalized Advantage Estimation (GAE).


8. The ratio — new-you vs old-you

PPO gathers responses from an old copy of the policy , then updates several times. To keep the maths honest when reusing that old data, we compare the new and old probabilities of the same token as a ratio.

Why a ratio and not a difference? Reusing old samples correctly (called importance sampling) requires reweighting each sample by exactly this ratio of "how likely now" over "how likely then." A difference would not correct the reuse; the ratio does.


9. The clip, , and the surrogate objective

If balloons, one token could yank the whole model. The clip flattens the ratio so it can't leave a safe band .

First, let us nail down exactly which average the below means:

Now assemble the ratio, the advantage, and the clip into the quantity PPO adjusts to make large:

Why clip? Once you've improved a token "enough," clipping makes the gradient go flat there — no reward for pushing further. It's a brake on over-eager moves, applied per token. Concretely: when the term is capped at , so once passes the gradient for that token becomes zero; when it is capped at , limiting how hard a correction is pushed. The makes the choice always the more conservative one.


10. KL divergence, , and the reference — the global leash

The clip is a local guard. Across thousands of tokens the model could still wander far from sensible language. So we add a global leash: a penalty for drifting away from a frozen reference policy .

The distance between two probability rulebooks is measured by the KL divergence. Full theory lives in KL Divergence; here is the piece we need.

Why the leash? The reward model is only accurate near where the reference lives. Stray too far and the judge is fooled (Reward Hacking & Specification Gaming). The KL keeps the model in trustworthy territory. This whole loop — reward model + PPO + KL leash — is Reinforcement Learning from Human Feedback (RLHF). A leash-free alternative is Direct Preference Optimization (DPO).


How the foundations feed the topic

Token = one text atom

State s and Action a

Trajectory tau over T steps

Policy pi_theta gives probabilities

Ratio rho = new over old

Reward model r_phi scores full response

Per token reward r_t terminal

Objective J = discounted sum with gamma

Expectation = average over trajectories

Gradient points uphill

Policy gradient estimator with log trick

Value V_psi and Advantage A

Clip with epsilon builds L_CLIP

PPO clipped objective

KL leash with beta and reference

Every arrow is a "you need this before that." Follow any path top to bottom and you reconstruct the parent note's derivation.


Equipment checklist

Cover the right side and self-test. If any answer surprises you, re-read that section above.

What is a token?
the smallest chunk of text the model emits in one step (roughly a word-piece).
What are the state and action ?
= prompt + tokens so far; = the next token to choose.
What is a trajectory ?
one full generation from prompt to last token.
What does output, and what is ?
a probability of action given state ; = the model's tunable weights.
Why probabilities instead of a hard choice?
they give a continuous surface we can slide with gradients.
When does the reward model score, and how are partial trajectories handled?
it scores the whole finished response once, at ; intermediate tokens get no task reward, only the KL term.
Write the objective .
— expected discounted cumulative reward.
What is and its usual LLM value?
the discount factor in weighting future reward by ; usually in LLM-RLHF.
Write the policy-gradient estimator.
.
What is the advantage and why subtract ?
how much better than average an action was; the baseline (critic knobs ) cuts variance without biasing the gradient.
What is the ratio , why a ratio, and why is its denominator safe?
new prob over old prob for importance sampling; the token was sampled from so its old prob is .
Write and say whether we maximize or minimize it.
; we maximize it (equivalently minimize ).
Write the KL divergence and say what and do.
; = frozen SFT landmark, = leash tightness penalizing drift.