Counterfactual Credit Assignment in Multi-Agent RL: A Worked Derivation
A team wins. The shared reward is +1. Every agent's gradient gets the same number, so the agent who stood still learns exactly as much as the agent who…

Key topics
A team wins. The shared reward is +1. Every agent's gradient gets the same number, so the agent who stood still learns exactly as much as the agent who saved the episode.
That is the failure this article fixes. Attribution is not a property of the outcome; it is a property of a comparison. Our job is to make the comparison formal, name what it holds fixed, and find where it stops being trustworthy.
If you have not yet built the intuition for why shared rewards create an attribution problem, start with the credit assignment concept piece first. Here we assume you already know the problem exists and want the machinery.
Why the Shared Return Cannot Attribute Anything
Set up the standard cooperative case. There are agents. At each step the environment is in state , the agents choose a joint action , and the environment returns a single scalar reward shared by everyone. Over an episode, the team collects one return .
Now look at the policy-gradient weight. For a joint policy , the update for agent 's parameters is proportional to
The term is identical for every agent. It does not depend on . So the update tells agent "that was good" or "that was bad" without ever telling it how much of it was you. With one team return, the gradient carries zero information about relative contribution.
The missing object is a per-agent signal that isolates one agent's effect while everything else is held constant. Two families of methods produce that signal, and they are not the same comparison. A counterfactual baseline resamples one agent's action while holding the rest of the joint action fixed. A difference reward removes the agent and compares the team outcome with and without it. Both are counterfactual, but they hold different things fixed and answer different questions. The derivation below is the action-resampling baseline; the removal form appears later as a contrast.
Knowledge check
Check your understanding
Answer this question before you continue.
Notation and the Joint-Action Setup
Define the symbols before we use them.
- : the state.
- : the joint action. Agent 's own action is .
- : the joint action of every agent except . So .
- : the joint action-value — expected return from when the team takes joint action and then follows the joint policy.
- : the joint policy, factored into per-agent policies over each agent's action-observation history .
- : the realized team return for the episode actually run.
- : the counterfactual return obtained when agent 's contribution is removed.
The marginal-contribution signal is the difference:
Read the sign carefully, because it is a real learning signal and not a bug:
- : agent improved the outcome.
- : agent was redundant — the team would have done about as well without it.
- : agent made things worse.
That last case is the one people flinch at. A negative is not an error; it is the environment telling agent that its action cost the team, and it should be pushed away from.
The Counterfactual Baseline, Derived Step by Step
The marginal-contribution form asks "what if we removed this agent?" The baseline form asks a slightly different question: "what if this agent had chosen differently?" Both start from the same joint action-value.
Start with , the value of the joint action the team actually took. We want to know what agent 's action was worth relative to its alternatives. So define the counterfactual baseline:
Read this slowly. We hold the state and the other agents' actions fixed. We let agent 's action range over every alternative , weight each by how likely agent 's own policy was to pick it, and average the resulting joint values. The baseline is the value agent would expect on average, given the rest of the team's behavior.
The per-agent advantage is then
This is the signal that replaces the shared return. It measures how much better the realized joint action was than agent 's average alternative.
Now the step that makes this a baseline and not an arbitrary subtraction. In the policy-gradient estimator, we replace with :
Subtracting does not change the expected gradient, provided does not depend on the sampled action in the current rollout. Here is why. The expectation of the score term is zero:
because is constant with respect to the sampled and the score function integrates to zero over the policy. So we can subtract it for free — in expectation. What we buy is variance reduction: the multiplier is centered around zero instead of being a raw positive return, so updates for redundant agents shrink toward nothing.
Note: The independence condition is the load-bearing assumption. The baseline must be measurable with respect to randomness external to agent 's current action. If the baseline secretly depends on the action agent just took, the subtraction is no longer free and you have introduced bias.
Knowledge check
Check your understanding
Answer this question before you continue.
What Is Actually Held Fixed
This is the most misread part of the method, so make the conditioning explicit.
Held fixed:
- the state ,
- the other agents' actions ,
- the environment dynamics,
- the reward function.
Not held fixed:
- agent 's action is resampled,
- everything downstream of that action is allowed to change.
The consequence is precise. The comparison answers "what would this rollout look like if agent had chosen differently?" It does not answer "what would the team score be without agent ?" Those are different questions with different answers.
The marginal-contribution form () removes the agent. The baseline form resamples the agent's action. Removing an agent changes the team's composition; resampling its action changes only its choice. When you want to know whether a role was worth having, ask the removal question. When you want to know whether a specific decision was good, ask the resampling question.
Picture a two-branch tree growing from a shared state. The trunk holds , , the dynamics, and the reward. The two branches are the realized joint action and the counterfactual. Everything above the fork is shared; everything below it is what you are comparing.
Knowledge check
Check your understanding
Answer this question before you continue.
A Worked Example With Numbers
Take two agents, each with two actions , and a joint reward that depends on both. Suppose the current policy is uniform, so for each action. The realized joint action is , and the critic gives us these joint values:
| Joint action | |
|---|---|
| 2 | |
| 3 | |
| 4 | |
| 6 |
Agent 1's advantage. Hold agent 2's action at . Agent 1's alternatives are and :
Agent 2's advantage. Hold agent 1's action at . Agent 2's alternatives are and :
Both advantages are positive, and they do not sum to the team advantage. That is the point where intuition usually breaks. There is no conservation law here: the advantages are two separate comparisons against two separate baselines, not a decomposition of one team quantity.
Now change the table so the same team outcome splits the agents' signals. Keep , but set and . Recompute:
Same team outcome, opposite signs. Agent 1's action was worse than its average alternative; agent 2's was better. The shared return of 6 could never have told you that.
Knowledge check
Check your understanding
Answer this question before you continue.
From Signal to Update
The per-agent update substitutes for the shared return:
The practical consequence is the whole reason to do this. An agent that was redundant in a rollout gets , so its update is near zero. It stops being reinforced for free-riding on teammates who did the work. An agent that hurt the team gets a negative multiplier and is pushed away from that action.
The cost is that computing requires a critic that can evaluate joint actions the team never actually took. That is why the method is normally paired with centralized training: the critic sees the full joint action during learning, even though each agent acts on local information at execution.
Where the Attribution Breaks
The signal is only as good as the counterfactual it is built on. Three assumptions carry the weight.
The critic must generalize to joint actions never taken. The baseline is an expectation over agent 's alternatives, and most of those alternatives were never sampled. If the critic cannot estimate their values, the baseline is a guess dressed as an expectation. This is the most common silent failure: the arithmetic is correct and the attribution is fiction.
The counterfactual must be well-defined. In tightly coupled tasks, resampling one agent's action can produce a state the environment never reaches, or a joint action with no coherent meaning. If the counterfactual is not a real possibility, the comparison is comparing nothing.
The signal attributes marginal, not causal, contribution. Correlated agents can each look redundant while the pair is essential. If agent 1 and agent 2 always act together, then holding one fixed while resampling the other will show near-zero advantage for both — even though removing both would collapse the team. Marginal contribution is not causal contribution.
Two more failure modes worth naming. When the action space is large, the expectation over is estimated from few samples and the baseline becomes high-variance. And when contributions are genuinely non-separable, a single-agent baseline will misattribute; multi-level or subset-based formulations that reason over groups of agents are the honest answer there.
When to Use a Counterfactual Baseline
The decision rule is short.
Use it when roles are distinguishable and the joint action-value is learnable. The comparison is then meaningful and cheap. Use it when the team reward is sparse and the shared-return gradient is too coarse to separate agents.
Do not use it when the critic cannot evaluate counterfactual joint actions — you will get confident, wrong attribution. Do not use it when the action space makes the expectation too noisy to estimate. And do not use it as a substitute for value decomposition when the real requirement is decentralized greedy action selection; that is a different constraint with a different solution.
| Method | What it holds fixed | What it requires | What it cannot represent |
|---|---|---|---|
| Counterfactual baseline | State, other agents' actions, dynamics, reward | A critic that generalizes to unsampled joint actions | Non-separable contributions; large action spaces cheaply |
| Value decomposition | A structural constraint linking per-agent utilities to team value | A mixing function with the right monotonicity or factorization property | Joint values the constraint excludes |
| Difference rewards | The rest of the team's behavior, with the agent removed | A well-defined counterfactual removal | Correlated agents that are jointly essential |
The three are not competitors so much as answers to different questions. Pick the one whose held-fixed set matches the question you actually need answered.
The Next Move
Attribution is a comparison, and the comparison is only as good as the critic that evaluates the counterfactual. That single sentence is the whole article.
So do not stop at reading the derivation. Take the four-cell table above, change one number in it, and recompute both advantages. Watch them flip sign. Then change the reward coupling so the two agents' contributions become correlated, and watch the baseline report both as redundant even though the pair is essential. That is the boundary of the method, and you will understand it far better from the arithmetic than from any paragraph describing it.
Run the numbers. Change the coupling. Watch the signal betray the assumption.
Knowledge check
Final check
Finish the article by checking the ideas you just learned.
References
Build a stronger RL mental model
Use structured resources to connect equations, algorithms, and observable agent behavior.


