A Unified Theoretical Framework for Task Recognition and Task Learning in In-Context Learning
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.