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

Transformers Provably Implement In-Context Reinforcement Learning with Policy Improvement

Haodong Liang, Lifeng Lai

TL;DR

This paper proves that a single linear self-attention transformer block can internally execute classical policy-improvement algorithms—specifically SARSA and actor-critic—entirely in-context without updating its own parameters. Crucially, the authors provide the first training convergence guarantee in the in-context reinforcement learning (ICRL) literature, showing that gradient descent successfully drives transformers toward these algorithmic structures.

What Problem Does This Paper Address?

In-context learning (ICL) allows large models to solve new tasks simply by conditioning on prompt examples, bypassing the need for weight updates. While theory has firmly established how transformers achieve this for supervised learning (by implicitly running variants of gradient descent), extending this understanding to In-Context Reinforcement Learning (ICRL) has proven difficult.

Prior theoretical work showed that transformers could perform policy evaluation (estimating how good a policy is via Temporal Difference learning). However, reinforcement learning requires a closed loop: an agent must not only evaluate a policy but also improve it based on experience. Furthermore, previous studies only proved that an ideal set of weights existed, leaving open the question of whether standard gradient-based training could actually discover them. This paper fills both gaps by tackling policy improvement and proving training convergence.

Methods

The authors analyze a single-layer, single-head transformer block utilizing linear self-attention.

  1. Explicit Construction: They mathematically construct exact weight configurations $(\mathbf{P}^\star, \mathbf{V}^\star)$ that allow the transformer to take a trajectory prompt and directly output updated policy parameters matching batch semi-gradient SARSA or batch actor-critic updates.
  2. Teacher-Mimicking Training: They introduce a training framework where the transformer is optimized to minimize the discrepancy between its output and the update rule of an analytical "teacher" algorithm.
  3. Gradient-Flow Analysis: To prove convergence, they analyze the continuous training dynamics (gradient flow). Because the optimal weights form a continuous manifold rather than a single point (due to scaling symmetries), standard strong-convexity proofs fail. Instead, they establish a local Polyak–Łojasiewicz (PL) inequality on a tubular neighborhood around the optimal manifold to guarantee exponential convergence.
Figure 1: A simplified pipeline of transformer’s ICRL implementation. The agent samples a trajectory under the current policy, which is fed into the transformer block. The transformer outputs an updated parameter w, which is then used to update the value function and policy.

Datasets

The theory was empirically validated using randomly generated tabular Markov Decision Processes (MDPs):

  • Environment Size: 9 states and 4 actions, with a discount factor ($\gamma$) of 0.5.
  • Generation: Transition kernels were drawn from a uniform Dirichlet distribution, and rewards were sampled uniformly from $[-1, 1]$.
  • Trajectories: Window lengths of $n=20$ steps were rolled out over a horizon of $T=1000$ steps to form the in-context prompts.

Key Insights and Ideas

  • Outputting Parameters, Not Just Values: Unlike prior models that only outputted a value estimate for a specific query state, this transformer architecture directly outputs the updated parameter vector ($\mathbf{w}$ for SARSA, or $\lambda$ and $\mathbf{w}$ for actor-critic). This allows the agent to immediately use these parameters to sample new trajectories, effectively closing the policy-improvement loop in-context.
  • The Optimization Landscape is a Manifold: The optimal solution is not an isolated point but a continuous manifold of equivalent parameter configurations. Under specific "richness conditions" (ensuring the training MDPs provide enough excitation in the data), the authors prove that gradient flow contracts locally toward this manifold.

Key Results

  • Mathematical Proofs: Theorem 3.1 and Theorem 3.3 formally guarantee that linear attention can perfectly implement batch SARSA and actor-critic updates. Theorem 3.2 provides the first local convergence guarantee for ICRL training dynamics.
  • Weight Recovery: Empirically, when trained on 10,000 random MDPs, the transformer's learned $\mathbf{P}$ and $\mathbf{V}$ matrices precisely recovered the sparse block structures predicted by the explicit theoretical constructions.
Figure 2: Final learned P and V matrices after training, for the SARSA transformer (top row) and the Actor-Critic transformer (bottom row). In both settings, the non-zero structure matches the theoretical constructions (P⋆, V⋆) in (5) and (14).
  • Strong Control Performance: When deployed on unseen test MDPs, the trained transformer closely tracked the performance of the analytical teacher algorithms, driving the agent rapidly toward oracle-level returns over long in-context horizons.
Closed-loop control performance on held-out random MDPs

Limitations and Future Work

  • Verification of Richness Assumptions: The convergence proof relies on structural richness assumptions regarding the trajectory distribution, which can be challenging to formally verify for complex, non-tabular MDP families.
  • Local Convergence Only: The training guarantee is local, meaning the transformer must be initialized sufficiently close to the optimal manifold. Global optimization guarantees remain an open challenge.
  • Simplified Architecture: The analysis is restricted to deterministic gradient flow and linear attention. Extending these proofs to stochastic gradient descent and standard softmax attention is left for future research.

Summary updated

BibTeX
@inproceedings{liang2026transformers,
  author = {Haodong Liang and Lifeng Lai},
  title = {{Transformers Provably Implement In-Context Reinforcement Learning with Policy Improvement}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=CRstYxsyfj},
}
Figure