In-Context Learning as Implicit Policy Gradient

Masahiro Kaneko, Timothy Baldwin

TL;DR

This paper provides a theoretical bridge between In-Context Learning (ICL) and Reinforcement Learning. The authors prove that when a Large Language Model (LLM) is prompted with examples paired with quality scores, its internal attention mechanism acts as an implicit policy gradient optimizer (specifically the REINFORCE algorithm), shifting its output distribution toward higher-reward responses without needing any parameter updates.

What Problem Does This Paper Address?

We know that LLMs can "learn" from examples provided in a prompt—a phenomenon called In-Context Learning. Most theoretical work has focused on supervised ICL, where the model sees input-output pairs $(x, y)$. However, recent empirical successes like "Optimization by PROmpting" (OPRO) and self-rewarding models use a different flavor: score-conditioned ICL. In this setting, the model is given multiple attempts at the same task paired with scalar rewards or scores $(y, r)$.

While this works well in practice, we haven't had a principled understanding of why. Is the model just copying the best example, or is it performing a more sophisticated optimization? This paper seeks to formalize the computational mechanism underlying score-conditioned generation.

Methods

The authors approach the problem through both theoretical proofs and mechanistic interpretability:

  1. Constructive Proof: They demonstrate that a specific configuration of transformer weight matrices allows the self-attention mechanism to implement reward-weighted aggregation. Essentially, the "Key" and "Query" matrices extract the score, while the "Value" matrix extracts the output embedding, resulting in a hidden-state update that is mathematically equivalent to a REINFORCE policy gradient step.
  2. Trust-Region Analogy: They derive an exact upper bound on the KL divergence (distribution shift) induced by these attention updates. This suggests that ICL naturally behaves like "trust-region" policy optimization, preventing the model from deviating too drastically from its base distribution.
  3. Mechanistic Analysis: To see if real-world models actually do this, they analyzed five open-weight LLMs (including Llama-3 and Olmo-3). They used linear probing to see if scores are encoded in hidden states and causal interventions (amplifying or suppressing specific attention heads) to see how it affected the model's behavior.

Datasets

The theory was validated across four diverse tasks:

  • DROP: A reading comprehension benchmark (evaluated via F1 score).
  • GSM8K: Mathematical word problems (evaluated via exact match).
  • HH-RLHF: A dataset focused on helpfulness and harmlessness.
  • JailbreakBench (JBB): A benchmark for adversarial "jailbreak" prompting (evaluated via attack success rate).

The authors tested these on a wide range of models, from smaller open-weights like Qwen3-4B to frontier models like GPT-4o and Claude Sonnet 4.

Key Insights and Ideas

  • Attention as an Optimizer: The most striking insight is that the transformer architecture is fundamentally "pre-wired" to perform optimization. The forward pass isn't just static inference; it's a dynamic process that can emulate gradient descent on the fly.
  • Score-Specialized Heads: The researchers identified specific attention heads in the middle-to-late layers of LLMs that are "specialized" for score extraction. When these heads were amplified, the model's sensitivity to scores increased; when suppressed, the "learning" effect vanished.
  • Implicit Regularization: The bounded nature of attention updates provides a theoretical explanation for why ICL is often more stable than fine-tuning. It has a built-in "trust region" that keeps the model's behavior within reasonable bounds.
  • Format Robustness: The effect persists across various ways of representing scores (decimals, percentages, or even natural language), though numeric formats are the most effective.

Key Results

  • High Correlation: There is a strong Spearman correlation (up to 0.74 in Llama3-70B) between the scores provided in the context and the change in the model's output probabilities.
  • Directional Alignment: The distribution shift produced by ICL has a high cosine similarity (exceeding 0.45) with the shift produced by explicit REINFORCE fine-tuning. This confirms that ICL and RL are moving the model in the same "direction" in the embedding space.
  • Iterative Improvement: In iterative settings, models showed rapid score improvements in early rounds that eventually plateaued, consistent with the theoretical KL-bound predictions.

Limitations and Future Work

  • Simplified Models: The formal proofs rely on linear attention and single-layer assumptions. While empirical evidence suggests these hold for multi-layer, non-linear models, a full formal proof for complex architectures remains an open challenge.
  • The "Identity" Assumption: The correspondence is most exact when the output layer acts as an identity map ($W_{out} \approx I$). In practice, the output map is more complex, making the relationship a "structural analogy" rather than a perfect identity.
  • Convergence: The paper proves that each step is a policy gradient step, but it does not guarantee that iterative ICL will always converge to a global optimum.
  • Future Directions: The authors suggest that this framework could lead to better "inference-time alignment" techniques, allowing models to be fine-tuned for safety or specific tasks purely through clever prompting rather than expensive weight updates.

Summary updated

BibTeX
@inproceedings{kaneko2026context,
  author = {Masahiro Kaneko and Timothy Baldwin},
  title = {{In-Context Learning as Implicit Policy Gradient}},
  booktitle = {COLM},
  year = {2026},
  url = {https://openreview.net/forum?id=U7yVG5V29x},
}
Figure