In-Context Learning (ICL) is the phenomenon whereby a pretrained language model adapts its predictions to a new task specified entirely through input-sequence demonstrations—without any gradient updates to model parameters. It matters because it challenges classical statistical learning theory, which requires parameter updates for generalization, suggesting that transformers implicitly implement a distinct inference-time learning algorithm encoded in their weights during pretraining. Remarkably, ICL performance can match or exceed fine-tuned models on many benchmarks, yet the model performing the task retains no persistent memory of having done so once the context window is cleared.
The core insight
A transformer performing in-context learning is not merely pattern-matching to memorized examples—it is, in a precise sense, executing a learning algorithm encoded implicitly in its weights. The forward pass through attention layers implements something functionally equivalent to gradient descent on a small dataset, where the "parameters" being updated are the attention keys and values rather than the model's weights. This means pretraining is not just learning facts; it is learning how to learn from demonstrations. The aha moment: the model's weights are the optimizer, and the context is the training set.
How It Works
Formal Setup
Let a pretrained autoregressive language model be parameterized by θ, defining a conditional distribution pθ(y∣x). An in-context learning prompt is a sequence:
1
C=[(x1,y1),(x2,y2),…,(xk,yk),xquery]
Explanation: The context C is a flat token sequence containing k input-output demonstration pairs followed by a query input, all concatenated with task-specific delimiters. At inference the model computes:
2
y^=argymaxpθ(y∣x1,y1,…,xk,yk,xquery)
Explanation: The predicted label is the highest-probability completion of the concatenated context, with no parameter update occurring anywhere in this process.
Bayesian Concept Inference Interpretation
Xie et al. (2021) formalize ICL as Bayesian inference over a latent concept c drawn from a prior induced by the pretraining corpus. Define a latent variable model where:
Explanation: The model marginalizes over all latent concepts c (data-generating functions present in the pretraining distribution), weighting each by its posterior probability given the demonstrations. Under mild regularity conditions—specifically that the pretraining corpus is generated by a Hidden Markov Model (HMM) with a mixture of concept distributions—the transformer's attention mechanism provably concentrates the posterior p(c∣C) on the true concept as k→∞, yielding:
4
k→∞limDKL(pθ(y∣C)p(y∣xquery,c∗))=0
Explanation: As demonstration count grows, the model's predictive distribution converges in KL-divergence to the oracle distribution under the true concept c∗, provided c∗ is in the support of the pretraining concept prior.
Implicit Gradient Descent Interpretation
Akyürek et al. (2022) and von Oswald et al. (2023) prove constructively that a linear self-attention layer implements one step of gradient descent on a least-squares regression problem. For a single linear attention layer with weight matrices WK,WQ,WV, the layer's output on the query token is:
5
β^=(X⊤X)−1X⊤Y
Explanation: The attention output for the query is algebraically equivalent to the ordinary least-squares solution β^ obtained from the in-context demonstration matrix X∈Rk×d and label vector Y∈Rk, meaning self-attention computes a closed-form learner. Here X is the matrix of k in-context input embeddings, Y is the corresponding label embedding matrix, and d is the embedding dimension. Crucially, this result holds exactly for linear attention without softmax normalization, and approximately for softmax attention in the low-temperature regime. Extensions by Dai et al. (2023) demonstrate that multi-layer transformers implement multi-step gradient descent, with each layer corresponding to one gradient step with an implicit learning rate determined by the attention temperature.
Task Diversity and Sample Complexity
Let T be the space of tasks seen during pretraining and Ttest be the test task distribution. Hahn & Goyal (2023) derive a PAC-learning-style bound on ICL generalization error ϵICL:
6
ϵICL≤ϵapprox+O(klog∣T∣)+dH(T,Ttest)
Explanation: ICL error decomposes into an approximation error from model capacity (ϵapprox), a demonstration-count term that decays as k−1/2 with task diversity ∣T∣, and a distribution shift term dH measuring how far test tasks lie from the pretraining task manifold. This bound makes precise why scale matters: larger models reduce ϵapprox, and more diverse pretraining corpora reduce dH(T,Ttest), explaining the empirical observation that ICL performance improves superlinearly with model scale (Brown et al., 2020).
researcher · Sang Michael Xie | b. 1997 | Bayesian Formalization of In-Context Learning
Xie is a PhD student at Stanford University working with Percy Liang and Tengyu Ma, focusing on the theory of foundation models and distribution shift. His 2021 NeurIPS paper "An Explanation of In-Context Learning as Implicit Bayesian Inference" provided the first rigorous probabilistic framework for ICL, proving that transformer pretraining on HMM-structured corpora induces a prior over concepts that enables posterior-consistent in-context inference. Remarkably, this theoretical framework was developed concurrently with GPT-3's empirical demonstrations of ICL, meaning the theory arrived almost simultaneously with the phenomenon it explained.
researcher · Ekin Akyürek | b. 1997 | Algorithmic Equivalence of Attention and Gradient Descent
Akyürek is a PhD student at MIT CSAIL whose 2022 ICLR paper "What Learning Algorithm is In-Context Learning? Investigations with Linear Models" constructively proved that transformers can implement gradient-based learning algorithms through their attention mechanism. He showed that a specific configuration of transformer weights exactly reproduces ridge regression on in-context examples—providing the mechanistic "how" to complement Xie's "why." This work bridged the gap between empirical ICL observations and classical learning theory in a way that surprised even established researchers in the field.
Figure
In-Context Learning Data Flow
Figure
Bayesian Concept Posterior Concentration
Intuition
The Mental Model
Imagine you are a polyglot consultant who has read millions of documents in dozens of domains. When a client hands you a memo with five example translations from legalese to plain English, you do not learn what legalese means—you already know it. Instead, the examples tune your register: they tell you how formal this client wants the output, what jargon they find acceptable, and that this conversation is about translation rather than summarization. In ICL, the model's weights are the totality of this expertise, and the context window is the client briefing that activates the right slice of it. The demonstrations are not a training set; they are a calibration signal—a way of pointing to one function from the enormous family of functions the model already knows how to compute. This is why randomizing labels in demonstrations barely hurts: you already know English-to-French translation; the label format is mainly telling you that translation is what's wanted here, not teaching you vocabulary.
Historical Development
Origins · 1 minhow this was discovered
History
2020
Brown et al. publish "Language Models are Few-Shot Learners" (GPT-3, NeurIPS 2020), demonstrating that a 175B parameter autoregressive model shows strong ICL across dozens of tasks from raw demonstrations—the phenomenon is named and characterized empirically for the first time at scale.
2021
Xie et al. publish "An Explanation of In-Context Learning as Implicit Bayesian Inference" (NeurIPS 2021), providing the first formal theoretical account of ICL as posterior inference over latent concepts induced by pretraining corpus structure, grounding the phenomenon in classical probabilistic learning theory.
2022
Min et al. publish "Rethinking the Role of Demonstrations" (EMNLP 2022), showing through controlled ablations that label correctness in demonstrations contributes surprisingly little to ICL performance—input distribution and output format matter more—fundamentally revising the community's understanding of what demonstrations communicate.
2022
Akyürek et al. publish "What Learning Algorithm is In-Context Learning?" (ICLR 2023, posted 2022), proving constructively that transformers can implement gradient descent through their attention mechanism, establishing the mechanistic foundation for ICL as implicit optimization.
2023
von Oswald et al. publish "Transformers Learn In-Context Learning by Gradient Descent" (ICML 2023), showing that transformers trained from scratch on regression tasks converge to weights that implement multi-step gradient descent, and that this structure emerges naturally from meta-learning on diverse tasks.
2023
Dai et al. publish "Why Can GPT Learn In-Context?" (ACL Findings 2023), formalizing the dual form between ICL and fine-tuning and proving that ICL implicitly performs gradient descent with an effective learning rate determined by the attention softmax temperature.
2023
Wei et al. and Turpin et al. investigate ICL failure modes and chain-of-thought augmentation, showing that reasoning-heavy tasks require extended context traces (scratchpads) rather than pure input-output demonstrations—extending ICL theory to structured inference.
2024
Adaptive context length selection and retrieval-augmented demonstration selection become active research frontiers, with theoretical work on optimal demonstration ordering and sample complexity of ICL under distribution shift becoming central open problems.
Why It Matters
Real-World Impact
GPT-4 (OpenAI, 2023)GPT-4's API exposes ICL as a first-class interface through system prompts and few-shot examples in the message history; benchmark results show GPT-4 reaching near-human performance on bar exam questions with zero-shot ICL, demonstrating that pretraining at sufficient scale can encode enough task knowledge that context alone suffices for specialization.
Claude (Anthropic, 2023–2024)Constitutional AI fine-tuning interacts with ICL by shaping which latent concepts the model's posterior concentrates on; Anthropic's research shows that RLHF and ICL are not orthogonal—RLHF modifies the pretraining-induced concept prior, making certain behaviors more accessible via in-context specification.
Codex / GitHub Copilot (OpenAI/Microsoft)Code completion exploits ICL by treating the preceding codebase as a demonstration context; the model infers library idioms, variable naming conventions, and API patterns from the file context without any fine-tuning, making the entire working repository an implicit few-shot prompt.
PaLM-2 and Gemini (Google DeepMind, 2023)Chain-of-thought prompting—a structured extension of ICL where demonstrations include intermediate reasoning steps—dramatically improves multi-step mathematical and logical reasoning, with PaLM showing that ICL with CoT demonstrations reaches competitive performance on GSM8K (arithmetic) and MATH benchmarks without task-specific fine-tuning.
BioMedLM / Med-PaLM (Stanford/Google, 2023)Medical question-answering systems use ICL with expert-curated few-shot examples to align general-purpose language models to clinical reasoning styles; ICL provides a parameter-efficient alternative to full medical fine-tuning, enabling rapid deployment across medical specialties with different demonstration sets.
LLaMA-3 / Mistral-based agents (Meta/Mistral AI, 2024)Open-source models demonstrate that ICL generalizes across model families at sufficient scale (≥7B parameters), enabling the research community to study ICL theory empirically; tool-use and function-calling are framed as structured ICL where the tool schema is part of the demonstration context.
Retrieval-Augmented Generation (RAG) systemsRAG architectures (Lewis et al., 2020) combine dense retrieval with ICL by dynamically selecting the most relevant demonstrations from a corpus for each query; this operationalizes the theoretical insight that demonstration relevance determines how sharply the concept posterior concentrates, turning ICL sample complexity bounds into engineering design criteria for retrieval.
Prerequisites
What This Leads To
See Also
Transformer Architecture · Few-Shot Learning · Prompt Engineering and Optimization · Meta-Learning · Emergent Capabilities in Large Language Models