Value gradients and costates

Tolga Ok

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.

Field
Samples
Gradient estimators
Figure 1. Initial-state ascent in a stochastic field. Gray streamlines show the drift, with arrows indicating its direction. Thick dashed red and solid blue curves update the initial state using REINFORCE and value gradients, respectively, with shared sampled innovations. Thin curves preview 64 sampled process trajectories, colored by terminal reward. The neutral dark trajectory is a zero-noise rollout, not an average. Background colors mark terminal reward regions, not attraction basins. Drag the ring or use the arrow keys to change the starting state.

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 sts_t, actions ata_t, rewards rt≔r(st,at)r_t \coloneqq r(s_t,a_t), and transitions p(st+1∣st,at)p(s_{t+1}\mid s_t,a_t). Starting from s0∼p0s_0\sim p_0, a policy π\pi generates a trajectory τ=(s0,a0,r0,s1,a1,… )\tau=(s_0,a_0,r_0,s_1,a_1,\dots). For a discount factor γ∈(0,1)\gamma\in(0,1), the goal is to find a policy that maximizes the expected cumulative reward.

arg max⁡π    Ep0,π,p[∑t≥0γtr(st,at)].\operatorname*{arg\,max}_\pi \;\; \mathbb{E}_{p_0,\pi,p}\left[\sum_{t \geq 0} \gamma^t r(s_t,a_t)\right].

In this problem, the policy π\pi influences the objective through the action distribution π(at∣st)\pi(a_t\mid s_t). 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

G(τ)=∑t≥0γtr(st,at).G(\tau)=\sum_{t \geq 0}\gamma^t r(s_t,a_t).

The expected return from a given initial state defines the value function Vπ(s)=Eπ,p[G(τ)∣s0=s]V_\pi(s)=\mathbb E_{\pi,p}[G(\tau)\mid s_0=s]. Averaging this value over s0∼p0s_0\sim p_0 gives the objective function J(π)=Ep0[Vπ(s0)]J(\pi)=\mathbb E_{p_0}[V_\pi(s_0)] 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 πθ\pi_\theta, write this objective as J(θ)=Ep0,πθ,p[G(τ)]J(\theta)=\mathbb E_{p_0,\pi_\theta,p}[G(\tau)]. 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 θ\theta 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 θ\theta, 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

∇θJ(θ)=∇θEθ[G(τ)].\nabla_\theta J(\theta)=\nabla_\theta\mathbb E_\theta[G(\tau)].

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 θ\theta 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 X=τX=\tau and f(τ)=G(τ)f(\tau)=G(\tau). Assuming the reward function has no direct dependence on θ\theta, the return of a fixed trajectory is independent of θ\theta. Thus, we obtain

∇θJ(θ)=Eθ ⁣[G(τ)∇θlog⁡pθ(τ)].\nabla_\theta J(\theta) =\mathbb E_\theta\!\left[ G(\tau)\nabla_\theta\log p_\theta(\tau) \right].

It remains to compute the trajectory log likelihood. Assuming that the initial-state distribution and transitions do not depend directly on θ\theta, the log trajectory density factors as

log⁡pθ(τ)=log⁡p0(s0)+∑t≥0[log⁡πθ(at∣st)+log⁡p(st+1∣st,at)].\begin{aligned} \log p_\theta(\tau) &=\log p_0(s_0)+\sum_{t \geq 0}\big[\log \pi_\theta(a_t\mid s_t) + \log p(s_{t+1}\mid s_t,a_t)\big]. \end{aligned}

Further assuming that pp does not depend on θ\theta, the transitions do not contribute to the derivative. Hence, we obtain the gradient

∇θJ(θ)=Eθ ⁣[G(τ)∑t≥0∇θlog⁡πθ(at∣st)].\nabla_\theta J(\theta) =\mathbb E_\theta\!\left[ G(\tau)\sum_{t \geq 0}\nabla_\theta\log\pi_\theta(a_t\mid s_t) \right].

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 J(θ)J(\theta) 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

∇θJ(θ)=∇θEθ[G(τ)].\nabla_\theta J(\theta)=\nabla_\theta\mathbb E_\theta[G(\tau)].

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 ff. The noise distribution qq stays fixed as θ\theta changes. If ff and hh are differentiable, we can move differentiation inside the expectation, and we obtain

∇θEX∼pθ[h(X)]=Eϵ∼q ⁣[∇θh(f(θ,ϵ))].\nabla_\theta\mathbb E_{X\sim p_\theta}[h(X)] = \mathbb E_{\epsilon\sim q}\!\left[ \nabla_\theta h(f(\theta,\epsilon)) \right].

To apply reparameterization to a trajectory, we need representations of both the policy and the transitions. Assume that there exist deterministic and differentiable functions μθ\mu_\theta and ff satisfying

at=μθ(st,ηt),whereat∼πθ(⋅∣st),st+1=f(st,at,ζt),wherest+1∼p(⋅∣st,at),\begin{aligned} a_t=\mu_\theta(s_t,\eta_t), \quad &\text{where} \quad a_t \sim \pi_\theta(\cdot\mid s_t), \\ s_{t+1}=f(s_t,a_t,\zeta_t), \quad &\text{where} \quad s_{t+1} \sim p(\cdot\mid s_t,a_t), \end{aligned}

where the noise variables ηt\eta_t and ζt\zeta_t 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

J(θ)=Eθ[G(τ)]=Eε[G(τθ(ε))],J(\theta)=\mathbb E_\theta[G(\tau)] = \mathbb E_\varepsilon[G(\tau_\theta(\varepsilon))],

where ε\varepsilon collects the initial state s0∼p0s_0\sim p_0 and all noise variables ηt\eta_t and ζt\zeta_t for t≥0t \geq 0. For fixed ε\varepsilon, define τθ(ε)=(s0,a0,r0,s1,a1,… )\tau_\theta(\varepsilon)=(s_0,a_0,r_0,s_1,a_1,\dots) recursively by

at=μθ(st,ηt),rt=r(st,at),st+1=f(st,at,ζt),t≥0.a_t=\mu_\theta(s_t,\eta_t), \qquad r_t=r(s_t,a_t), \qquad s_{t+1}=f(s_t,a_t,\zeta_t), \qquad t\geq 0.

We hold the initial state and noise variables in ε\varepsilon fixed and generate the actions, later states, and rewards using this recursion.

Computational graph of a trajectoryA portion of an infinite-horizon trajectory. Dashed edges continue beyond the right edge between state t+3 and action t+3. Each state feeds a circular policy node labeled pi theta, which determines the action. The state and action both determine the next state through the transition model, and both determine the current reward. Noise inputs are omitted. The gradient-flow switch animates return derivatives backward from state t+3. At each step, the local reward contributes to both the action and state derivatives before the policy propagates its contribution to the state. Reduced-motion mode shows static arrows.πθrtatπθrt+1at+1πθrt+2at+2stst+1st+2st+3RewardActionState
Each state feeds the policy, and each state–action pair determines the reward and the next state. The circles represent the policy. Toggle gradient flow to follow return derivatives backward through the graph.

We can now differentiate the rewritten objective. Since the distribution of ε\varepsilon is independent of θ\theta, we can move the derivative inside the expectation, provided the sampled return is differentiable and suitable integrability conditions hold.

∇θJ(θ)=Eε ⁣[∇θG(τθ(ε))].\nabla_\theta J(\theta) = \mathbb E_\varepsilon\!\left[ \nabla_\theta G(\tau_\theta(\varepsilon)) \right].

To estimate this gradient, we sample ε\varepsilon and differentiate the return through the recursion. As we vary θ\theta, we change the actions through μθ\mu_\theta and propagate these changes through ff to later states and rewards. We then average the return derivatives to obtain a pathwise gradient estimator.

Estimator

Both REINFORCE and value gradients express ∇θJ(θ)\nabla_\theta J(\theta) 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 ε\varepsilon, 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 πθ\pi_\theta and the transition model pp. 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 η↦μθ(s,η)\eta\mapsto\mu_\theta(s,\eta) is invertible for each state ss and denote its inverse by μs,θ−1\mu_{s,\theta}^{-1}. Similarly, we assume that ζ↦f(s,a,ζ)\zeta\mapsto f(s,a,\zeta) is invertible for each pair (s,a)(s,a) and denote its inverse by fs,a−1f_{s,a}^{-1}. For each observed transition (s,a,s+)(s,a,s^+), we then infer the noise by

η=μs,θ−1(a),ζ=fs,a−1(s+).\eta=\mu_{s,\theta}^{-1}(a), \qquad \zeta=f_{s,a}^{-1}(s^+).

In words, η\eta is the noise that makes the policy output the observed action, and ζ\zeta 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 εi\varepsilon_i for trajectory ii. At the parameters that generated the data, the reparameterized trajectory τθ(εi)\tau_\theta(\varepsilon_i) reproduces the observed trajectory, and as θ\theta changes with εi\varepsilon_i fixed, it shows how this trajectory would change. The gradient ∇θG(τθ(εi))\nabla_\theta G(\tau_\theta(\varepsilon_i)) is thus a sample of the pathwise gradient Eε[∇θG(τθ(ε))]\mathbb E_\varepsilon[\nabla_\theta G(\tau_\theta(\varepsilon))] derived above, evaluated along an observed trajectory. When the noise was recorded during sampling, we use it as εi\varepsilon_i directly.

Once the trajectories are reparameterized with the inferred noise, we can compute the gradient with respect to the policy parameters θ\theta 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 V(s)=Eε[G(τθ(ε))∣s0=s]V(s)=\mathbb E_\varepsilon[G(\tau_\theta(\varepsilon))\mid s_0=s]. Although our objective is to obtain ∇θJ(θ)\nabla_\theta J(\theta), let us first introduce λ(s)≔∇sV(s)\lambda(s)\coloneqq\nabla_s V(s), which we call the costate. With the policy-induced dynamics fπ(s,ε)=f(s,μθ(s,η),ζ)f^\pi(s,\varepsilon)=f(s,\mu_\theta(s,\eta),\zeta) and reward rπ(s,ε)=r(s,μθ(s,η))r^\pi(s,\varepsilon)=r(s,\mu_\theta(s,\eta)), where ε=(η,ζ)\varepsilon=(\eta,\zeta), we then obtain the recursion

λ(st)=Eεt[∂srπ(st,εt)⊤+γ ∂sfπ(st,εt)⊤λ(st+1)].\lambda(s_t)=\mathbb E_{\varepsilon_t}\big[\partial_s r^\pi(s_t,\varepsilon_t)^\top+\gamma\,\partial_s f^\pi(s_t,\varepsilon_t)^\top\lambda(s_{t+1})\big].

Using the costates, we can then compute the gradient with respect to the policy parameters θ\theta by

∇θJ(θ)=E ⁣[∑t≥0γt (∂θμθ)⊤(∂art⊤+γ (∂aft)⊤λ(st+1))].\nabla_\theta J(\theta) =\mathbb E\!\left[\sum_{t\geq 0}\gamma^t\,(\partial_\theta\mu_\theta)^\top \Big(\partial_a r_t^\top+\gamma\,(\partial_a f_t)^\top\lambda(s_{t+1})\Big)\right].

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 λ(s)i\lambda(s)_i indicates that any decision that increases feature ii 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.

References

Asadulaev, A., Ognev, D., Salta, K., & Takac, M. (2026). Value-Gradient Hypothesis of RL for LLMs. arXiv preprint: 2605.21654. 
Blackwell, D. (1947). Conditional Expectation and Unbiased Sequential Estimation. The Annals of Mathematical Statistics, 18(1), 105–110. 
Glynn, P. W. (1990). Likelihood Ratio Gradient Estimation for Stochastic Systems. Communications of the ACM, 33(10), 75–84. 
Hafner, D., Lillicrap, T., Ba, J., & Norouzi, M. (2020). Dream to Control: Learning Behaviors by Latent Imagination. International Conference on Learning Representations. 
Heess, N., Wayne, G., Silver, D., Lillicrap, T., Erez, T., & Tassa, Y. (2015). Learning Continuous Control Policies by Stochastic Value Gradients. Advances in Neural Information Processing Systems, 28, 2944–2952. 
Metz, L., Freeman, C. D., Schoenholz, S. S., & Kachman, T. (2021). Gradients are Not All You Need. arXiv preprint: 2111.05803. 
Mohamed, S., Rosca, M., Figurnov, M., & Mnih, A. (2020). Monte Carlo Gradient Estimation in Machine Learning. Journal of Machine Learning Research, 21(132), 1–62. 
Parmas, P., Rasmussen, C. E., Peters, J., & Doya, K. (2018). PIPPS: Flexible Model-Based Policy Search Robust to the Curse of Chaos. Proceedings of the 35th International Conference on Machine Learning, 80, 4065–4074. 
Suh, H. J. T., Simchowitz, M., Zhang, K., & Tedrake, R. (2022). Do Differentiable Simulators Give Better Policy Gradients? Proceedings of the 39th International Conference on Machine Learning, 162, 20668–20696. 
Williams, R. J. (1992). Simple statistical gradient-following algorithms for connectionist reinforcement learning. Machine Learning, 8, 229–256.