Value gradients and costates
In reinforcement learning, the reward for a decision often arrives many steps after the decision is made. Capturing this relation helps us improve the decisions that lead to higher rewards. Policy-gradient methods such as REINFORCE implement this by sampling trajectories and making the decisions in high-return trajectories more likely (Williams, 1992). They rely on the statistical assumption that high-valued decisions are often found in high-return trajectories. Hence, these methods are intrinsically model-free. Although model-free algorithms have certain merits, it remains an open question how we can leverage a model of the environment, if we have one. Can we use the model to find the associations between past decisions and future rewards? In this post, we will cover a method called value gradients, a model-based approach that answers this question.
We begin with a simple, interactive 2D environment that helps us visually compare the two methods and, hopefully, develop insight into them. We then cover the derivations of vanilla REINFORCE and the value-gradient method. Before we begin, if you need a refresher on policy gradients, see this excellent blog post by Lilian Weng or Spinning Up’s introduction to policy optimization. For a broader picture, the survey by Mohamed et al. (Mohamed et al., 2020) compares score-function and pathwise estimators (the mechanism behind value gradients) well beyond reinforcement learning.
The environment
Consider the 2D system whose drift is shown by the streamlines below. In this environment, we choose an initial state and let the system evolve for 64 noisy steps following the drift. A trajectory receives a reward of +1 if it ends on the right side, and 0 otherwise. The initial state is our only decision, and the reward arrives only after all 64 steps. The natural question is in which direction we should move the starting point to obtain high-return trajectories (here, the reward and the return are the same).
The reward is delayed and sparse, which gives the policy-gradient estimate a high variance. The initial decision (the location of the initial state) passes through many random transitions before a single delayed reward is observed. We represent the policy by this single decision, and the gradient with respect to this decision becomes the policy gradient.
Try dragging the initial point and pressing Resample a few times. You will see that the thick dashed red curve changes with each resample. This curve shows how the starting point moves under successive gradient-ascent steps following the gradient estimate of REINFORCE, where at each step we resample a new set of trajectories to compute the gradient. The solid blue curve follows the value-gradient estimate from the same starting point in the same way.
At this point, you might have noticed that if every sampled trajectory ends in the red region (i.e., zero reward), the REINFORCE estimate is exactly zero. If every trajectory ends in the blue region, every return is one, and the estimate is a plain average of scores. These scores have mean zero, and their average over a finite batch is pure noise. In both cases, the batch contains no contrast between good and bad runs, and REINFORCE learns from exactly that contrast. The value-gradient estimate is near zero in both cases as well, and rightly so.
The main message is that having access to a model (here, the exact model) provides us with higher-quality gradients and hence better decisions. Value gradients are one way to leverage a model to produce better gradient estimates. This contrasts with, for instance, using the model to simulate trajectories and then obtaining gradient estimates from them, as is more common in the literature. As we shall see later, value gradients can also be applied to approximate models (Heess et al., 2015), which makes them practical.
Preliminaries
To explain the two curves, we leave the environment for a while and turn to the general problem it was built from. Here, we introduce some of the notation and the assumptions we will use throughout.
As usual in reinforcement learning (perhaps a little predictably), let’s consider a Markov decision process (MDP) with states , actions , rewards , and transitions . Starting from , a policy generates a trajectory . For a discount factor , the goal is to find a policy that maximizes the expected cumulative reward.
In this problem, the policy influences the objective through the action distribution . Intuitively, we look for a policy that puts higher probability on actions that lead to better trajectories. We measure the quality of a trajectory by its return, defined as
The expected return from a given initial state defines the value function . Averaging this value over gives the objective function of the policy-optimization problem. Although we define the objective as the expectation of the value function, we will not need to realize a value function explicitly.
For a parameterized policy , write this objective as . Following a gradient-ascent scheme, we will need to compute the gradient of this objective. We will compare two approaches to this computation. Intuitively, one approach updates to make actions in high-return trajectories more likely. The other method will update the parameters to make actions yield a higher return under the same trajectory. The former is the intuition behind REINFORCE, while the latter is the intuition behind value-gradient methods. This distinction may feel subtle at first, but it will become clearer as we derive the two gradients. In the environment, the initial state plays the role of , and no actions occur between the decision and the reward.
REINFORCE
To derive the gradient estimated by REINFORCE, we start by differentiating the objective, which is given by
The parameter appears only in the distribution over trajectories. We therefore cannot move the gradient inside the expectation as it stands, because the return of a fixed trajectory does not depend on and its derivative is zero. The log-derivative trick resolves this by rewriting the derivative of a distribution as an expectation under that distribution.
For REINFORCE, we apply the log-trick with and . Assuming the reward function has no direct dependence on , the return of a fixed trajectory is independent of . Thus, we obtain
It remains to compute the trajectory log likelihood. Assuming that the initial-state distribution and transitions do not depend directly on , the log trajectory density factors as
Further assuming that does not depend on , the transitions do not contribute to the derivative. Hence, we obtain the gradient
In the equation above, only the policy factors contribute to the derivative. This is the gradient estimated by REINFORCE (Williams, 1992).
We estimate the expectation by averaging return-weighted scores over sampled trajectories. Recall that in the 2D environment, we sample several trajectories and use them to compute the gradient estimates. Intuitively, the derivative acts on the log likelihood, while the return simply weights the gradients. In plain words, REINFORCE makes the sampled actions more likely in proportion to the return of their trajectory.
This is, however, the most naive form of REINFORCE. In practice, we reduce the variance using control variates, or simply a value function as a baseline. We use the plain version throughout to keep the comparison simple. For more details, Spinning Up’s section on baselines walks through both.
Value gradients
Let’s return to the same objective again and reconsider the assumptions we made in REINFORCE. Previously, we used the log-derivative trick to express the gradient of the objective as an expectation of return-weighted scores. That route never looks inside the dynamics. When a differentiable model of the dynamics is available, we can obtain the same gradient with an alternative derivation. We will then show that the estimator of this derivation is the value-gradient estimator.
Let’s consider again the derivative of the objective, given by
Again, the parameter appears only in the distribution over trajectories. With a model, however, we can ask how the sampled trajectory itself would change if the parameters changed. In fact, as the parameters change, so do the policy and hence the sampled actions and states. However, this dependency passes through the sampling process, which is not differentiable as written, and we thus rely on another trick.
Reparameterization puts the parameter dependence inside the expectation, through the function . The noise distribution stays fixed as changes. If and are differentiable, we can move differentiation inside the expectation, and we obtain
To apply reparameterization to a trajectory, we need representations of both the policy and the transitions. Assume that there exist deterministic and differentiable functions and satisfying
where the noise variables and are independent across steps and of each other, with fixed distributions independent of the states, actions, and parameters. These representations let us rewrite the objective as
where collects the initial state and all noise variables and for . For fixed , define recursively by
We hold the initial state and noise variables in fixed and generate the actions, later states, and rewards using this recursion.
We can now differentiate the rewritten objective. Since the distribution of is independent of , we can move the derivative inside the expectation, provided the sampled return is differentiable and suitable integrability conditions hold.
To estimate this gradient, we sample and differentiate the return through the recursion. As we vary , we change the actions through and propagate these changes through to later states and rewards. We then average the return derivatives to obtain a pathwise gradient estimator.
Estimator
Both REINFORCE and value gradients express as an expectation of gradients. To turn either expression into an estimator, we replace the expectation with an average over samples. In REINFORCE, the expectation is over the trajectory distribution, and we can thus average the return-weighted scores of sampled trajectories. In value gradients, however, the expectation is over the noise , and the estimator needs samples of the noise, from which we then compute the trajectories and their return derivatives. This is impractical, since in most reinforcement-learning settings we observe only sampled trajectories and not the noise that generated them. We now introduce a way to use sampled trajectories in the value-gradient estimator, by inferring the noise that generated them.
Suppose we have a set of trajectories sampled under the policy and the transition model . The noise of a step is simplest to infer when the observed action and next state determine it uniquely, and we restrict ourselves to this case. We assume that is invertible for each state and denote its inverse by . Similarly, we assume that is invertible for each pair and denote its inverse by . For each observed transition , we then infer the noise by
In words, is the noise that makes the policy output the observed action, and is the noise that makes the model produce the observed next state.
We repeat this calculation at every step and collect the inferred noise, together with the initial state, into for trajectory . At the parameters that generated the data, the reparameterized trajectory reproduces the observed trajectory, and as changes with fixed, it shows how this trajectory would change. The gradient is thus a sample of the pathwise gradient derived above, evaluated along an observed trajectory. When the noise was recorded during sampling, we use it as directly.
Once the trajectories are reparameterized with the inferred noise, we can compute the gradient with respect to the policy parameters by simply running backpropagation. For a visual reference, see the computational graph above and switch on Compute gradient. Averaging across the sampled trajectories, we obtain the value-gradient estimate.
Costates
We can also build a recursion, very similar to the Bellman recursion, but this time over the gradient flow. Recall that . Although our objective is to obtain , let us first introduce , which we call the costate. With the policy-induced dynamics and reward , where , we then obtain the recursion
Using the costates, we can then compute the gradient with respect to the policy parameters by
We can develop an interesting insight by interpreting the individual dimensions of the costate. Since costates carry sensitivity information, each feature (or dimension) of the costate can be seen as a marginal feature value. That is, each dimension of the costate tells us how influential the corresponding feature of the state is for the overall value. For example, a large indicates that any decision that increases feature has a high impact on the value.
Where this leads
In my opinion, value gradients and the costate framework offer an exciting direction in model-based reinforcement learning. They provide an elegant way to leverage a differentiable model for credit assignment, with gradient estimates that do not rely solely on statistical assumptions. If you want to read further, the following list provides some directions and references.
- Learned models. The main attraction of value gradients and costates comes when the model is learned. Stochastic value gradients learn the model from data and use it in the recursions of this post, which gives an elegant model-based reinforcement learning framework with recursions of its own (Heess et al., 2015). Dreamer uses the same mechanism over “dreamed” trajectories in a learned latent space (Hafner et al., 2020). In both, the policy is only as good as the model allows, since a fitted model biases the gradient in a way that more samples cannot fix.
- Language models. Asadulaev et al. use value gradients to explain why critic-free methods such as PPO and GRPO work for language-model post-training (Asadulaev et al., 2026). They show that the backward pass of the actor update propagates costates whose expectation is the value gradient, and that for transformer policies over discrete tokens, backpropagation through attention approximates these costates.
- When pathwise gradients fail. The costate recursion multiplies the Jacobians along a trajectory, and when these Jacobians expand the state, the product grows exponentially with the horizon. In chaotic dynamics, this makes pathwise gradients explode (Metz et al., 2021; Parmas et al., 2018), and with contact or other discontinuities, they can also be biased (Suh et al., 2022).
- The general picture. Mohamed et al. provides an excellent survey of the relationship between score-function, pathwise, and measure-valued estimators across machine learning (Mohamed et al., 2020).