In-Context Learning Is Provably Bayesian Inference: A Generalization Theory for Meta-Learning

Tomoya Wakayama, Taiji Suzuki

Introduction

Large Language Models (LLMs) exhibit a remarkable capability known as In-Context Learning (ICL): they can adapt to new tasks simply by observing a few prompt examples, without any weight updates. Despite its empirical success, the theoretical underpinnings of ICL have remained elusive. Existing theories often fall short in two areas: they struggle to account for the heterogeneous mix of tasks seen in real-world pretraining, and they lack a finite-sample theory explaining how the number of pretraining prompts ($N$) and prompt length ($p$) interact.

This paper bridges that gap by framing ICL as a meta-learning problem, providing rigorous mathematical proof that Transformers perform implicit Bayesian inference. Rather than merely memorizing patterns, Transformers learn to identify and solve specific task types from a few examples with exponential efficiency.

The Bayes-Centric Framework

To analyze ICL, the authors utilize a Bayes-centric framework applied to a simplified but representative architecture: the uniform-attention Transformer. The cornerstone of their analysis is a "Risk Identity" that decomposes the total ICL error into two orthogonal components:

  • The Bayes Gap: This measures approximation error—how much better an ideal Bayesian predictor would perform compared to the trained Transformer. It is heavily dependent on the pretraining phase.
  • The Posterior Variance: This represents intrinsic uncertainty. Even with a perfect model, some ambiguity remains in the task itself. This variance depends on the number of examples provided at test time.

By leveraging sequential learning theory and optimal transport-based approximation, the authors calculate how quickly a model converges to the optimal meta-algorithm during pretraining.

Key Insights and Scaling Laws

  • The $pN$ Scaling Law: The authors derive a non-asymptotic upper bound for the Bayes Gap, revealing that the error shrinks at a rate proportional to $1/(pN)$. This establishes a direct coupling between the number of pretraining prompts ($N$) and their context length ($p$). A model can compensate for fewer pretraining prompts if those prompts are longer, and vice versa.
  • Exponential Task Identification: Real-world pretraining involves a mixture of tasks. The theory demonstrates that a model's uncertainty about which task it is currently performing vanishes exponentially fast. After just three or four in-context examples, the model effectively "locks in" on the correct task type.
  • Out-of-Distribution (OOD) Stability: When evaluating distribution shifts between pretraining and test prompts, the authors found that the Bayes Gap increases linearly with the Wasserstein distance (a measure of divergence between two distributions). However, the Posterior Variance remains an intrinsic property of the task itself.

Empirical Validation

Because calculating a true Bayesian predictor is virtually impossible for real-world text, the authors validated their theory using synthetic regression mixtures (e.g., linear vs. non-linear/tanh regression). This allowed them to compare the Transformer against a ground-truth Bayesian predictor.

Using a GPT-2 architecture, they demonstrated that the Transformer's error curve closely tracks the theoretical "Bayes (mixture)" curve. As more examples are added to the prompt, the model's performance rapidly converges to the "Bayes (oracle)" curve—the performance of a model that explicitly knows the task beforehand. Furthermore, empirical power-law fits confirmed that a joint model accounting for both $p$ and $N$ explains the data significantly better ($R^2 = 0.912$) than models analyzing either variable in isolation.

Limitations and Future Work

While the paper provides a robust theoretical foundation, it acknowledges several limitations. The primary proof relies on uniform-attention Transformers; extending this to standard softmax attention is mathematically complex due to data-dependent attention weights. Additionally, the theory assumes a finite number of task types, whereas real-world task spaces are likely continuous or infinite. Finally, the analysis is built on squared error loss, which is standard for regression but different from the cross-entropy loss typically used for LLM token prediction. Future work will need to adapt these theoretical tools to broader, more complex architectures and loss functions.

Summary updated

BibTeX
@inproceedings{wakayama2026context,
  author = {Tomoya Wakayama and Taiji Suzuki},
  title = {{In-Context Learning Is Provably Bayesian Inference: A Generalization Theory for Meta-Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=BUFSSOuphA},
}
Figure