Sample-Efficient Online Learning in LM Agents via Hindsight Trajectory Rewriting

Michael Y. Hu, Benjamin Van Durme, Jacob Andreas, Harsh Jhamtani

TL;DR

ECHO (Experience Consolidation via Hindsight Optimization) is a new framework that significantly boosts the sample efficiency of Language Model (LM) agents by "rewriting history." Instead of just reflecting on failures, ECHO identifies alternative goals achieved during a failed attempt and synthesizes optimized, successful trajectories for those goals, allowing agents to learn much faster in novel environments.

What Problem Does This Paper Address?

While Large Language Models are impressive generalists, they often struggle when dropped into unfamiliar, interactive environments. They typically require many trials to learn the "lay of the land"—a problem known as poor sample efficiency. In the real world, where every interaction might involve a slow human response or a physical robot movement, we can't afford thousands of failed attempts.

Existing agent architectures try to solve this using "Reflexion" (thinking about what went wrong) or basic memory buffers. However, these methods often fail to extract the full value of a failed trajectory. If an agent fails to find an "orange star" but happens to pass a "green door" along the way, standard methods might just record a failure. The authors argue that every failure is a "synthetic success" for a different goal, if only the agent knew how to relabel and optimize it.

Methods

ECHO adapts a concept from Reinforcement Learning called Hindsight Experience Replay (HER) and supercharges it with the reasoning capabilities of LMs. The process follows two main rules:

  1. The Hindsight Rule: After a trajectory (successful or not), the LM analyzes the logs to identify "subgoals" or alternative objectives it actually encountered. It then rewrites the original messy trajectory into a clean, optimized "workflow" for those alternative goals.
  2. The Update Rule: The agent maintains a memory buffer of these workflows. To keep memory efficient and effective, it uses a compression-inspired heuristic: if it generates a new workflow for an existing goal, it only keeps the shorter one (following the principle of Minimum Description Length).

Unlike traditional HER, which just changes a label, ECHO uses the LM's world knowledge to actively edit out redundant steps and "hallucinate" more efficient paths based on what it saw.

ECHO identifies alternative goals (like the yellow door) from a failed attempt to find an orange star and generates optimized workflows for them.

Datasets

The authors evaluated ECHO on "stateful" versions of two benchmarks, where agents persist memory across multiple attempts in the same environment:

  • XMiniGrid-Stateful: A 2D grid-world navigation and planning task where agents must find and interact with objects in a partially observable house.
  • PeopleJoinQA-Stateful: A complex enterprise simulation where an agent must call tools and "talk" to simulated employees to gather information and answer multi-part questions.

Key Insights and Ideas

  • LMs as "Incomplete World Models": Even if an LM doesn't have a perfect map of a new environment, it has enough "common sense" to know that if it saw a key in Room A while looking for a ball in Room B, it can synthesize a perfect path to that key for future use.
  • Optimization over Reflection: The authors found that generic reflection (e.g., "I should be more efficient next time") is often too vague to change behavior. Concrete, rewritten trajectories provide much stronger "few-shot" examples for the agent to follow in later episodes.
  • The Power of Compression: By preferring shorter workflows in memory, the agent naturally gravitates toward optimal behavior without needing an explicit reward function for speed.

Key Results

ECHO demonstrated substantial improvements in both performance and efficiency:

  • XMiniGrid: ECHO outperformed the standard ReAct baseline by 80% in mean reward. It also significantly beat more complex architectures like Reflexion and Agent Workflow Memory (AWM).
  • PeopleJoinQA: While Reflexion achieved slightly higher accuracy, ECHO was much more efficient, resolving queries using 1.6 fewer messages on average than the baseline.
  • Trajectory Validity: In a manual check, 85% of the "counterfactual" trajectories ECHO synthesized were actually executable and correct, proving that LMs can reliably "hallucinate" better versions of their own past experiences.
In XMiniGrid, ECHO (purple) achieves a significantly higher mean reward (0.27) compared to the ReAct baseline (0.15) and other memory methods.

Limitations and Future Work

  • Natural Language vs. Code: The current version uses natural language for workflows. The authors suggest that programmatic/code-like representations might be even more precise and effective for complex tasks.
  • Memory Scaling: Adding many trajectories to memory can eventually bloat the LM's context window. Future work could integrate ECHO with retrieval-augmented generation (RAG) to pull only the most relevant rewritten experiences.
  • Update Heuristics: The "shorter is better" rule is a simple starting point; more sophisticated ways to merge old and new environmental knowledge could further improve performance.

Summary updated

BibTeX
@inproceedings{hu2025sample,
  author = {Michael Y. Hu and Benjamin Van Durme and Jacob Andreas and Harsh Jhamtani},
  title = {{Sample-Efficient Online Learning in LM Agents via Hindsight Trajectory Rewriting}},
  booktitle = {LAW @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=HQsdiJghaB},
}

Loading related content...

Figure