AUTOHARNESS: IMPROVING LLM AGENTS BY AUTOMATICALLY SYNTHESIZING A CODE HARNESS

Xinghua Lou, Miguel Lazaro-Gredilla, Antoine Dedieu, Carter Wendelken, Wolfgang Lehrach, Kevin Murphy

TL;DR

Large language models (LLMs) often make illegal moves when acting as agents in games. This paper introduces AutoHarness, a method where an LLM automatically synthesizes a "code harness" to enforce game rules. This approach allows smaller LLMs to outperform larger ones by preventing illegal actions and can even lead to generating the entire game-playing policy as code, making inference faster and cheaper.

What Problem Does This Paper Address?

LLMs have shown impressive capabilities in various tasks, including code generation and problem-solving. However, when deployed as agents in interactive environments like games, they frequently exhibit a critical flaw: they make "illegal moves." These aren't just strategically poor decisions, but actions that violate the fundamental rules of the environment. For instance, in a chess competition, a significant portion of a powerful LLM's losses were due to illegal moves. Traditionally, preventing such errors involves either costly fine-tuning of the LLM or manually crafting "harnesses" – code that acts as a guardrail to filter out invalid actions. Manual harness creation is time-consuming, brittle, and needs to be redone for every new environment. This paper addresses the need for an automated, scalable, and effective way to ensure LLM agents adhere to environmental rules.

Methods

The core of AutoHarness is an LLM (specifically Gemini-2.5-Flash) that learns to generate a code harness through an iterative process. This is framed as a search problem over the space of programs. The LLM acts as a "mutation operator," proposing code refinements based on feedback from the game environment. This process is guided by Thompson sampling, a technique that balances exploring different code structures and exploiting promising ones to converge on a robust control loop.

The harness can take a few forms:

  • Harness-as-action-filter: The LLM proposes actions, and the harness verifies their legality. If an action is illegal, the LLM is prompted again with feedback about the illegal move.
  • Harness-as-policy: This is the most advanced form, where the LLM generates the entire game-playing logic as Python code. This eliminates the need to call the LLM during gameplay, making inference very fast and cost-effective.

The learning process involves a tree search where nodes represent different code hypotheses. Feedback from the environment (whether an action was legal and the reward obtained) is used to refine these hypotheses.

Datasets

The primary dataset used is TextArena, a collection of 145 text-based games. For evaluation, the authors selected 16 one-player (1P) and 16 two-player (2P) games from this collection. To make the problem more challenging, they modified some games by removing explicit "Available Moves" hints from the observation strings, forcing the LLM to deduce legal actions from environmental feedback.

Key Insights and Ideas

  • LLMs can learn to enforce their own rules: The most striking insight is that LLMs, when prompted correctly and given appropriate feedback, can automatically generate code that prevents them from making illegal moves. This is a significant step beyond relying solely on the LLM's internal "understanding" of rules.
  • Smaller models can outperform larger ones with a harness: By using a smaller, more cost-effective LLM (Gemini-2.5-Flash) to synthesize a custom harness, the authors demonstrate that it can achieve better performance than much larger, more capable LLMs (like Gemini-2.5-Pro) in certain tasks.
  • Code-as-policy is a viable and efficient paradigm: Generating the entire agent policy as executable code can be more efficient and cost-effective than relying on LLM inference at runtime, especially for tasks where a deterministic policy can be learned.
  • Iterative refinement with environmental feedback is powerful: The tree search and iterative refinement approach, guided by game outcomes, is effective in evolving code that meets specific functional requirements (like legality).

Key Results

  • 100% Legal Action Rate: The synthesized harnesses achieved a 100% legal action success rate across all 145 TextArena games tested.
  • Outperforming Larger Models (2P Games): In 2-player games, Gemini-2.5-Flash with the AutoHarness achieved a 56.3% win rate against Gemini-2.5-Pro (38.2% win rate), and a 64.8% win rate against vanilla Gemini-2.5-Flash.
  • Competitive Performance (1P Games): In 1-player games, the AutoHarness approach achieved a higher average reward than Gemini-2.5-Pro and was competitive with GPT-5.2-High.
  • Harness-as-Policy Superiority: The "Harness-as-Policy" approach achieved the highest average reward (0.870) across 16 1P games, outperforming Gemini-2.5-Pro (0.707), GPT-5.2 (0.635), and GPT-5.2-High (0.844).
  • Cost-Effectiveness: The Harness-as-Policy approach had nearly zero test-time cost, whereas experiments with GPT-5.2 and GPT-5.2-High cost approximately $640.

Limitations and Future Work

  • Environment-Specific Harnesses: Currently, a separate harness is generated for each environment. The authors envision distilling these domain-specific experts back into the base LLM to create a more generalized and recursively self-improving system.
  • Multimodal Games: The current work focuses on text-based games. Future work aims to extend this to more challenging multimodal games.
  • Library of Reusable Harnesses: Building a library of reusable harnesses for common game mechanics or rule types is a promising direction.
  • Two-Player Game Complexity: While the paper shows success in 2-player games, the authors note that more complex strategic reasoning (like MCTS) might be required for certain games, which could necessitate learning a code world model.

Summary updated

BibTeX
@inproceedings{lou2026autoharness,
  author = {Xinghua Lou and Miguel Lazaro-Gredilla and Antoine Dedieu and Carter Wendelken and Wolfgang Lehrach and Kevin Murphy},
  title = {{AUTOHARNESS: IMPROVING LLM AGENTS BY AUTOMATICALLY SYNTHESIZING A CODE HARNESS}},
  booktitle = {RSI @ ICLR},
  year = {2026},
  url = {https://openreview.net/forum?id=g9rEYVNn5T},
}
Figure