Safe In-Context Reinforcement Learning

Amir Moeini, Minjae Kwon, Alper Kamil Bozkurt, Yuichi Motai, Rohan Chandra, Lu Feng, Shangtong Zhang

TL;DR

This paper introduces SCARED, the first method for safe in-context reinforcement learning (ICRL). SCARED allows agents to adapt to new tasks without parameter updates, while crucially adhering to user-defined safety budgets. This breakthrough is vital for deploying ICRL in real-world scenarios where safety is paramount.

What Problem Does This Paper Address?

Traditional reinforcement learning (RL) agents learn through extensive trial-and-error, often requiring parameter updates even during deployment. In-context reinforcement learning (ICRL) offers a promising alternative, enabling agents to adapt to new tasks at test time solely by leveraging their interaction history, without any parameter updates. This is highly desirable for real-world applications as it simplifies deployment and reduces computational overhead. However, a critical gap has been the lack of safety guarantees in ICRL. Agents exploring new tasks in real-time could potentially exhibit unsafe behaviors, limiting their applicability in domains like robotics or autonomous systems. This paper addresses this by proposing the first framework for safe ICRL, ensuring that agents not only adapt effectively but also operate within specified safety constraints.

Methods

The core contribution is SCARED (Safe Contextual Adaptive Reinforcement via Exact-penalty Dual). SCARED frames safe ICRL as a constrained Markov decision process (CMDP), where agents aim to maximize rewards while keeping accumulated costs below a user-defined budget. The key innovation lies in its parameter-update-free adaptation during test time. SCARED achieves this by:

  • CMDP Formulation: Extending the ICRL framework to incorporate cost functions and safety budgets.
  • Exact-Penalty Dual: Proposing a novel optimization approach based on an exact penalty method in its dual form. This allows the agent to learn a policy that balances reward maximization with adherence to safety constraints.
  • Contextual Adaptation: The agent's policy is conditioned on the interaction history (context) and the current cost-to-go (CTG), enabling it to dynamically adjust its behavior based on the accumulated cost and the remaining budget.

This approach contrasts with existing safe RL methods that often rely on parameter updates or meta-learning, and with standard ICRL methods that lack safety considerations.

Datasets

The paper introduces new benchmarks for evaluating safe in-context adaptation:

  • SafeDarkRoom: A grid-world environment where agents must learn to navigate and reach a goal while avoiding obstacles, with sparse rewards and costs for collisions.
  • SafeDarkMujoco: A continuous state-action space environment analogous to SafeDarkRoom, using MuJoCo simulation.
  • SafeVelocity: Based on the SafetyGym benchmark, this environment involves controlling robot velocities, with a focus on adapting to unseen target velocities.

These environments are designed to test both out-of-distribution (OOD) generalization (e.g., edge-oriented obstacle placement during evaluation vs. center-oriented during training) and unseen in-distribution (ID) tasks.

Key Insights and Ideas

  • Safety is Achievable in Parameter-Free Adaptation: The paper demonstrates that it's possible to achieve robust safety guarantees even when agents adapt to new tasks without updating their parameters, a significant step for ICRL.
  • Budget-Aware Behavior: SCARED agents actively adjust their behavior based on the safety budget. A higher budget allows for more aggressive exploration and potentially higher rewards, while a lower budget enforces more conservative, safety-conscious actions.
  • OOD Generalization with Safety: The proposed benchmarks and SCARED's performance highlight its ability to generalize to unseen tasks that are distributionally different from the training data, while maintaining safety.
  • Long-Term Credit Assignment in Online Learning: The ablation studies suggest that online learning (like SCARED) is better suited for long-term credit assignment in safety-constrained tasks compared to offline methods, which struggle with dataset constraints.

Key Results

  • Superior Performance: SCARED consistently outperforms existing ICRL and safe meta-RL baselines across challenging benchmarks in terms of both cumulative reward and cost reduction.
  • Effective Budget Adaptation: Figure 2 clearly shows that as the cost budget (CTG) increases, SCARED achieves higher total returns, demonstrating its ability to leverage the budget for more aggressive exploration.
  • Robustness to OOD Tasks: SCARED shows significant improvement in return and reduction in cost as in-context interaction grows, even in OOD environments, outperforming baselines that fail to achieve safe adaptation.
  • SCARED Optimization Stability: The paper notes that SCARED's optimization process is more stable and easier to tune compared to naive primal-dual methods.

Limitations and Future Work

  • CMDP Assumption: The method relies on the availability of cost signals and an appropriate CTG budget, which might require domain knowledge or calibration.
  • Unmodeled Hazards: The framework doesn't eliminate risk entirely, especially in environments with unmodeled hazards or adversarial conditions. It suggests complementing the approach with human oversight and additional safety mechanisms.
  • Future Directions: The authors envision this work guiding the development of more robust, generalizable, and safety-aware in-context learning methods for real-world applications. Further research could explore extending the framework to handle more complex safety specifications or dynamic budget adjustments.

Summary updated

BibTeX
@inproceedings{moeini2026safe,
  author = {Amir Moeini and Minjae Kwon and Alper Kamil Bozkurt and Yuichi Motai and Rohan Chandra and Lu Feng and Shangtong Zhang},
  title = {{Safe In-Context Reinforcement Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=lm4PZa4kjZ},
}
Figure