Emergent Agentic Transformer from Chain of Hindsight Experience

Hao Liu, Pieter Abbeel

Introduction

While large Transformer models have revolutionized natural language processing and computer vision, they have historically struggled to reach the same heights in Reinforcement Learning (RL). A major bottleneck is that current Transformer-based policies, such as the Decision Transformer, act primarily as "one-shot" learners: they attempt to imitate high-reward trajectories found in a static dataset.

However, real-world intelligence relies heavily on trial and error. If an agent fails a task on the first try, it should use that failure as context to adjust its strategy for the next attempt. Existing models lack this "agentic" quality. They cannot stitch together information from multiple sub-optimal attempts to find a successful path, nor do they improve their strategy during live interaction without explicit weight updates.

The Agentic Transformer (AT) introduces a novel architecture that allows Transformer models to learn from their own mistakes by training on "Chains of Hindsight"—sequences of attempts sorted from worst to best. Unlike previous models, AT can combine information from multiple sub-optimal trials to improve its performance both during training and at test time.

Methods: Chain of Hindsight

The authors treat RL as a sequence modeling problem with a unique twist. Instead of feeding the model a single, isolated trajectory, they feed it a Chain of Hindsight (CoH). This process involves several key steps:

  1. Sequence Training: The model is trained on a sequence of $n$ trajectories (typically 4) sampled from a dataset. Crucially, these trajectories are sorted in ascending order based on their total achieved rewards.
  2. Hindsight Relabeling: The "target return" for every trajectory in the chain is relabeled to match the maximum reward achieved by the best trajectory in that specific sequence. This provides a powerful learning signal: "Even if you started poorly, here is the sequence of improvements that leads to success."
  3. Task Completion Tokens: The architecture introduces a binary token ($d$) that informs the model whether a specific attempt succeeded in reaching the target return.
  4. Autoregressive Prediction: The core model is a decoder-only, GPT-style Causal Transformer. It predicts the next action based on a comprehensive context of past states, actions, rewards, target returns, and completion tokens across the entire chain of attempts.
Chain of Hindsight methodology showing trajectory sorting and return relabeling.

Datasets and Evaluation

The researchers evaluated the Agentic Transformer on two major offline RL benchmarks to test its robustness and adaptability:

  • D4RL: A standard benchmark featuring MuJoCo continuous control environments (HalfCheetah, Hopper, Walker) with datasets ranging from "Medium" to "Expert" quality.
  • ExoRL: A more challenging dataset consisting of diverse, exploratory, and often highly sub-optimal trajectories. This serves as a "stress test" to determine whether a model can learn from messy, imperfect data rather than relying solely on near-perfect expert demonstrations.

Key Insights

  • In-Context Learning for RL: AT exhibits "emergent agency." At test time, if the model is allowed to attempt a task four times in a row, it uses the first three failures as context to refine its strategy for the fourth attempt. This happens entirely in-context, without any gradient updates.
  • The Power of the "d" Token: Ablation studies reveal that the task completion token is vital. It acts as a feedback mechanism, allowing the model to recognize when a previous strategy failed so it can pivot to a better approach in the next link of the chain.
  • Learning to "Stitch": By observing a sequence of "bad, better, best" trajectories, the model learns the underlying delta required for improvement. This allows it to perform exceptionally well even when the training data is entirely sub-optimal.
  • Scaling Matters: The paper demonstrates a clear scaling trend. Larger models (scaling from a "Small" 2-layer model to an "XLarge" 8-layer model) and longer hindsight chains consistently yield better downstream performance.

Key Results

On the D4RL benchmark, the Agentic Transformer proved highly competitive with traditional methods. It achieved a total average score of 85.21, outperforming the baseline Decision Transformer (77.49) and even edging out the state-of-the-art TD-learning algorithm, TD3+BC (84.83).

However, AT's true dominance emerged on the sub-optimal ExoRL benchmark, where its ability to learn from failure was most apparent. It achieved an average score of 83.02, nearly doubling the performance of the Decision Transformer (45.51).

ExoRL average score demonstrating test-time improvement across multiple rollouts.

As shown in the results, the model exhibits remarkable test-time improvement. In ExoRL tasks, the model's score jumped from roughly 50 on the first rollout to over 83 by the fourth rollout, proving it successfully "learns" from the current session's history.

Limitations and Future Work

While promising, the Agentic Transformer comes with computational trade-offs. Because AT processes multiple trajectories simultaneously, the input sequences become exceptionally long (up to 20,000 tokens). This requires significant memory and compute power due to the quadratic complexity of standard self-attention mechanisms. Furthermore, rolling out multiple trajectories to achieve peak performance is inherently slower during inference than a single-shot policy.

Future work will likely focus on exploring more efficient attention mechanisms to handle even longer histories, and scaling the model to more complex, real-world robotic applications where diverse, sub-optimal data is the standard.

Summary updated

BibTeX
@inproceedings{liu2023emergent,
  author = {Hao Liu and Pieter Abbeel},
  title = {{Emergent Agentic Transformer from Chain of Hindsight Experience}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=ewD2Leqruy},
}
Figure