Algorithm Distillation for Safe In-Context Reinforcement Learning

Minjae Kwon, Amir Moeini, Shangtong Zhang, Lu FengarXiv 2026 · arXiv

Reinforcement Learning (RL) has made massive strides in mastering complex tasks, but deploying these agents in the real world requires more than just high scores—it requires safety. Safe In-Context Reinforcement Learning (Safe ICRL) is a frontier where agents must adapt to new, unseen tasks while strictly adhering to safety budgets, all without updating their model parameters.

In their recent paper, researchers from the University of Virginia introduce a more robust way to train these agents. By rethinking the data used for training and introducing a novel test-time adjustment called A-Lag, they’ve significantly improved how agents balance the "reward vs. risk" trade-off.

TL;DR

The authors improve Safe In-Context RL by distilling a budget-aware teacher and introducing A-Lag, a test-time mechanism that reweights actions based on predicted cost. This approach achieves lower safety costs across five diverse benchmarks compared to previous state-of-the-art distillation methods.

What Problem Does This Paper Address?

Standard In-Context RL (ICRL) uses Transformers to "learn how to learn" from interaction history. Safe ICRL adds a layer of difficulty: the agent must maximize rewards while keeping cumulative costs (like collisions or speed violations) under a specific budget.

Previous attempts, like Safe Algorithm Distillation (Safe AD), often failed because they were trained on the learning histories of standard safe RL algorithms. These histories are "budget-blind"—they don't show the agent how to change its behavior if the budget is nearly exhausted or how to search for a hidden goal safely. Consequently, prior agents often overspent their budgets or failed to reach goals in complex environments.

Methods

The researchers proposed two major innovations:

  1. A Budget-Aware Teacher & Dynamic Data: Instead of using standard RL logs, they created a two-phase teacher. In the Search phase, the teacher explores the environment, becoming more cautious as its budget dwindles. Once it finds the goal (or identifies a safety limit), it switches to an Expert phase to exploit the task efficiently. Crucially, they collected data under dynamic budgets, varying the starting safety limit to teach the agent how to handle different levels of "allowable risk."
  2. A-Lag (Action-level Lagrangian): This is a test-time adjustment. The agent’s Transformer is equipped with an extra "head" that predicts the immediate cost of any given action. At deployment, A-Lag reweights the policy’s action probabilities to favor lower-cost actions. The "strength" of this safety filter is controlled by a Lagrange multiplier ($\lambda$) that updates between episodes: if the agent overspends its budget, it becomes more conservative in the next episode.

Datasets

The authors evaluated their methods on five benchmarks across three families:

  • DarkRoom: A grid world with a hidden goal and obstacles.
  • PointGoal & CarGoal: Continuous navigation tasks where hazards and goals are invisible until reached.
  • HalfCheetah & Ant: Locomotion tasks with a hidden velocity limit.

They specifically tested generalization by using different layouts for training (center-oriented) and testing (edge-oriented), forcing the agent to truly adapt in-context.

Key Insights and Ideas

  • Diversity is Safety: Training on dynamic budgets significantly increases "occupancy entropy"—essentially, the agent sees a much wider variety of (Reward-to-Go, Cost-to-Go) pairs. This data coverage is vital for the Transformer to understand how to behave when it's "running out of safety."
Table 1: Coverage of the (RTG, CTG) plane in the teacher data under fixed and dynamic budgets (both quality levels). Each input is rescaled to [0, 1] between its near-minimum and near-maximum (the 1st and 99th percentiles over both datasets, with values outside clipped), and the plane is divided into a 20 × 20 grid. Cells: occupied cells out of 400. Entropy = −P
  • Dual Adaptation: A-Lag adapts in two ways simultaneously. First, through its context (the Transformer "remembers" where hazards are). Second, through the multiplier (the dual-ascent update explicitly tunes the agent's risk aversion based on past failures).
  • The "Safety Brake" Works: The authors proved mathematically that increasing the multiplier $\lambda$ never increases the expected predicted cost. This provides a solid theoretical foundation for why A-Lag is an effective "safety brake" at test time.

Key Results

  • Lower Costs Everywhere: A-Lag achieved a lower mean episode cost than the baseline Safe AD in all five environments.
  • Dynamic Budget Benefits: In DarkRoom and HalfCheetah, training with dynamic budgets improved both the final return and the safety violation rate.
Figure 1: Models trained on dynamic-budget histories (Dynamic) and on fixed-budget histories (Fixed), on the test tasks. Each curve shows the mean episode return or cost over three seeds, with a shaded band of ±2 standard errors. With dynamic budgets, A-Lag has a lower cost in four of the five environments, all but PointGoal, and Safe AD has a higher return in all five, compared with fixed budgets.
  • Rapid Adjustment: A-Lag’s violation rate and excess cost often drop sharply after the very first episode of a deployment, as the adaptive multiplier quickly corrects for the specific task's safety constraints.

Limitations and Future Work

While A-Lag is a step forward, it has its trade-offs. In four out of five environments, the lower cost came at the price of a slightly lower final return—the classic "safety-performance trade-off."

Additionally, the cost head currently only predicts immediate cost. Future work could involve predicting long-term cumulative cost to make the agent even more proactive. Finally, the teacher policy still requires some environment-specific design (like defining the search/expert phases), and automating this teacher generation remains an open challenge.

Summary updated

Abstract

Safe in-context reinforcement learning (Safe ICRL) asks a policy with fixed parameters to improve on an unseen constrained task from its interaction history while aiming to keep its cost within a budget. Prior work studied Safe ICRL on five benchmarks in three families: a grid world with a hidden goal and obstacles (DarkRoom), continuous navigation where goals and hazards stay invisible until reached (PointGoal, CarGoal), and locomotion with a hidden velocity limit (HalfCheetah, Ant). Safe Algorithm Distillation (Safe AD), trained on the learning histories of a safe RL algorithm, has shown adaptation failures in prior evaluations. For example, in the locomotion tasks its return stays below zero, and it rarely reaches the goal in CarGoal. We revisit two parts of Safe AD. First, we distill a budget-aware teacher with two phases. It searches for the hidden task, exploring more when more budget remains. After its first reward or cost, an expert policy exploits the task within the remaining budget. We collect the teacher's histories under fixed or dynamic budgets. Dynamic budgets increase the occupancy entropy of the return and budget inputs in every environment, and at test time they improve return and lower cost in DarkRoom and HalfCheetah. Second, we propose A-Lag, a test-time adjustment that reweights the distilled policy's actions by their predicted cost, with an adaptive Lagrange multiplier that is updated from the costs observed. In our experiments, A-Lag had lower mean episode cost than Safe AD in all five environments, with lower mean final return in four.
BibTeX
@misc{kwon2026algorithm,
  author = {Minjae Kwon and Amir Moeini and Shangtong Zhang and Lu Feng},
  title = {{Algorithm Distillation for Safe In-Context Reinforcement Learning}},
  year = {2026},
  eprint = {2611.21524},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.21524},
}
Figure