Large Language Models (LLMs) exhibit a remarkable capability known as In-Context Learning (ICL): by simply appending a few examples of a task to a prompt, the model can execute that task on a new input without any updates to its weights. To a traditional machine learning practitioner, this looks like magic—how can a frozen network "learn" a new function entirely during a forward pass?
The emerging consensus across recent research is that the model is not actually learning a new task from scratch. Instead, it is performing implicit Bayesian inference. Under this view, the pretraining phase equips the model with a vast library of latent concepts, algorithms, and tasks. When presented with a prompt containing demonstrations, the model treats these examples as statistical evidence to update its internal posterior distribution, identifying and selecting the pre-existing capability that best explains the context.
The Core Framework: Latent Task Identification
To build intuition for why this works, we must look at the next-token prediction objective used during pretraining. As Liyi Zhang et al. demonstrate, minimizing next-token prediction error forces a model's internal embeddings to act as predictive sufficient statistics that capture the latent generating distributions of the text. When an LLM encounters a prompt like Apple -> Red, Lime -> Green, Banana ->, it treats the sequence as a noisy observation generated by a hidden variable—a "latent task."
As formalized by Xie et al., ICL can be framed as a process where the model calculates a posterior predictive distribution over its pre-existing library of concepts. Rather than optimizing weights, the forward pass navigates this internal hypothesis space:
[Pretrained Knowledge Base] ---> (Prompt Examples as Evidence) ---> [Posterior Distribution over Tasks] ---> [Bayes-Optimal Prediction]
Wies et al. support this by modeling ICL as latent task identification, proving that the prompt acts as a signal to locate a specific task from the pretraining mixture distribution rather than synthesizing entirely new logic. This explains why ICL struggles with truly out-of-distribution tasks: as Qixun Wang et al. show, when faced with novel logic, the model simply attempts to map the input to the closest known function within its internal hypothesis space.
How Attention Implements Bayesian Model Averaging
How does a transformer architecture physically execute this mathematical abstraction? The answer lies in the attention mechanism.
Theoretical work by Yufeng Zhang et al. and Wei et al. demonstrates that transformers can implicitly implement Bayesian Model Averaging (BMA). Instead of picking a single task deterministically, the attention mechanism uses the prompt examples to estimate a posterior distribution over hidden task variables or causal structures. The final prediction is a weighted average of predictions from these latent "experts," proportional to how well each expert explains the provided context.
Furthermore, this process mirrors standard optimization algorithms. Akyürek et al. prove that under noisy conditions, this implicit inference allows the transformer to act as a minimum-Bayes-risk estimator, utilizing the prior distribution from pretraining to produce optimal predictions when faced with uncertainty in the input sequence.
Dynamics of Inference: Phase Transitions and Example Selection
This Bayesian perspective also sheds light on the internal dynamics of how a model processes a prompt. When the context is short, the model maintains high uncertainty, spreading its posterior probability across many potential latent tasks. However, as the number of context examples grows, the model re-weights its hypotheses.
Bigelow et al. observed this empirically using binary sequences, showing that models undergo sharp "phase transitions"—suddenly switching from random behavior to deterministic pattern-following as the evidence tips the scale toward a specific hypothesis.
Because the prompt serves as evidence for this internal inference step, the choice and order of examples matter immensely. Wu et al. demonstrate that selecting semantically similar examples facilitates this implicit inference, making it easier for the model to locate the correct task concept within its latent space during the inference process.
Explaining Downstream Phenomena
Viewing LLMs as latent variable models provides elegant explanations for several poorly understood behaviors across the deep learning lifecycle:
Catastrophic Forgetting
When a model is fine-tuned, it often appears to lose its pre-trained capabilities. However, Kotha et al. argue that fine-tuning does not erase these capabilities; instead, it skews the model's prior distribution, biasing it toward the fine-tuning data. The pre-trained capabilities remain internally accessible and can be recovered by using prompts (such as "Conjugate Prompting") that bypass this distributional skew to alter the implicit task inference.
Weak-to-Strong Generalization
When a strong model is trained on labels from a weaker supervisor, it can actually outperform the supervisor. Somerstep et al. and Polo et al. explain this through the Bayesian lens: the model treats the weak supervisor's examples merely as noisy evidence to point toward a pre-existing, clean latent concept. By treating the supervisor as a pointer rather than an absolute source of truth, the model leverages its superior pre-trained priors to filter out the supervisor's noise and reconstruct the true underlying intent.
Key Foundational Work
An Explanation of In-context Learning as Implicit Bayesian Inference (2021) — contributed 1 concept elements to this direction.
Conditional BRUNO: a neural process for exchangeable labelled data (2019) — contributed 1 concept elements to this direction.