Policy Gradient Methods › Reward-to-Go
Language AI HandbookAll chapters Policy Gradient Methods › Reward-to-GoOne section · Sections in this chapter9 / 14 ↓
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
Another variance reduction technique exploits a basic causal property of sequential decision-making: actions can only affect future rewards, not past ones. A decision made at time cannot retroactively change what happened at times through . Yet in basic REINFORCE, we multiply every token's gradient by the full sequence reward, even though early tokens cannot have influenced any "future" rewards from the perspective of later tokens.
Think of a recipe as an analogy. If a chef makes three decisions (which oil to use, which spice to add, whether to reduce the heat), and the final dish receives a good review, it would be wrong to credit the choice of oil for the flavor benefits that the reduced heat provided. The choice of oil could not have influenced whether the sauce reduced properly, since the oil was chosen before the heat decision. Yet basic REINFORCE does exactly this: it credits the oil choice for the reduced-heat flavor improvement.
Instead of multiplying each token's gradient by the total reward, we can use only the rewards that could have been causally influenced by that action. This is called the reward-to-go formulation. The reward-to-go at timestep sums all rewards from that step forward:
where:
- : reward-to-go from timestep
- : immediate reward received at timestep
- The sum starts at the current timestep and runs to the end of the sequence
This captures only the rewards that could have been influenced by the action at time , ignoring any rewards that occurred before. The policy gradient with reward-to-go is:
where:
- : expectation over sequences sampled from the policy
- : the token generated at timestep
- : the cumulative reward from time onwards (reward-to-go), ignoring past rewards
This does not change the expected gradient (the proof is analogous to the baseline proof: terms involving rewards from before time vanish in expectation because they are independent of the action at time ) but reduces variance by removing terms that add noise without giving useful credit assignment signal.
import torch
def compute_rewards_to_go(rewards, gamma=1.0):
"""
Compute reward-to-go for each timestep.
Args:
rewards: List of rewards at each timestep
gamma: Discount factor (1.0 = no discounting)
Returns:
Tensor of rewards-to-go
"""
T = len(rewards)
rewards_to_go = torch.zeros(T)
# Work backwards from the end of the sequence
running_sum = 0
for t in reversed(range(T)):
running_sum = rewards[t] + gamma * running_sum
rewards_to_go[t] = running_sum
return rewards_to_goThe backward pass through the sequence is computationally efficient. We start from the last timestep (where reward-to-go equals just the immediate reward) and accumulate backward. The discount factor down-weights rewards that are further in the future, introducing a form of temporal discounting. In standard RLHF with a single end-of-sequence reward, is common since all rewards are already at the end.
# Example: sequence with only a final reward
intermediate_rewards = [0, 0, 0, 0, 1.0] # Only final reward is non-zero
rewards_to_go = compute_rewards_to_go(intermediate_rewards)Timestep | Immediate Reward | Reward-to-Go
---------------------------------------------
0 | 0.0 | 1.0
1 | 0.0 | 1.0
2 | 0.0 | 1.0
3 | 0.0 | 1.0
4 | 1.0 | 1.0In this example with only a final reward, reward-to-go equals the final reward at all timesteps. This makes sense: from every position in the sequence, the only future reward available is the one at the end. The real benefit of reward-to-go emerges when we have intermediate rewards, such as the per-token KL divergence penalty that RLHF systems often add.


In the typical RLHF setup where reward is only given at the end, reward-to-go equals the final reward at all timesteps. However, this structure becomes important when we add auxiliary rewards or penalties, such as the KL divergence penalty we will introduce in subsequent chapters. That penalty applies at each token, creating a richer reward structure where reward-to-go provides meaningful variance reduction by correctly attributing per-token contributions to the overall gradient.
Combining Baselines and Reward-to-Go
The two techniques can be combined. We can use a per-token value function as a baseline for each timestep, subtracting it from the reward-to-go at that position:
where:
- : the per-token advantage at timestep
- : the reward-to-go from timestep
- : the value function predicting expected future reward from state
This per-token advantage is what actor-critic algorithms compute. The actor is the policy network being trained; the critic is the value function giving the baseline. By training the critic to accurately predict expected future rewards, we get better baselines, which gives us lower-variance advantage estimates, which gives us more stable policy updates.