KL penalties and reference models

A KL penalty in LLM reinforcement learning is a term that discourages the policy πθ\pi_\theta from drifting away from a fixed reference model πref\pi_{\text{ref}}, usually the checkpoint the run started from. It adds β DKL(πθ ∥ πref)\beta\, D_{\mathrm{KL}}(\pi_\theta \,\|\, \pi_{\text{ref}}) to the loss, or subtracts the equivalent from the reward. The coefficient β\beta sets how much reward the policy gives up to stay close, and so how far an agent can move from its starting behavior to learn a new skill.

Two different anchors

Two mechanisms in policy optimization both measure how far a policy has moved, and they are often confused.

Reference-model KL penaltyBehavior-policy trust region
Compared againstA fixed model, often the initial checkpointThe policy that sampled this batch
Moves during the runNoYes, every step
Question it answersHow far from the starting model is the policy allowed to go overall?How far can one update trust this batch?
Typical formβ DKL(πθ∥πref)\beta\,D_{\mathrm{KL}}(\pi_\theta\|\pi_{\text{ref}})PPO clipping, a ratio or probability mask, or a KL to μ\mu

The trust region is a statistical device for off-policy data: the closer the batch is to the current policy, the further one update can safely move. The reference KL is a budget on total distribution shift, and it holds even when every rollout is perfectly on-policy. A penalty on log⁡2(πθ/μ)\log^2(\pi_\theta/\mu) toward the behavior policy is still a trust region, even though it looks like a KL term.

Why a reference penalty exists

The reference penalty came from RLHF against learned reward models. A reward model is only accurate near the distribution it was trained on. Far from it, the policy can find outputs the reward model scores highly but humans would not, which is reward hacking. Keeping the policy near πref\pi_{\text{ref}} keeps it where the reward is trustworthy, and it helps preserve fluency and general capabilities that the reward does not measure. Ziegler et al. and InstructGPT both train against a reward with βlog⁡(πθ/πref)\beta \log(\pi_\theta/\pi_{\text{ref}}) subtracted.

With verifiable rewards, the argument weakens. A unit test does not become less accurate as the policy moves, and the goal is often for the policy to move a long way. Dr. GRPO sets β=0\beta = 0 on the grounds that rule-based verifiers remove the distribution-shift concern, and DAPO and CISPO also drop the term. Dropping it also removes the cost of running a reference model forward on every batch. On-policy RL also stays near its starting model in KL even with β=0\beta = 0 (forgetting), so the explicit anchor is partly redundant.

Estimating KL from samples: k1, k2, k3

The exact KL sums over the whole vocabulary at every position. Most implementations instead estimate it from the sampled tokens alone, using only two log-probabilities per token. John Schulman's note on approximating KL names three estimators. For DKL(πθ∥πref)D_{\mathrm{KL}}(\pi_\theta\|\pi_{\text{ref}}) with tokens sampled from πθ\pi_\theta, let

r=πref(yt)πθ(yt).r = \frac{\pi_{\text{ref}}(y_t)}{\pi_\theta(y_t)}.
EstimatorFormulaBiasNotes
k1k_1−log⁡r-\log runbiasedHigh variance, negative for many samples
k2k_212(log⁡r)2\tfrac12(\log r)^2biased, usually slightlyAlways nonnegative, low variance
k3k_3(r−1)−log⁡r(r - 1) - \log runbiasedAlways nonnegative, low variance

k3k_3 adds r−1r - 1, which has expectation zero under πθ\pi_\theta, to k1k_1. That keeps it unbiased while making every sample nonnegative. The zero expectation needs πref\pi_{\text{ref}} to put no probability on tokens πθ\pi_\theta can never sample; two full softmaxes satisfy this.

A worked example with two sampled tokens:

Tokenπθ\pi_\thetaπref\pi_{\text{ref}}rrk1k_1k2k_2k3k_3
a0.50.20.40.9160.4200.316
b0.30.62.0−0.6930.2400.307

Token b is one the policy has made less likely than the reference did. k1k_1 scores it as negative divergence, which is correct on average across samples but noisy for any one token. k2k_2 and k3k_3 report both tokens as drift.

In-reward versus in-loss placement

In the reward. Classic RLHF with PPO subtracts βlog⁡(πθ/πref)\beta \log(\pi_\theta/\pi_{\text{ref}}), which is k1k_1, from the per-token reward. The penalty then flows through returns and advantages like any other reward, and the policy-gradient machinery turns it into a correct gradient of the KL-regularized objective. The KL value itself is treated as a constant, never differentiated.

In the loss. GRPO adds β k3\beta\, k_3 per token directly to the loss, with β=0.04\beta = 0.04 in DeepSeekMath, and backpropagates through it. The paper's reason is to keep the KL out of the advantage calculation.

These are not interchangeable, because the gradient of an unbiased estimate is not necessarily an unbiased estimate of the gradient. For a single sampled token, ignoring later positions:

  • The gradient of k1k_1 in the loss has zero expectation, so it does nothing on average.
  • The gradient of k3k_3 in the loss is (1−r)∇θlog⁡πθ(1-r)\nabla_\theta\log\pi_\theta, whose expectation under πθ\pi_\theta is the gradient of the forward KL DKL(πref∥πθ)D_{\mathrm{KL}}(\pi_{\text{ref}}\|\pi_\theta), not the reverse KL the objective states.
  • The gradient of k2k_2 in the loss matches the gradient of the reverse KL.

Tang and Munos analyze these pitfalls, including a second one: in-loss penalties at each token ignore how that token changes the contexts of later tokens, which yields only a partial gradient. In-reward placement avoids both, at the cost of mixing the KL into the reward signal.

When to keep it

A reference KL is worth its extra forward pass when the reward is a learned model or a judge that can be exploited, when retaining broad behavior is an explicit goal, or when a run is unstable in ways that point to drift. It is usually unnecessary when rewards are verifiable and the task is the only objective.

Long runs that keep the penalty meet a second problem. As the policy improves, the KL term grows until it dominates the loss and updates shrink. ProRL handles this with reference resets: when validation performance stalls or degrades, it hard-resets πref\pi_{\text{ref}} to a recent snapshot of the policy and reinitializes the optimizer state. The penalty then bounds drift within each training stage instead of across the whole run.