TLDR: A new research paper introduces a method where AI agents learn to play Atari games by optimizing their own Python code policies using large language models (LLMs). This approach, called generative code optimization, allows agents to self-improve through execution traces and natural language feedback. It achieves performance comparable to deep reinforcement learning baselines with significantly less training time and interactions, and demonstrates that LLMs can infer complex game dynamics without explicit programming.
Artificial intelligence has made incredible strides in recent years, particularly in creating agents that can master complex tasks like playing video games. However, a persistent challenge remains: how to make these agents learn efficiently and in a way that humans can easily understand. Traditional methods, often relying on deep reinforcement learning, can require vast amounts of training data and produce “black box” policies that are difficult to interpret or verify, which is a major concern for critical applications.
A new research paper titled “Learning Game-Playing Agents with Generative Code Optimization” by Zhiyi Kuang, Ryan Rong, YuCheng Yuan, and Allen Nie from Stanford University introduces an innovative approach to tackle this problem. Their method proposes treating an agent’s decision-making policies not as complex neural networks, but as self-evolving Python programs. These programs take the current game observation as input and output an action, allowing agents to improve themselves through execution traces and natural language feedback, with minimal human intervention.
Policies as Evolving Code
The core idea is to represent an agent’s behavior as explicit, modular Python code. This “programmatic policy” offers several advantages, including interpretability, modularity, and the potential for formal verification. Unlike neural networks, which are non-differentiable and hard to optimize with traditional code methods, this research leverages large language models (LLMs) for generative optimization. The LLM acts as a “code optimizer,” iteratively refining the policy code based on feedback.
To facilitate this, the researchers use the Trace framework, which captures detailed records of the agent’s interactions with the environment, known as “execution traces.” These traces provide rich, structured feedback to the LLM, allowing it to pinpoint where the policy might be failing and suggest meaningful code updates. This is a significant departure from prior work, as it focuses on optimizing the policy code itself, extracting detailed execution traces, and using graph-based backtracing with LLMs for updates.
Interpretable Game States and Guided Learning
To make the game environment more understandable for the LLM, the approach utilizes Object-Centric Atari environments (OCAtari). Instead of raw pixels, OCAtari converts observations into object-level representations, providing key information like coordinates, size, and velocity for each game object (e.g., the ball, paddle, or bricks). This abstraction allows the agents to reason over gameplay dynamics more efficiently and interpretably.
The agent’s policy is designed with a high-level plan-act interface, consisting of decision-making functions like `predict_ball_trajectory` or `select_action`. These functions are marked as “trainable,” meaning the LLM can optimize them. For instance, in Pong and Breakout, the policy includes a function to predict the ball’s future position, which then informs the paddle’s movement. In Space Invaders, separate functions handle shooting and movement decisions.
A crucial aspect of their learning design is “staged feedback.” The agents are trained in an episodic reinforcement learning setup, where each iteration involves a rollout and evaluation. The LLM receives natural language feedback derived from full-length evaluation episodes. This feedback is tailored to different performance levels (e.g., “Good job! You’re close to winning” for high scores, or “Your score is -5 points. Try to improve paddle positioning” for low scores). This staged approach helps guide the LLM, especially in games with evolving dynamics or sparse rewards, by directing its attention to specific game mechanisms or high-level strategies.
Impressive Results and Emergent Understanding
The approach was evaluated on three classic Atari games: Pong, Breakout, and Space Invaders. The results are compelling: the game-playing Python programs achieved performance competitive with established deep reinforcement learning baselines like DQN and PPO. What’s more, this was accomplished with significantly less training time (reductions ranging from 52% to 98%) and fewer environment interactions.
One of the most surprising findings was the LLM optimizer’s ability to infer underlying game dynamics and constraints without explicit programming. For example, in Breakout, the LLM (specifically OptoPrime) was able to correctly identify the exact position of the right wall by analyzing ball position and velocity changes in the execution traces. It also learned accurate ball physics, such as bounce mechanics, without being explicitly told these details. This highlights the LLM’s capacity for causal reasoning over long, sparse sequences of events.
The researchers also tracked the evolution of the policy code’s complexity. They observed that policies grew significantly in length and structural complexity during optimization, measured by lines of code, cyclomatic complexity, and nested if-depth. Interestingly, complexity often plateaued or even slightly decreased in later iterations, suggesting the model reorganizes logic for efficiency rather than simply expanding it indefinitely.
Also Read:
- Language Models Crafting Sound Policies for Automated Planning
- ReST-RL: Enhancing LLM Code Reasoning Through Optimized Self-Training and Value-Guided Decoding
Future Prospects
While the approach shows immense promise, the researchers acknowledge some limitations. LLMs can occasionally introduce unstable edits, and the performance can be sensitive to the precise wording of prompts, especially given the context window constraints of current models. Nevertheless, this work introduces a novel framework for agent learning that combines programmatic reasoning with language-based optimization, particularly effective in sparse-reward settings.
This research opens exciting avenues for creating more interpretable, efficient, and adaptable AI agents. By making policies human-readable and allowing LLMs to refine them through structured feedback, we move closer to a future where AI systems are not just powerful, but also transparent and trustworthy. You can read the full research paper here.


