Safe RL: Risk, Safe Exploration, and Alignment
A companion to the constrained-MDP lesson. Constraining the mean cost is not enough: a policy safe on average can be catastrophic in the tail, and a policy safe at convergence can violate its limits wildly while learning.
╌╌╌╌
This builds on Safe and Constrained RL: The CMDP and Policy Methods, which argued that reward maximization alone invites specification gaming, formalized the honest alternative as a constrained MDP (maximize return subject to a cost budget), and solved it two ways: Lagrangian primal-dual with a learned multiplier (RCPO), and constrained policy optimization with a trust-region cost bound (CPO).
Those methods bound the expected cost at convergence. Two gaps remain. A policy safe on average can still be ruinous in its worst episodes, and a policy that ends up safe can behave dangerously while it is still learning. This lesson closes both gaps and then connects safety to the alignment framing.
Risk-sensitive RL: optimizing the tail
Constraints bound the expected cost, but expectation is blind to variance. A policy that is safe on average can still visit a catastrophe on a small fraction of episodes, and for high-stakes deployments the tail is the whole point: a self-driving policy with a great mean and a rare fatal outcome is unacceptable, and no bound on rules it out. Risk-sensitive RL optimizes a functional of the return distribution that weights bad outcomes more heavily than the mean does.1
This connects directly to distributional RL, which already models the full distribution of returns rather than only its mean . Once you have the distribution, you can optimize any risk measure of it. The standard choice is Conditional Value at Risk. Let be the return (a random variable) and fix a tail level . The value-at-risk is the -quantile of — the return you do at least as well as with probability — and is the mean of the returns below that quantile:
It is the average outcome over the worst fraction of episodes. Maximizing makes the policy improve its bad cases, not its average one. As the whole distribution is in the tail and CVaR reduces to the ordinary expectation (risk-neutral); as it concentrates on the single worst outcome (worst-case, robust). The knob dials continuously between average-case and worst-case optimization.
CVaR has two properties that make it the default risk measure. It is a coherent risk measure (it respects diversification and monotonicity in a precise sense that variance does not), and it has a variational form,
that turns CVaR optimization into a tractable stochastic program — the auxiliary variable converges to , and the objective is differentiable in the policy parameters, so it plugs into a policy-gradient method. A distributional critic that already estimates the quantiles of supplies everything the CVaR objective needs; risk-sensitivity is then almost free on top of a distributional agent.
Worked example: CVaR on a discrete return distribution
Compute CVaR by hand to see how differently it ranks policies from the mean. Take two policies whose returns are ten equally-likely outcomes each:
- Policy A (steady): returns , mean .
- Policy B (gamble): returns , mean .
Both have the same mean , so a risk-neutral objective is indifferent. Now take , the worst — one outcome out of ten. For A the single worst outcome is , so . For B it is , so . The tail measure ranks A vastly above B — versus — even though their means are identical, because B pays for its higher typical return with a rare catastrophe. Widen the tail to (the worst three of ten): A averages ; B averages . As both CVaRs climb back to the shared mean and the distinction vanishes. The knob sets how much of the bad tail you insist on optimizing: a self-driving policy graded at is forced to fix its one-in-a-hundred worst episode, which is the one that matters.
Safe exploration
Constraints and risk measures shape the policy the agent converges to. They say nothing about the actions it takes while learning, and that is where physical systems get damaged. A robot that must not exceed a joint torque cannot afford to discover the limit by exceeding it once. Safe exploration enforces constraints during learning, on every action, not merely on the final policy. Three mechanisms dominate.
Shielding
A shield is a reactive safety layer synthesized from a formal specification of
what must never happen — a temporal-logic formula like never enter a collision state,
never let the tank run dry.
2 At each step the shield inspects the
action the agent proposes and, if that action could lead to a specification
violation within its horizon, it overrides it with a safe alternative. The agent
learns normally, receiving the state and reward as usual, but its actions are
filtered through a correctness monitor that is provably safe by construction. The
shield ignores reward entirely; it encodes only the safety automaton, and it
guarantees the spec is never violated regardless of what the still-learning policy
proposes. The limitation is that you must be able to write the specification and model
the dynamics well enough to check it — feasible for discrete or well-understood
systems, hard for high-dimensional continuous ones.
Lyapunov methods
A second approach borrows the Lyapunov function from control theory. A Lyapunov
function is a scalar energy
that certifies stability: if you can guarantee
never increases along the agent's trajectories, the agent is trapped inside a
sublevel set — a certified safe region it can never leave.
Lyapunov-based safe RL3 constructs such a function for the cost constraint
and restricts the policy at each state to the actions that keep from growing,
which is a per-state constraint on the action set rather than a bound on the episodic cost. The guarantee is that the closed-loop system stays within the certified region for all time, giving the same during-learning safety a shield does but derived from the cost dynamics instead of a logical spec. The difficulty is constructing a valid Lyapunov function; much of the work is in learning or approximating one that is both safe and not so conservative that it blocks the task.
Safety layers
The most direct mechanism assumes a differentiable model of the cost and projects each proposed action onto the safe set. A safety layer4 sits between the policy and the environment: the policy proposes an action , and the layer solves a small optimization to find the closest action that satisfies the per-step cost constraint,
where is a linear model of the next-step cost learned from data. With a single linear constraint this projection has a closed-form solution — the layer either passes through unchanged (if it is already safe) or slides it along the constraint's gradient just far enough to satisfy the budget. Because the correction is analytic and differentiable, gradients still flow back to the policy, which learns to propose actions that need little correction. The safety layer is the continuous-control counterpart to the shield: where the shield vetoes discrete actions against a logical spec, the layer projects continuous actions against a learned cost model.
The three mechanisms trade off generality against assumptions. A shield needs a logical spec and a checkable model; Lyapunov methods need a certified energy function; a safety layer needs a differentiable cost model. Each provides what the constrained and risk-sensitive methods do not: safety during learning, enforced action by action.
| Mechanism | What it needs | What it guarantees |
|---|---|---|
| Shield | temporal-logic spec + discrete model | never violates the logical spec |
| Lyapunov | a certified for the cost | stays inside a certified region for all time |
| Safety layer | differentiable per-step cost model | each action satisfies the per-step cost budget |
| Lagrangian / CPO | only a cost signal and threshold | expected cost (asymptotic / near-monotonic) |
Benchmarks and safe RLHF
The methods above are a decade of theory; two later developments ground them in practice and connect the thread back to the RLHF lesson.
Standardized safe-RL benchmarks. For years constrained RL lacked a common testbed, and every paper reported on its own tasks. Safety Gym fixed this: a suite of continuous-control navigation tasks where an agent must reach goals while a separate cost signal counts contacts with hazards, and every method is measured on the same reward-versus-cost frontier.5 Its empirical finding is worth stating plainly, because it tempers the theory: a well-tuned PPO-Lagrangian baseline — plain PPO with the primal-dual multiplier of this lesson bolted on — is hard to beat, and CPO's per-step guarantee often does not yield a better final frontier despite its stronger theory. The practical default in constrained deep RL is the Lagrangian method, not the trust-region one; CPO is preferred only when violations during training must be bounded, not merely at convergence.
Safe RLHF. The constrained formalism came back to language models directly. The RLHF lesson optimized a single reward that had to bundle helpfulness and harmlessness together, which pits the two against each other inside one scalar. Safe RLHF splits them: it trains a separate helpfulness reward model and a cost model for harm, then optimizes helpfulness subject to a harmlessness constraint using exactly the Lagrangian primal-dual machinery of this lesson — a learned multiplier trading helpfulness for safety on a slow timescale.6 The over-optimization the RLHF lesson controlled with a KL leash is, in this framing, one constraint among several, and the CMDP is the right container for all of them. The boat spinning in the lagoon and a language model that is helpful but unsafe are the same failure, and the same constrained-optimization discipline answers both.
Robustness and the alignment framing
Two threads sit just outside the CMDP formalism but belong to the same concern: making a policy behave when the world is not the training distribution.
The first is sim-to-real robustness. Policies trained in simulation exploit the simulator's quirks and fail on the real system whose friction, latency, and mass differ from the model — a distribution shift that shows up as a safety failure. Domain randomization7 addresses it directly: randomize the simulator's physics parameters (masses, friction, delays, sensor noise) across a wide range during training, so the policy must succeed on a whole family of dynamics rather than one. A policy robust across the randomized family tends to treat the real system as just another sample from it. This is a form of worst-case optimization — optimizing for the hardest dynamics you might face is a robust objective, related to the CVaR tail, and it is why domain randomization and risk-sensitive RL are two answers to one question: how to be safe under uncertainty you cannot fully model.
The second is the broader AI-safety / alignment framing. Every technique here is a response to the same fact: a scalar reward is an incomplete statement of intent, and a competent optimizer will exploit the incompleteness. Specification gaming, reward hacking, and over-optimization are one phenomenon seen at different scales — a boat spinning in a lagoon, a language model padding an answer, a cleaning robot hiding the mess — and the constrained, risk-sensitive, and shielded methods are the engineering responses. The RLHF lesson's KL leash and this lesson's cost budget are the same idea pointed at different proxies: bound how hard you push on a measure, because the measure stops being right before the optimizer stops pushing. As RL systems act in the world with real consequences, that discipline stops being an add-on and becomes the design problem.
The through-line: reward is not the whole story
The unconstrained objective assumed the reward said everything. The methods here each admit that it does not and add the missing structure back.
| Approach | What it adds beyond the reward |
|---|---|
| CMDP | an explicit cost signal and a budget ; feasibility as a first-class goal |
| Lagrangian / RCPO | a learned price that self-tunes to hit |
| CPO | a trust-region cost bound giving approximate feasibility every step |
| CVaR / risk-sensitive | optimize the lower tail of the return, not its mean |
| Shield / Lyapunov / safety layer | per-action safety enforced during learning |
| Domain randomization | robustness to dynamics the training distribution did not cover |
The lesson's one rule, stated plainly: real deployments must respect constraints the reward does not capture. Reward maximization is a means, not the goal, and a policy that maximizes reward while violating a limit the reward failed to mention has not succeeded — it has found the gap between what you wrote and what you meant. It shares a principle with offline RL, where the discipline was pessimism about values the data cannot verify; here it is pessimism about behaviors the reward cannot see. In both, the safe move is to distrust the parts of the objective you have no way to check.
Footnotes
- Chow, Ghavamzadeh, Janson, Pavone (2017),
Risk-Constrained Reinforcement Learning with Percentile Risk Criteria
, JMLR — develops policy-gradient and actor-critic methods for CVaR objectives and constraints, using the variational (Rockafellar–Uryasev) form of CVaR. ↩ - Alshiekh, Bloem, Ehlers, Könighofer, Niekum, Topcu (2018),
Safe Reinforcement Learning via Shielding
, AAAI — synthesizes a reactive shield from a temporal-logic safety specification that overrides unsafe actions, guaranteeing the spec holds throughout learning. ↩ - Chow, Nachum, Duenez-Guzman, Ghavamzadeh (2018),
A Lyapunov-based Approach to Safe Reinforcement Learning
, NeurIPS — constructs Lyapunov functions for CMDP constraints and restricts the policy to actions that keep the agent inside a certified feasible region during and after training. ↩ - Dalal, Dvijotham, Vecerik, Hester, Paduraru, Tassa (2018),
Safe Exploration in Continuous Action Spaces
, arXiv:1801.08757 — adds a differentiable safety layer that analytically projects each proposed action onto the set satisfying a learned linear per-step cost model. ↩ - Ray, Achiam, Amodei (2019),
Benchmarking Safe Exploration in Deep Reinforcement Learning
(Safety Gym), OpenAI — introduces a standardized suite of constrained continuous-control tasks with a separate cost signal, and finds a tuned PPO-Lagrangian baseline competitive with or better than CPO on the final reward-cost frontier, establishing the Lagrangian method as the practical default. ↩ - Dai, Pan, Sun, et al. (2023),
Safe RLHF: Safe Reinforcement Learning from Human Feedback
, arXiv:2310.12773 (ICLR 2024) — decouples helpfulness and harmlessness into a separate reward model and cost model, then optimizes helpfulness subject to a harmlessness constraint via Lagrangian primal-dual updates, applying the CMDP formalism directly to language-model alignment. ↩ - Tobin, Fong, Ray, Schneider, Zaremba, Abbeel (2017),
Domain Randomization for Transferring Deep Neural Networks from Simulation to the Real World
, IROS — randomizes simulator appearance and dynamics during training so a policy robust across the family transfers to the unseen real system. ↩
╌╌ END ╌╌