A Unified Theoretical Framework for Task Recognition and Task Learning in In-Context Learning

Zhixuan Pan, Li Cao, Yiqi Dong, Chenyu Gan, Shaowen Wang, Jian LiNeurIPS 2026

TL;DR

When you give an LLM a few examples in a prompt, is it genuinely learning a new rule on the fly, or just recognizing a pattern it already memorized during pre-training? It turns out it does both—and this paper proves mathematically why and when. By treating a finite-sized neural network like a strict information budget, the authors show that heavy-tailed training data naturally forces models to memorize frequent tasks while building a general-purpose learning algorithm for everything else.

What Problem Does This Paper Address?

In-context learning (ICL)—an LLM's uncanny ability to adapt to new tasks from a few prompt demonstrations without updating a single weight—is actually two distinct behaviors wearing a trench coat:

  1. Task Recognition: Identifying a familiar task from the prompt and retrieving pre-trained knowledge (e.g., realizing a prompt asks for English-to-French translation).
  2. Task Learning: Inferring genuinely novel input-output mappings on the fly during the forward pass (e.g., learning a brand-new, made-up cipher from examples).

Until now, theoretical explanations have lived in separate silos. One camp modeled ICL as pure Bayesian retrieval over known pre-training distributions; another reverse-engineered Transformers to show they can simulate algorithms like gradient descent in their activations. What has been missing is a unified theory explaining how finite model capacity and the messy, non-uniform distribution of pre-training data jointly force both modes to emerge and coexist inside the same model.

Methods

To bridge this gap, the authors pair Bayesian sequential prediction with rate-distortion theory (an information-theoretic way of asking: given a strict memory limit, what is the smartest thing to compress and keep?).

First, they model the pre-training corpus using an infinite species-sampling mixture (specifically, a Pitman–Yor process). Think of this as an endlessly growing ecosystem of latent tasks: as you scrape more of the internet, you mostly re-encounter popular "head" tasks, alongside a continual trickle of rare and brand-new "tail" tasks—capturing the classic Zipfian heavy-tailed statistics of natural language.

Next, they constrain the model's total capacity $C$, splitting its budget between two competing investments:

  • Task-specific memory: Mutual information retained in the weights about specific training tasks.
  • Shared inference algorithm ($\Omega$): A fixed, reusable mechanism that processes prompt context on the fly across all tasks.

Solving this optimization problem reveals how an optimal predictor allocates its finite parameters across tasks of varying frequencies, allowing the authors to derive formal error bounds for ICL as prompt length $n$ grows.

Datasets

The authors test their theory across two distinct arenas:

  • Controlled Synthetic Suite: Four custom stochastic task families—Linear Regression, Markov Models, nested bracket Dyck-$k$ languages, and Probabilistic Context-Free Grammars (PCFGs). Each model is trained in a single pass on 30 million documents whose task frequencies follow a Pitman–Yor heavy tail ($d = 0.8, \theta = 100$).
  • Real-World LLM Evaluation: Using SmolLM2-1.7B-Instruct, they construct binary category questions (e.g., "Is a peach a kind of fruit?") paired with 16 label sets ranging from ultra-frequent English (yes/no), to non-English languages (Italian, Hindi, Filipino), to completely abstract nonce symbols, testing both standard and "flipped" (reversed meaning) demonstrations.
Figure 1: Overview of the four sets of synthetic experiments: Each panel corresponds to the data generation model for a specific experiment and is divided into two sections by a dashed line. The upper section illustrates how a task is generated by a stochastic process governed by a task-specific parameter. The lower section displays a sample training document generated by that task, along with a brief description of its evaluation metric.

Key Insights and Ideas

  • The "Water-Filling" Capacity Split: When parameters are scarce, an optimal model allocates memory like pouring water into an uneven basin. Tasks above a critical frequency threshold ($p_{\text{crit}}$) get dedicated memory in the weights. Tasks below that threshold get strictly zero task-specific bits—ignoring them individually is mathematically optimal!
  • Why Heavy Tails Breed Generalization: Because the "tail" of rare tasks gets zero dedicated memory, how does the model minimize loss on them? If the collective probability mass of all rare tasks ($p_{\text{tail}}$) is large enough, it forces the model to invest capacity into the shared inference algorithm $\Omega$. Heavy-tailed data isn't a nuisance; it is the exact pressure that forces Task Learning to coexist with Task Recognition.
  • Two Distinct Speed Limits: Because the two regimes rely on fundamentally different mechanisms, their prediction errors shrink at different speeds as you add more examples ($n$) to the prompt:
  • Memorization Regime (Frequent tasks): Error drops exponentially ($\exp(-\Omega(n))$) as the prompt quickly disambiguates which stored task is active, bottoming out at a residual pre-training floor.
  • Generalization Regime (Rare/Unseen tasks): Because the model has to estimate continuous task parameters from scratch using context, error is lower-bounded by a much slower power-law decay ($\Omega(n^{-1})$).

Key Results

  • Clean Empirical Phase Transition: Across all four 30M-document synthetic experiments, binning tasks by pre-training exposure reveals a sharp divide. The top 60%–80% of training exposure is best fit by exponential decay curves, while the rarest 20%–30% of seen tasks and Out-Of-Distribution (OOD) tasks switch cleanly to power-law fits.
  • Near-Oracle In-Context Generalization: On rare and OOD synthetic tasks, the trained models' error curves closely hug an ideal Bayesian baseline that knows the true structural prior $G_0$, confirming the models learned a reusable inference algorithm rather than memorizing tail tasks.
  • Overriding Priors in SmolLM2-1.7B: When prompted with flipped labels (where yes means false), strong English priors fiercely resist Task Learning, hovering near 50% accuracy (random/equal preference) even after $K=24$ demonstrations. Weaker priors (Hindi/Filipino, abstract symbols) quickly abandon Task Recognition and adopt the reversed rule, climbing toward 90%–100% context-following accuracy.

Limitations and Future Work

While the main body focuses on establishing the foundational math, the authors rely on several idealizations to keep the information-theoretic derivations tractable:

  • During the capacity-allocation analysis, the grouping of training documents into latent tasks is treated as observed side information, and the exact KL divergence objective is simplified via an additive surrogate (accurate primarily when task posteriors separate cleanly).
  • Proving the exponential recognition bound requires a strict separability margin ($\Delta > 0$) and bounded log-likelihood ratios across candidate tasks.
  • In the real-world LLM experiment, true pre-training frequencies of label-question pairs could not be counted directly, requiring a heuristic, common-sense ordering of familiarity. Future work isn't explicitly mapped out in the main text, though relaxing these idealized separation assumptions and connecting the abstract cost $\ell(\Omega)$ directly to concrete Transformer circuit architectures are natural next frontiers.

Summary updated

Abstract

In-context learning (ICL) allows large language models (LLMs) to adapt to new tasks from a few examples without parameter updates. Previous work and our empirical studies suggest two modes in ICL: {\em Task Recognition}, which applies pre-trained knowledge to familiar tasks, and {\em Task Learning}, which generalizes to novel mappings during inference. However, a unified theoretical account of these behaviors remains lacking. We propose a new Bayesian prediction framework that models pre-training data using a {\em species-sampling mixture} over latent tasks to capture the continual emergence of new tasks as the pre-training corpus grows. By formulating an information-theoretic optimization problem, we derive the structure of the optimal ICL predictor under explicit capacity constraints. Our analysis characterizes optimal capacity allocation across task frequencies: a water-filling strategy that prioritizes high-frequency tasks while assigning zero task-specific information to rare ones. This gives rise to two regimes: for frequent tasks, the model performs \textit{Task Recognition}, with a prediction-error upper bound consisting of an exponentially decaying recognition term and a residual pre-training error. For rare or unseen tasks, the model performs Task Learning by executing an implicit inference algorithm, with a power-law lower bound on its expected prediction error. Our results provide a principled explanation for the dual nature of ICL and establish a direct connection between pre-training data distributions, model capacity, and in-context generalization behavior.
BibTeX
@inproceedings{pan2026unified,
  author = {Zhixuan Pan and Li Cao and Yiqi Dong and Chenyu Gan and Shaowen Wang and Jian Li},
  title = {{A Unified Theoretical Framework for Task Recognition and Task Learning in In-Context Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=mzLkPTQDvG},
}
Figure