Derive the Lagrangian and Primal-Dual Update for a Constrained MDP
You write down the problem cleanly: maximize expected reward, keep expected cost under a budget. Then you freeze, because there is no obvious place to put…

Key topics
You write down the problem cleanly: maximize expected reward, keep expected cost under a budget. Then you freeze, because there is no obvious place to put the constraint. You cannot subtract it from the reward without choosing a penalty weight by hand, and a hand-chosen weight is a guess that only sometimes lands on the budget you wanted.
The fix is to stop guessing the price of the constraint and let the optimization set it. That price is the Lagrange multiplier, and the machinery that updates it is a primal-dual loop. This article derives both from the constrained problem, then draws a hard line between the optimization construction and any claim about safety.
What the Constraint Actually Says
Before any multiplier appears, fix the problem. A constrained Markov decision process extends an ordinary MDP with a second signal. You have states , actions , a transition kernel , and a discount factor . Alongside the reward , the environment emits a cost .
A policy induces two discounted value functions from a fixed initial-state distribution :
The constrained problem is:
Every symbol now has a referent. The constraint is an inequality on an expected discounted cost, evaluated from , not a per-episode cap. That distinction will matter later.
If you have read the prerequisite concept article, you already have the penalty-versus-constraint distinction. The short version: a penalty discourages bad behavior, a constraint restricts the set of admissible policies. We do not re-derive that here. What we need is the machinery that turns the restriction into something an optimizer can actually run.
The derivation leans on three assumptions. State and action spaces are finite. At least one feasible policy exists, so the constraint set is nonempty. And the constraint is evaluated in expectation over . Break any of these and the guarantees below weaken or vanish.
Knowledge check
Check your understanding
Answer this question before you continue.
Why You Cannot Just Subtract the Constraint
The tempting move is to fold the cost into the reward: define and run any standard RL algorithm. This works, in the narrow sense that it produces a policy. It fails as a way to hit a budget.
The penalty weight picks the reward-cost trade-off for you. You choose , and the resulting policy satisfies only by accident. Run the same on a different seed or a slightly different environment and the realized cost drifts. There is no closed-form inverse that maps to a budget, because the constraint is a hard inequality on a quantity the agent only observes through sampling. You are tuning a dial whose units you cannot read.
The Lagrangian changes the question. Instead of asking "what penalty weight produces the budget?", it asks "what price makes the budget bind?" The optimization answers that, and the answer is a number you can inspect.
Warning: The Lagrangian is an optimization device. It reformulates the problem so a solver can attack it. It is not a safety mechanism, and it does not make the constraint unbreakable.
Building the Lagrangian Step by Step
Attach a multiplier to the cost-budget gap and write:
Three pieces. The reward term is what you want to grow. The priced violation term charges the policy when it overspends and credits it when it underspends. The constant is an offset that shifts the whole expression but does not change which policy is best for a fixed .
Now ask what the inner minimization over does. Two cases.
If , the gap is nonpositive. Multiplying by gives a nonpositive number, so . The inner minimum over is attained at , and collapses to .
If , the gap is positive. As , the term . The inner minimum is unbounded below, so infeasible policies are driven to .
That is the whole trick. The inner minimization over acts as a switch: feasible policies keep their reward, infeasible policies are annihilated. So
recovers the constrained optimum. This max-min ordering is the primal problem.
The useful move is to swap the order. Under saddle-point conditions, the min and max commute:
The right-hand side is the dual problem, and it is the one that gives you an algorithm. The inner is an ordinary unconstrained MDP for any fixed . The outer is a one-dimensional search over a price.
Knowledge check
Check your understanding
Answer this question before you continue.
Reading the Multiplier as a Price
is the marginal cost of tightening the budget by one unit. Economists call this the shadow price of the constraint, and the name earns its keep here.
When , the constraint is slack. The unconstrained reward-maximizing policy already respects the budget, so the price of the constraint is zero. When is large, the constraint binds hard and the agent trades reward for feasibility. The multiplier is not a hyperparameter you tune. It is a quantity the optimization discovers.
The mechanism becomes concrete when you look at the inner problem. For a fixed ,
That is an ordinary MDP with modified reward . Any standard RL algorithm can solve it. The constrained problem has been reduced to a sequence of unconstrained ones, each indexed by a price.
A Two-Policy Toy
Take a CMDP with two candidate policies and .
| Policy | ||
|---|---|---|
| 10 | 3.0 | |
| 7 | 0.5 |
is infeasible (). is feasible. For a fixed , the Lagrangian values are:
Set them equal: , so . Below , the infeasible policy wins because its reward advantage outweighs the price. Above it, the feasible policy wins. The multiplier is exactly the price at which the agent becomes indifferent between breaking the budget and respecting it.
Knowledge check
Check your understanding
Answer this question before you continue.
Deriving the Primal-Dual Update
The dual problem is . We attack it by alternating: fix one variable, improve the other.
Primal step. Fix . Ascend the gradient of with respect to the policy parameters . Since is constant, this is gradient ascent on the modified reward . For a tabular or softmax policy, that is an ordinary policy-gradient step.
Dual step. Differentiate with respect to :
This derivative is negative exactly when the constraint is violated () and positive when it is satisfied. The outer problem is a minimization over , so we descend this derivative. Descending means stepping in the direction that increases when the constraint is violated. Written as an ascent on the violation:
When cost exceeds the budget, rises and the modified reward penalizes cost harder. When cost sits below the budget, falls and the agent is free to chase reward again.
Projection. The derivation assumed . The raw update can push negative when the constraint is comfortably satisfied. So project after every step:
The projection is what enforces the nonnegativity the math required. Without it, a negative multiplier would reward cost rather than penalize it, and the whole construction inverts.
Note: The two updates run on different timescales. The primal chases the best policy for the current price; the dual chases the price that makes the budget bind. In practice the dual usually steps more slowly, so the primal has time to adapt before the price moves again.
Continuing the Toy
Start at , . At , wins with , and .
| Iteration | Chosen policy | New | |||
|---|---|---|---|---|---|
| 0 | 0.0 | 3.0 | +2.0 | 1.0 | |
| 1 | 1.0 | 3.0 | +2.0 | 2.0 | |
| 2 | 2.0 | 0.5 | −0.5 | 1.75 | |
| 3 | 1.75 | 0.5 | −0.5 | 1.5 |
Watch the mechanism. While the constraint is violated, climbs. Once the price crosses and the feasible policy takes over, cost drops below the budget and eases back. The multiplier oscillates around the indifference point, and the cost oscillates around the budget. That is the primal-dual loop doing its job.
Knowledge check
Check your understanding
Answer this question before you continue.
What the Derivation Does Not Promise
This is the part most treatments skip, and it is the part that keeps you out of trouble.
The Lagrangian reformulation is exact only under convexity and saddle-point conditions. Policy parameterization makes the primal problem non-concave in general, so the min-max swap is not guaranteed to hold. Convergence is a property of specific algorithms under specific assumptions, not a property of the derivation itself.
Feasibility is not guaranteed. If no policy satisfies the budget, the dual diverges and there is nothing to converge to. The method cannot manufacture a feasible policy out of an infeasible problem.
Expected-cost constraints are satisfied in expectation over . A single episode can still blow through the budget badly. If your cost is catastrophic in one trajectory, an expectation is the wrong object to constrain.
Multiplier oscillation and slow dual convergence are common. is a learned quantity with its own stability problems, and it can wander for a long time before settling.
Common mistake: Treating a converged as proof that the policy is safe. It is proof that the expected cost landed near the budget on the runs you measured. Those are different claims.
The decision rule: use this when you want a tunable reward-cost trade-off with a principled price. Do not use it as a substitute for action masking, shielding, or hard runtime limits. Those enforce; the Lagrangian negotiates.
Where to Take This Next
The smallest useful test is a tabular primal-dual loop on a toy CMDP. Implement the two updates above, log and the realized cost each iteration, and plot cost against the budget over time. You will see the oscillation from the toy example appear in your own numbers.
Watch for two failure signatures. If climbs without cost dropping, your policy may be unable to reach feasibility, or your dual step size is too large. If cost drops below budget while reward collapses, is overshooting and the agent is paying too much for a constraint that was already satisfied.
The durable rule: the multiplier is a price, the projection is the guardrail, and neither one is a safety guarantee. When you are ready to see the fixed-penalty baseline and the primal-dual method run side by side on the same toy problem, that comparison is where the theory meets measured cost.
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.


