Simple Algorithm
Reinforcement Learning · Fixed Point · 26 min

Temporal Credit Assignment

Which action earned the reward? Long delays, hindsight and return decomposition.

A chess game is lost on move 60 because of a pawn push on move 12. Minsky named the problem in 1961: which of many earlier actions deserves the credit, or the blame, for an outcome? Every method in this topic answers it well for short delays. This lesson measures why long delays defeat TD and Monte Carlo alike, then surveys the research that tries to do better: adapting the dials automatically, learning which earlier steps a reward depends on, carrying value back through memory, and assigning credit in hindsight. Much of it is recent and empirical, and the lesson says what each idea does and does not show.

What you’ll learn

  • Define temporal credit assignment, and quantify how delay slows TD (information moves back about one step per update), how intervening noise slows Monte Carlo, and how discounting hides distant consequences.
  • Explain eligibility traces, λ\lambda and γ\gamma as fixed credit-assignment rules, and how meta-gradients adapt them online.
  • Explain return decomposition, synthetic returns and temporal value transport, and prove that a return-equivalent redistribution of reward leaves optimal policies unchanged.
  • Derive hindsight credit assignment from Bayes' rule, and weigh its benefits against its variance.

Why long delays are hard

Minsky (1961) called it the credit-assignment problem: when success follows a long sequence of decisions, how should credit be divided among them? Temporal credit assignment asks which earlier actions mattered; structural credit assignment asks which parts of a system, such as which weights of a network. Sutton's 1984 thesis, where temporal-difference learning took shape, was titled Temporal Credit Assignment in Reinforcement Learning. Three quantities make long delays hard: how fast information about an outcome travels back, how much unrelated noise piles up on the way, and how much the objective itself discounts distant consequences.

DerivationTD(0) moves information back about one step per update
  1. v1(n)=(1−α) v1(n−1)+α⋅1,vi(n)=(1−α) vi(n−1)+α vi−1(n−1)(i≥2)v_1^{(n)} = (1-\alpha)\,v_1^{(n-1)} + \alpha\cdot 1,\qquad v_i^{(n)} = (1-\alpha)\,v_i^{(n-1)} + \alpha\,v_{i-1}^{(n-1)}\quad(i \ge 2)

    Take a deterministic chain whose only reward, 1, comes on leaving its last state, with all values starting at 0, γ=1\gamma = 1 and step α\alpha. Number the states by their distance ii from the end. Each episode visits i=k,k−1,…,1i = k, k-1, \dots, 1 in that order, so state ii is updated before its successor and bootstraps from the successor's value of the previous episode:

  2. vi(n)=Pr⁡(Binomial(n,α)≥i)v_i^{(n)} = \Pr\big(\text{Binomial}(n,\alpha) \ge i\big)

    By induction on nn, using Pr⁡(Xn≥i)=(1−α)Pr⁡(Xn−1≥i)+αPr⁡(Xn−1≥i−1)\Pr(X_n \ge i) = (1-\alpha)\Pr(X_{n-1} \ge i) + \alpha\Pr(X_{n-1} \ge i - 1) for Xn∼Binomial(n,α)X_n \sim \text{Binomial}(n, \alpha), the solution is a binomial tail:

  3. The reward's influence moves back like a counter that advances with probability α\alpha per episode: the state kk steps from the reward needs about k/αk/\alpha episodes, and with α=1\alpha = 1 exactly kk. The delay costs time linearly, and in a stochastic environment every one of those steps also passes on the noise of the estimates it bootstraps from. Traces and n-step returns shortcut the chain; Monte Carlo does not need it at all.

Predict before you look

In that chain, with α=0.5\alpha = 0.5, what is the value of the state 10 steps before the reward after 20 episodes? (Its true value is 1.)

Show the answer

0.59

Pr⁡(Binomial(20,0.5)≥10)≈0.588\Pr(\text{Binomial}(20, 0.5) \ge 10) \approx 0.588. After 40 episodes it is above 0.999. With α=0.1\alpha = 0.1 the same state needs about 100 episodes to reach one half (0.55 after 100 episodes), in a world with no noise at all.

Monte Carlo has no propagation delay, but it pays in variance. If kk steps separate an action from its consequence, and each intervening step adds independent reward noise of variance σ2\sigma^2, the return's variance grows by kσ2k\sigma^2, while the action's effect Δ\Delta on the return stays fixed. The number of episodes needed to see the effect at a given confidence grows like kσ2/Δ2k\sigma^2/\Delta^2: linearly in the length of the distraction.

Discounting hides the consequence altogether. An effect kk steps away enters the discounted objective with weight γk\gamma^k; with γ=0.99\gamma = 0.99 and k=500k = 500 that is about 0.00660.0066. An agent that optimizes the discounted return barely cares about the consequence, and its estimates of it are buried under the noise of everything nearer.

Traces, λ and γ as hand-made credit rules

An eligibility trace is a fixed credit-assignment rule: a TD error at time kk credits the visit at time t≤kt \le k with weight (γλ)k−t(\gamma\lambda)^{k-t}, by recency, and with accumulating traces also by frequency. It knows nothing about which earlier step mattered. A key picked up 500 steps before the door it opens fades exactly like every irrelevant step in between. λ\lambda trades bias against variance, and γ\gamma plays two roles at once: it defines the objective, and it limits how far back credit can usefully flow.

Meta-gradients (Xu, van Hasselt & Silver, 2018) stop choosing these by hand. Let η=(γ,λ)\eta = (\gamma, \lambda) parameterize the return the agent learns from. After an update θ′=θ+f(τ,θ,η)\boldsymbol\theta' = \boldsymbol\theta + f(\tau, \boldsymbol\theta, \eta) on experience τ\tau, the agent measures the result on fresh experience τ′\tau' with an objective J′J' that uses fixed reference parameters, and differentiates through the update:

∂J′(τ′,θ′,ηˉ)∂η=∂J′(τ′,θ′,ηˉ)∂θ′  dθ′dη.\frac{\partial J'(\tau', \boldsymbol\theta', \bar\eta)}{\partial\eta} = \frac{\partial J'(\tau', \boldsymbol\theta', \bar\eta)}{\partial\boldsymbol\theta'}\;\frac{d\boldsymbol\theta'}{d\eta}.

Gradient steps on η\eta adapt the return online. On 57 Atari games over 200 million frames, the authors reported a new state-of-the-art score at the time. The rule is still geometric, though: meta-gradients tune how fast credit fades, not where it goes.

Check your understanding

An agent picks up a key, wanders through 300 steps of unrelated activity, then opens a door with it for +1+1. Using TD(λ) with γλ=0.99\gamma\lambda = 0.99, about how much of the door's TD error reaches the key pickup through the trace?

  1. About 5%, the same fraction that reaches any step 300 steps back, relevant or not
  2. All of it, because the key caused the reward
  3. None, because traces only reach the previous step
  4. About 99%, because γλ\gamma\lambda is close to 1
Show the answer

A. About 5%, the same fraction that reaches any step 300 steps back, relevant or not

0.99300≈0.0490.99^{300} \approx 0.049. The trace weight depends only on elapsed time. The key pickup gets the same small share as any other step 300 steps back, and the irrelevant steps in between get larger shares. Learned decompositions and hindsight methods try to direct credit to the key instead.

Learning where credit belongs

Return decomposition. RUDDER (Arjona-Medina, Gillhofer, Widrich, Unterthiner, Brandstetter & Hochreiter, 2019) learns where an episode's reward comes from. A sequence model gg (an LSTM in the paper) is trained to predict the episode's return from each prefix τ0:t=(S0,A0,…,St,At)\tau_{0:t} = (S_0, A_0, \dots, S_t, A_t) of its trajectory, and the change in its prediction becomes a new reward,

R~t+1=g(τ0:t)−g(τ0:t−1).\tilde R_{t+1} = g(\tau_{0:t}) - g(\tau_{0:t-1}).

A step that makes the predicted return jump, such as picking up the key, receives the credit at once, however long before the door it happens. (This difference of predictions is the simplest of the contribution analyses the authors use.) The return decomposition is safe for the reason the derivation shows.

DerivationRedistribution that keeps optimal policies (undiscounted episodes, as in RUDDER)
  1. ∑t=0T−1R~t+1=g(τ0:T−1)−g∅\sum_{t=0}^{T-1}\tilde R_{t+1} = g(\tau_{0:T-1}) - g_\varnothing

    The redistributed rewards of an episode telescope; g(τ0:−1)g(\tau_{0:-1}), the prediction before any step, is a constant g∅g_\varnothing:

  2. J~(π)=J(π)−g∅for every policy π\tilde J(\pi) = J(\pi) - g_\varnothing\quad\text{for every policy }\pi

    If the prediction is exact once the whole trajectory is known, g(τ0:T−1)=G0g(\tau_{0:T-1}) = G_0, every episode's new return is its old return minus the same constant, and so is every policy's expected return. The ordering of policies, and with it the set of optimal ones, is unchanged: the problems are return-equivalent. A practical method can enforce exactness by giving the prediction's leftover error to the last step, which leaves that part of the reward delayed.

  3. The payoff: with an ideal redistribution the expected reward still to come after each step is zero, since the prediction already accounts for it. Action values then equal expected immediate redistributed rewards, and learning them needs neither long chains of bootstrapping nor long noisy returns. On artificial tasks with delayed rewards the authors report learning far faster than Monte Carlo, and exponentially faster than TD(λ), MCTS and reward shaping as the delay grows; added to PPO on Atari, RUDDER improved scores, most of all on games with delayed rewards.

Synthetic returns (Raposo et al., 2021) learn associations between states and distant rewards directly: a model explains each reward through learned contributions of the earlier states of the episode, and a state's contribution is then used as an additional, synthetic reward, so ordinary TD learning sees the credit early. The learned contributions spike at states that follow critical actions. An IMPALA-based agent with synthetic returns solved Atari Skiing, a game with a long reward delay that had resisted deep RL agents, by the authors' account "25 times faster" than the published state of the art.

Temporal value transport (Hung et al., 2019) uses memory instead. The agent writes to an external memory and reads it by attention. When, at a later time, a read attends strongly to a memory written at an earlier time, TVT adds the value estimated at the later time, weighted by the attention, to the reward at the earlier time. Value jumps the gap directly, without passing through the discounted, noisy steps in between. In tasks where an early action matters only after a long phase of unrelated rewards, agents with TVT learned what standard agents with discounting could not.

ApproachWhat is learnedHow credit reaches the past
Eligibility tracesnothing: a fixed ruleback in time, fading by γλ\gamma\lambda per step
Meta-gradient γ\gamma, λ\lambda (Xu et al., 2018)the fading ratethe same geometric rule, tuned online
RUDDER (Arjona-Medina et al., 2019)a return predictor ggreward moved to the steps that change the prediction
Synthetic returns (Raposo et al., 2021)contributions of past states to rewardsa synthetic reward at the contributing states
TVT (Hung et al., 2019)a memory read by attentionvalue added to the reward of the recalled time step
Hindsight credit assignment (Harutyunyan et al., 2019)the probability of each action given the outcomeevery outcome reweighted for every action, next step
The first two rows decide credit by elapsed time; the others learn something about which steps an outcome depends on, at the cost of a model that can be wrong.

Hindsight: credit from outcomes

Hindsight credit assignment (Harutyunyan et al., 2019) turns the question around. Instead of asking what an action will lead to, ask, once an outcome is known, how likely each action was to have produced it. Bayes' rule makes this exact.

DerivationAction values from hindsight probabilities
  1. h(a∣s,z)=π(a∣s) p(z∣s,a)p(z∣s)h(a \mid s, z) = \frac{\pi(a \mid s)\,p(z \mid s, a)}{p(z \mid s)}

    Let ZZ be the return of a trajectory that starts in state ss and follows π\pi, first action included, and define the hindsight distribution h(a∣s,z)=Pr⁡(A0=a∣S0=s,Z=z)h(a \mid s, z) = \Pr(A_0 = a \mid S_0 = s, Z = z). By Bayes' rule,

  2. Qπ(s,a)=∑zp(z∣s,a) z=Eτ∼π[h(a∣s,Z)π(a∣s) Z]Q^\pi(s,a) = \sum_zp(z \mid s,a)\,z = \E_{\tau\sim\pi}\left[\frac{h(a \mid s, Z)}{\pi(a \mid s)}\,Z\right]

    Solve for p(z∣s,a)p(z \mid s, a) and average zz: the value of action aa is a reweighting of returns that followed any action:

  3. Qπ(s,a)=r(s,a)+∑k≥1γk Eτ∼π[hk(a∣s,Sk)π(a∣s) Rk+1]Q^\pi(s,a) = r(s,a) + \sum_{k\ge1}\gamma^k\,\E_{\tau\sim\pi}\left[\frac{h_k(a \mid s, S_k)}{\pi(a \mid s)}\,R_{k+1}\right]

    The same argument with the state SkS_k reached kk steps later, using hk(a∣s,s′)=Pr⁡(A0=a∣S0=s,Sk=s′)h_k(a \mid s, s') = \Pr(A_0 = a \mid S_0 = s, S_k = s') and the fact that the reward after SkS_k depends on the first action only through SkS_k when actions follow π\pi, gives the state-conditional form. ■\blacksquare

Check your understanding

In return-conditional hindsight credit assignment, suppose the return ZZ is independent of the first action. What weight h(a∣s,Z)/π(a∣s)h(a \mid s, Z)/\pi(a \mid s) does a return receive, for each action aa?

  1. 0 for every action, so no learning happens
  2. 1 for every action, so every action receives the same value: none gets spurious credit
  3. 1/π(a∣s)1/\pi(a \mid s), the importance ratio
  4. It depends on the action that was actually taken
Show the answer

B. 1 for every action, so every action receives the same value: none gets spurious credit

If ZZ carries no information about A0A_0, then h(a∣s,Z)=π(a∣s)h(a \mid s, Z) = \pi(a \mid s) and every ratio is 1: each action's estimate is the plain average return. Credit flows to an action only to the extent that it made the observed outcome more (ratio above 1) or less (below 1) likely. That is how hindsight methods reduce the noise that irrelevant actions add to credit.

The gains: every trajectory from ss informs the value of every action, and an action is credited in proportion to how much it changed the probability of what happened. The costs: hh must be learned, typically as a classifier that predicts the first action from the state and the outcome, and its errors bias the estimates; and the ratio h/πh/\pi is large wherever π(a∣s)\pi(a \mid s) is small, which can raise the variance. The original paper demonstrated the method on illustrative tasks built to isolate credit-assignment difficulties. Counterfactual credit assignment (Mesnard et al., 2021) carries the idea into policy gradients, with baselines computed from learned summaries of the future and constrained so that the gradient stays unbiased.

Key takeaways

  • Temporal credit assignment asks which earlier actions deserve credit for an outcome (Minsky, 1961). In a chain, TD(0)'s value ii steps from a reward after nn episodes is Pr⁡(Binomial(n,α)≥i)\Pr(\text{Binomial}(n,\alpha) \ge i): information travels back about one step per update.
  • Monte Carlo avoids the propagation delay but its variance grows with every noisy step between action and consequence, and discounting weights a consequence kk steps away by γk\gamma^k.
  • Eligibility traces credit by recency alone; meta-gradients can tune γ\gamma and λ\lambda online, but cannot direct credit to the step that mattered.
  • Return decomposition (RUDDER) moves reward to the steps that change a learned return prediction; an exact redistribution is return-equivalent and keeps the optimal policies. Synthetic returns and temporal value transport learn associations or use memory to carry credit across long gaps.
  • Hindsight credit assignment weights each outcome by h(a∣s,outcome)/π(a∣s)h(a \mid s, \text{outcome})/\pi(a \mid s) for every action, so actions without influence get no spurious credit; its costs are a learned model and possibly large ratios. Benchmarks and guarantees for long-term credit assignment remain open.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In a deterministic chain whose only reward comes kk steps after its first state, TD(0) runs with α=1\alpha = 1 and values starting at 0. After how many episodes does the first state's value first reflect the reward?

  1. 1
  2. kk
  3. k2k^2
  4. Never, since TD(0) bootstraps
Show the answer

B. kk

With α=1\alpha = 1, vi(n)=Pr⁡(Binomial(n,1)≥i)=1[n≥i]v_i^{(n)} = \Pr(\text{Binomial}(n, 1) \ge i) = \mathbb 1[n \ge i]: each episode moves the information back exactly one state, so the state kk steps from the reward first sees it in episode kk. Monte Carlo, or TD(1), would update it after the first episode.

Question 2

Why does Monte Carlo struggle when a long stretch of unrelated, noisy rewards separates an action from its consequence?

  1. Because Monte Carlo must wait for the episode to end
  2. Because Monte Carlo returns are biased by the noise
  3. Because the noise of every intervening reward adds to the return's variance while the action's effect stays the same, so more episodes are needed to detect it
  4. Because Monte Carlo cannot handle discounting
Show the answer

C. Because the noise of every intervening reward adds to the return's variance while the action's effect stays the same, so more episodes are needed to detect it

The return stays unbiased, but its variance grows by about σ2\sigma^2 per noisy step, so the episodes needed to detect an effect Δ\Delta grow like kσ2/Δ2k\sigma^2/\Delta^2. TD reduces the variance by bootstrapping, at the price of the propagation delay; neither solves the problem for very long gaps.

Question 3

RUDDER replaces each reward by the change in a learned prediction of the episode's return, R~t+1=g(τ0:t)−g(τ0:t−1)\tilde R_{t+1} = g(\tau_{0:t}) - g(\tau_{0:t-1}). Why does this leave the optimal policies unchanged, in undiscounted episodic tasks?

  1. Because the redistributed rewards telescope to the episode's return minus a constant, if the final prediction is exact, so every policy's expected return shifts by the same constant
  2. Because the predictor gg is trained on optimal trajectories only
  3. Because the new rewards are potential-based shaping with Φ=g\Phi = g
  4. It does not: redistribution always changes the optimal policy
Show the answer

A. Because the redistributed rewards telescope to the episode's return minus a constant, if the final prediction is exact, so every policy's expected return shifts by the same constant

∑tR~t+1=g(τ0:T−1)−g∅=G0−g∅\sum_t\tilde R_{t+1} = g(\tau_{0:T-1}) - g_\varnothing = G_0 - g_\varnothing when the final prediction equals the return. A shift shared by all policies cannot change which is best. If the final prediction is off, the leftover error must be returned to the episode, for instance at its last step, to keep this guarantee.

Question 4

In return-conditional hindsight credit assignment, how does a sampled return ZZ from state ss enter the estimate of Qπ(s,a)Q^\pi(s,a)?

  1. Only if aa was the action actually taken, with weight 1
  2. With weight π(a∣s)/b(a∣s)\pi(a \mid s)/b(a \mid s), an importance ratio
  3. With weight h(a∣s,Z)/π(a∣s)h(a \mid s, Z)/\pi(a \mid s), for every action aa, whichever was taken
  4. With weight γk\gamma^k, where kk is the length of the episode
Show the answer

C. With weight h(a∣s,Z)/π(a∣s)h(a \mid s, Z)/\pi(a \mid s), for every action aa, whichever was taken

By Bayes' rule, p(z∣s,a)=h(a∣s,z) p(z∣s)/π(a∣s)p(z \mid s,a) = h(a \mid s,z)\,p(z \mid s)/\pi(a \mid s), so Qπ(s,a)=Eτ∼π[(h(a∣s,Z)/π(a∣s))Z]Q^\pi(s,a) = \E_{\tau\sim\pi}[(h(a \mid s,Z)/\pi(a \mid s))Z]. One return informs every action, more for actions that made it more likely.

Further reading

  • M. Minsky, "Steps toward artificial intelligence," Proceedings of the IRE 49(1), 8–30 (1961).
  • J. A. Arjona-Medina, M. Gillhofer, M. Widrich, T. Unterthiner, J. Brandstetter & S. Hochreiter, "RUDDER: Return decomposition for delayed rewards," Advances in Neural Information Processing Systems 32 (2019).
  • A. Harutyunyan, W. Dabney, T. Mesnard, M. Gheshlaghi Azar, B. Piot, N. Heess, H. van Hasselt, G. Wayne, S. Singh, D. Precup & R. Munos, "Hindsight credit assignment," Advances in Neural Information Processing Systems 32 (2019).
  • C.-C. Hung, T. Lillicrap, J. Abramson, Y. Wu, M. Mirza, F. Carnevale, A. Ahuja & G. Wayne, "Optimizing agent behavior over long time scales by transporting value," Nature Communications 10, 5223 (2019).
  • E. Pignatelli, J. Ferret, M. Geist, T. Mesnard, H. van Hasselt, O. Pietquin & L. Toni, "A survey of temporal credit assignment in deep reinforcement learning," arXiv:2312.01072 (2023).