NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning Game-Playing Agents with Generative Code Optimization

Zhiyi Kuang, Ryan Rong, YuCheng Yuan, Allen Nie

TL;DR

Instead of training black-box neural networks through millions of trial-and-error steps, this paper introduces a generative optimization approach where Atari game-playing policies are modular Python programs iteratively refined by a Large Language Model (LLM). Guided by object-centric state representations, execution traces, and staged natural language feedback, the programmatic agents match deep reinforcement learning (RL) performance on classic Atari games while cutting training time by 52% to 98%.

What Problem Does This Paper Address?

Standard deep RL algorithms (e.g., PPO, DQN) excel at complex games, but they suffer from severe sample inefficiency—often demanding millions of environment frames—and produce opaque policies that are virtually impossible to interpret, inspect, or formally verify.

Programmatic policies (explicit code defining agent behavior) offer transparency and modularity. However, optimizing code is difficult because programs are non-differentiable (rendering gradient descent useless), brute-force combinatorial search is intractable, and naive LLM code generation tends to produce brittle, ungrounded scripts. Furthermore, while recent LLM-based optimizers work on short-horizon tasks (under 10 steps), classic Atari games span thousands of steps with sparse rewards and difficult credit assignment challenges.

Methods

The authors cast game playing as an iterative programmatic control problem using the Trace framework:

  1. Object-Centric State Abstraction: Using OCAtari, raw pixel frames are converted into structured, object-level dictionaries capturing entity labels, coordinates $(x, y)$, sizes $(w, h)$, velocities $(dx, dy)$, and game status (lives, score).
  2. Modular Programmatic Policies: Agent policies are written as human-readable Python classes with explicit plan-and-act subroutines (e.g., predict_ball_trajectory, select_action, decide_shoot) decorated as optimizable modules (@trace.bundle(trainable=True)).
  3. Execution Tracing & Generative Optimization: The agent rolls out short trajectory sequences (15 to 400 steps to respect LLM context budgets). The OptoPrime optimizer (backed by Claude 3.5 Sonnet) analyzes the end-to-end execution traces and causal backtraces to localize logic failures and propose code revisions.
  4. Staged Natural Language Feedback: Because short rollouts capture only local game fragments, policies evaluated solely on short-horizon rewards plateau. The system evaluates complete games (~4,000 steps) and supplies tiered natural language feedback (Low, Medium, High performance levels) that instructs the LLM to attend to evolving mechanics, such as tunnel digging in Breakout or paddle positioning in Pong.

Datasets

The authors evaluate on three classic Atari 2600 environments via the Arcade Learning Environment (ALE) and OCAtari:

  • Pong
  • Breakout
  • Space Invaders

Key Insights and Ideas

  • Emergent Game Physics Discovery: Even when crucial environment dynamics (such as wall boundaries or ball deflection angles) are omitted from starter docstrings, the LLM deduces them directly from tabular trajectory logs. For instance, the optimizer discovered the exact coordinate of Breakout's right wall ($x = 152$) purely by spotting the frame where horizontal velocity flipped sign.
  • Overcoming Context Bottlenecks with Staged Language Feedback: LLMs cannot ingest full 4,000-step trajectory histories. Providing high-level, human-like linguistic feedback derived from full evaluation rollouts bridges the temporal credit assignment gap, preventing local-optima traps.
  • Code Complexity Dynamics: Policies do not bloat indefinitely. Tracking cyclomatic complexity and lines of code (LOC) reveals that code expands during initial exploration, then stabilizes or refactors into cleaner logic in later iterations.

Key Results

Using a single environment instance (compared to 8 parallel instances used by deep RL baselines), the code-based agents achieve competitive scores in a fraction of the time:

  • Pong: Scores 21 (perfect game) in 43 minutes (vs. DQN's 20 in 10h 6m; PPO's 19 in 2h 24m).
  • Breakout: Achieves a score of 353 in 1 hour 31 minutes (vs. DQN's 302 in 26h 54m; PPO's 443 in 3h 8m).
  • Space Invaders: Reaches a score of 1,200 in 36 minutes (vs. DQN's 1,383 in 26h 52m; PPO's 939 in 5h 39m).
  • Feedback Ablation: On Pong, an agent trained only on short rollout feedback capped out at a score of 7, whereas adding full-game staged feedback allowed it to hit the winning ceiling score of 21.

Limitations and Future Work

  • Reliance on Object Parsers: The method relies on object-centric representations (OCAtari) rather than learning perceptual representations directly from raw pixel arrays.
  • LLM Instability: LLM code generation can introduce occasional unstable or buggy code edits during intermediate iterations.
  • Prompt and Architecture Crafting: Performance still depends on human-designed functional interfaces, initial docstrings, and staged feedback thresholds.
  • Future Work: Developing autonomous feedback generation, extending the framework to more complex genres, and reducing prompt engineering overhead.

Summary updated

BibTeX
@inproceedings{kuang2025learning,
  author = {Zhiyi Kuang and Ryan Rong and YuCheng Yuan and Allen Nie},
  title = {{Learning Game-Playing Agents with Generative Code Optimization}},
  booktitle = {PRAL @ ICML},
  year = {2025},
  url = {https://openreview.net/forum?id=ZM65X3NoTd},
}
Figure