Large Language Models can Implement Policy Iteration

Ethan Brooks, Logan A Walls, Richard Lewis, Satinder Singh

In the world of Reinforcement Learning (RL), the "holy grail" is an agent that can learn a new task rapidly through trial and error, without needing thousands of expert demonstrations or slow, compute-heavy gradient updates. Researchers have presented a fascinating step toward this goal: In-Context Policy Iteration (ICPI).

By treating a Large Language Model's (LLM) context window as a dynamic learning space, they demonstrate that a frozen model can improve its own behavior simply by "reading" its own history, effectively performing RL without weight updates.

The Problem with Current Approaches

Most current applications of foundation models to RL fall into two camps, both of which have significant drawbacks:

  1. Imitation Learners: These require massive datasets of expert demonstrations, which are labor-intensive and expensive to collect. Furthermore, these agents typically cannot outperform the experts they imitate.
  2. Fine-tuners: These adapt to tasks via gradient descent (updating weights or training adapter layers). This process is inherently slow and sacrifices the agile, "few-shot" learning capabilities that make LLMs so attractive in the first place.

This paper asks a fundamental question: Can we implement a classic RL algorithm—Policy Iteration—using only the in-context learning capabilities of an LLM? In other words, can a model look at a history of its own mediocre attempts, figure out what worked, and act more intelligently in the next step, all without changing its underlying weights?

Methodology: In-Context Policy Iteration (ICPI)

The authors introduce ICPI, a method that uses the LLM as both a world model (to predict future states) and a policy (to select actions).

In-Context Policy Iteration Architecture

The algorithm operates through a continuous feedback loop:

  1. The Environment as Text: The agent's state, available actions, and rewards are converted into structured, Python-like code snippets.
  2. Q-Value Estimation via Rollouts: To decide on an action, the LLM performs "mental simulations." Using the history provided in its prompt, it predicts future trajectories (states and rewards) for each possible action.
  3. Greedy Action Selection: The agent calculates the expected return (Q-value) for each action based on these rollouts and chooses the action that yields the highest predicted cumulative reward (the argmax).
  4. The Feedback Loop: As the agent interacts with the real environment, it adds these newly executed trajectories to an "experience buffer."
  5. Prompt Evolution: The prompt is iteratively updated with the most recent, higher-reward trajectories. This creates a self-improving cycle: better experience leads to better prompts, which improves the accuracy of the mental simulations, ultimately leading to better actions.

Crucially, the researchers utilized Codex, an LLM trained heavily on code. Its ability to handle structured logic and "Chain-of-Thought" reasoning proved essential for maintaining a consistent world model during rollouts.

Experimental Setup

The method was evaluated on six illustrative RL benchmarks designed to test specific agent capabilities:

  • Chain & Distractor Chain: Tests basic navigation and the ability to ignore "noise" in the state representation.
  • Maze: Tests 2D movement and obstacle avoidance.
  • Mini Catch: A simplified version of the classic paddle-and-ball game.
  • Mini Invaders: A simplified Space Invaders clone requiring sequential targeting.
  • Point-Mass: Tests the ability to handle continuous state spaces (encoded as floating-point numbers).

Key Results and Insights

The experiments revealed several critical insights about the viability of in-context RL:

Performance of ICPI vs Baselines across six environments
  • Rapid Convergence: In several tasks, ICPI found near-optimal policies within just 100–200 steps. This is significantly faster than traditional baselines like Tabular Q-learning in the same low-data regime.
  • Outperforming Imitation: ICPI consistently outperformed a "No ArgMax" baseline (which simply imitates successful trajectories in the prompt). This proves that the LLM's active simulation and Q-value evaluation are superior to simple pattern matching.
  • Learning Without Gradients: ICPI successfully distills the "improvement operator" of policy iteration into the prompt itself. The learning happens entirely within the model's activations, not its weights.
  • The Necessity of Scale and Structure: Only the largest model tested (code-davinci-001) consistently demonstrated the ability to learn. Furthermore, models trained on natural language performed poorly compared to code-trained models, suggesting that RL transitions map more naturally to programming logic than prose.
  • Prompt Balancing and "Hints": The LLM is not a perfect simulator out of the box. Providing "domain hints" (akin to Chain-of-Thought) helped the LLM accurately track state changes. Additionally, carefully balancing the ratio of terminal to non-terminal states in the prompt was necessary to prevent the model from becoming overly optimistic or pessimistic.

Limitations and Future Work

While ICPI is a promising proof-of-concept, it faces several hurdles before it can be applied to complex, real-world RL problems:

  • Context Window Bottlenecks: The agent's "memory" is strictly limited by the LLM's context window. As tasks grow in complexity, the prompt may run out of space for the necessary historical trajectories.
  • State Encoding: Converting complex environments (like high-resolution visual inputs) into text is a major challenge. Future iterations might require multi-modal LLMs capable of processing visual states directly.
  • Action Spaces: Currently, the method is best suited for discrete action spaces. Extending this framework to handle high-dimensional continuous control remains an open problem.

Summary updated

BibTeX
@inproceedings{brooks2023large,
  author = {Ethan Brooks and Logan A Walls and Richard Lewis and Satinder Singh},
  title = {{Large Language Models can Implement Policy Iteration}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=LWxjWoBTsr},
}
Figure