Behavior-agnostic Task Inference for Robust Offline In-context Reinforcement Learning

Long Ma, Fangwei Zhong, Yizhou WangICML 2025

The ability to adapt to new environments with noisy dynamics and unseen objectives is crucial for AI agents. In-context reinforcement learning (ICRL) has emerged as a paradigm to build adaptive policies, employing a context trajectory of test-time interactions to infer the true task and the corresponding optimal policy efficiently without gradient updates. However, ICRL policies heavily rely on context trajectories, making them vulnerable to distribution shifts from training to testing.

Behavior-agnostic Task Inference (BATI) is a new framework that makes offline ICRL significantly more robust to these distribution shifts. By replacing traditional learned encoders with a model-based maximum-likelihood approach focused purely on environment dynamics, BATI ensures that agents infer their current task based on how the world reacts, rather than how a previous agent behaved.

The Challenge of Context Shift

In-context reinforcement learning allows AI agents to adapt to new tasks on the fly by looking at a "context" (a short history of recent interactions) to figure out what they should be doing. While this is great for building flexible agents, it introduces a major weakness: context shift.

Most current ICRL methods are trained "offline" on static datasets. During training, the agent learns to associate certain behaviors in the context with specific tasks. For example, if a training dataset shows a robot moving fast, the agent might learn that "fast movement = high-velocity task."

However, at test time, the context might come from a different source—perhaps a noisy or sub-optimal policy. If the test-time robot is moving slowly even though the task is "high velocity," a traditional agent will get confused. It relies on behavioral cues (how the agent acts) rather than physical dynamics (how the environment responds to actions). This paper tackles the challenge of making task inference robust when the behavior seen in the context is very different from what was seen during training.

Behavior-agnostic Task Inference (BATI)

The authors introduce BATI to address this vulnerability. The core shift is moving away from a "black-box" encoder that tries to guess the task (approximate Bayesian inference) and moving toward a model that asks, "Which task best explains these physics?"

  1. Task Embedding Table: Instead of a single encoder network, BATI maintains a table of learnable embeddings—one for each task in the training set.
  2. Dynamics Estimator: During the offline training phase, BATI learns a dynamics model that predicts the next state and reward given the current state, action, and a task embedding.
  3. Maximum Likelihood Inference: At test time, when the agent is given a new context, it doesn't use an encoder. Instead, it performs an optimization: it searches through the task embeddings to find the one that maximizes the likelihood of the observed transitions in the context.
  4. Policy Conditioning: The final policy is conditioned on this inferred task embedding to execute the optimal behavior.

By focusing exclusively on the transition dynamics, BATI insulates itself from the specific policy used to collect the context data.

Key Insights and the "Astronaut" Analogy

A core insight of this work is that dynamics are more reliable than behavior. Behavioral correlations are "shortcuts" that neural networks tend to exploit, but they are fragile. Physical dynamics (like gravity, friction, and reward structures) remain stable across different agents.

The Astronaut Analogy

The paper illustrates this with an intuitive analogy: If you see someone wearing a spacesuit, you might assume they are on the Moon based on this behavioral/appearance cue. However, if you watch them jump and they fall back down quickly, the dynamics cue tells you they are actually on Earth. BATI operates like the observer who watches the jump height (dynamics), rather than the one who just looks at the suit (behavior).

Furthermore, the authors prove mathematically that when training data has a high correlation between tasks and behaviors, a standard encoder cannot be trained to be robust to context shifts because it is fundamentally biased by the training distribution. Interestingly, environmental noise makes the "behavioral shortcut" even more tempting for standard encoders, leading to a total collapse in performance that BATI successfully avoids.

Experimental Evaluation and Results

The researchers evaluated BATI on several standard MuJoCo robot locomotion benchmarks (AntDir, HalfCheetahDir, HalfCheetahVel, HopperParam, WalkerParam) modified to include task variations, as well as KuhnPoker to test generalizability beyond continuous control. Crucially, they introduced adversarial context shifts, where the context provided at test time was collected by a policy designed for a completely different task, specifically to trick the agent.

BATI consistently outperformed state-of-the-art baselines like UNICORN, CSRO, and FOCAL across all tested environments:

  • Robustness to Shift: In the "AntDir" environment with significant context shift, BATI achieved an episodic return of ~46, while the next best baseline (CSRO) struggled at ~29, and others fell into negative values.
  • Out-of-Distribution (OOD) Tasks: BATI was the only method capable of achieving positive returns when faced with both OOD contexts and OOD tasks simultaneously.
  • Scaling with Data: Unlike baselines, BATI’s performance improved steadily as the length of the context increased, showing it effectively distills more information from longer sequences of physics interactions.

Limitations and Future Work

While highly effective, BATI has some limitations. It requires a search/optimization step during online inference, which is more computationally intensive than a single forward pass through an encoder. Additionally, the current work focuses on state-based MuJoCo environments; scaling this to high-dimensional visual inputs where dynamics are harder to model is a noted next step. Finally, the method assumes that the "integral over behavior" is roughly the same across tasks, which might not hold in highly complex or heterogeneous task distributions.

Overall, BATI represents a significant step toward AI agents that can land in a new environment, observe the "rules of the world" for a few seconds, and immediately know how to behave—regardless of who was there before them.

Summary updated

BibTeX
@inproceedings{ma2025behavior,
  author = {Long Ma and Fangwei Zhong and Yizhou Wang},
  title = {{Behavior-agnostic Task Inference for Robust Offline In-context Reinforcement Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=jMKaATBEKb},
}
Figure