Abstract
This work presents In-Context Policy Iteration, an algorithm for performing Reinforcement Learning (RL), in-context, using foundation models. While the application of foundation models to RL has received considerable attention, most approaches rely on either (1) the curation of expert demonstrations (either through manual design or task-specific pretraining) or (2) adaptation to the task of interest using gradient methods (either fine-tuning or training of adapter layers). Both of these techniques have drawbacks. Collecting demonstrations is labor-intensive, and algorithms that rely on them do not outperform the experts from which the demonstrations were derived. All gradient techniques are inherently slow, sacrificing the "few-shot" quality that made in-context learning attractive to begin with. In this work, we present an algorithm, ICPI, that learns to perform RL tasks without expert demonstrations or gradients. Instead we present a policy-iteration method in which the prompt content is the entire locus of learning. ICPI iteratively updates the contents of the prompt from which it derives its policy through trial-and-error interaction with an RL environment. In order to eliminate the role of in-weights learning (on which approaches like Decision Transformer rely heavily), we demonstrate our algorithm using Codex, a language model with no prior knowledge of the domains on which we evaluate it.
Introduction
Foundation models have demonstrated remarkable capabilities in natural language processing and reasoning. However, their application to Reinforcement Learning (RL) has typically relied on two resource-intensive paradigms: large-scale curation of expert demonstrations or gradient-based fine-tuning. Expert demonstrations are costly to acquire and often cap the agent's performance at the level of the demonstrator. Conversely, gradient-based methods are computationally slow and undermine the rapid adaptation potential of in-context learning.
This work introduces In-Context Policy Iteration (ICPI), an algorithm that enables Large Language Models (LLMs) to perform RL tasks entirely within their context window. ICPI eliminates the need for expert data or weight updates by iteratively updating the prompt with the agent's own trial-and-error experiences, effectively implementing a model-based policy iteration algorithm.
Methods
ICPI utilizes a frozen LLM as both a world model and a policy engine. The core of the algorithm is a loop that alternates between environment interaction and prompt refinement.
The ICPI Loop
- Mental Simulations (Rollouts): For every possible action $a$ in the current state $s_t$, the LLM is prompted to predict future states, rewards, and episode termination. This "chain-of-thought" reasoning allows the model to simulate trajectories.
- Q-Value Estimation: The agent calculates the discounted sum of predicted rewards from these simulations to estimate the $Q$-value, $Q(s_t, a)$.
- Policy Improvement: The agent selects the action $a_t = \arg \max_a Q(s_t, a)$ to execute in the environment.
- Prompt Evolution: The actual trajectory (state, action, reward, next state) is appended to a replay buffer maintained within the LLM's prompt. As the prompt grows with more experiences, the model's internal world model and policy improve.
The authors leveraged Codex and represented environment states as Python-like snippets, capitalizing on the model's pre-trained code reasoning abilities.
Experimental Setup
The performance of ICPI was evaluated across six diverse RL benchmarks:
- Chain & Distractor Chain: Testing state transition learning and noise robustness.
- Maze: A $3 \times 3$ gridworld for spatial navigation.
- Mini Catch: A paddle-and-ball game for object tracking.
- Mini Invaders: A simplified shooter game requiring target prioritization.
- Point-Mass: A continuous-state physics task.
Key Results
ICPI demonstrates significant advantages over baseline methods, including "No arg max" (imitation of past successes) and "Matching Model" (nearest-neighbor retrieval).
- Rapid Convergence: In environments like Maze and Chain, ICPI reaches near-optimal performance within dozens of steps.
- Zero-Shot Generalization: Unlike tabular RL, the LLM can generalize from its context to handle novel state-action pairs, leading to faster early-stage learning.
- Scaling Effects: The ability to perform policy iteration appears to be an emergent property of scale. While the 185B parameter Codex model succeeded, smaller models (e.g., 6B parameters) often failed to improve.
Discussion and Limitations
While ICPI offers a path toward "instantly adaptive" agents, it faces several challenges. The context window limits the agent's long-term memory, and the computational cost of generating multiple rollouts per step is high. Additionally, the current implementation is restricted to discrete action spaces due to the requirement of iterating through all possible actions for the arg max selection.
Future work may leverage expanding context windows and more efficient inference techniques to deploy ICPI in complex, real-world environments where rapid, gradient-free adaptation is critical.
Summary updated