arXiv:2610.21901cs.LGOct 2026

Manuscript Offprint

arXiv:2610.21901

In-Context Learning Before Specialisation: Theory of Learning in a Solvable Topic Model

Francesco Cagnetta, Casper L. Christensen

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

Sequence models can generalise in fundamentally distinct ways: extrapolating structures seen in training, or using their prompt to adapt to novel tasks. The prevailing strategy determines how the model behaves beyond its training distribution, controlling potential capabilities and risks. Recent work has characterised many different strategies theoretically, and empirically described the transition between them as the quality and quantity of pretraining data vary. However, there is no learning theory for this competition, hence no fundamental understanding of why one solution might be favoured over another. Here we provide such a theory in a solvable topic model—documents drawn from a finite mixture of latent topics, mirroring the heterogeneity of modern pretraining data. The dataset comes with two distinct solutions: a specialised predictor, that stores the finite topic mixture, and an in-context one, optimal for topics never seen during training. Our theory predicts their emergence at distinct sample complexities: the in-context predictor is available early, while the specialised one emerges only after the training tokens outnumber the parameters of the data-generating process. Trained transformers reproduce both scales and the separating crossover in remarkable agreement with the theory, with no fitted parameters.

Figure

Figure

Figure 1: In-context learning and specialisation in a random topic model. Left: A random topic model consists of DD topics, i.e. unigram probabilities pz∈ΔK−1\mathbf{p}_z \in \Delta^{K-1} over a vocabulary of size KK. Training sequences are obtained by sampling a topic zz uniformly, then TT tokens i.i.d. from pz\mathbf{p}_z. Right: Learning curve of a one-layer transformer trained for next-token prediction on a random topic model with (K,D,T)=(32,512,128)(K, D, T) = (32, 512, 128). Initially, both In-Distribution (ID, on the DD training topics, blue) and Out-Of-Distribution (OOD, on a new topic p⋆\mathbf{p}_\star, orange) descend with NN towards the entropy of the Bayes-optimal OOD predictor (orange dotted): the add-one estimator rt(a∣X≤t)=(#{i≤t∣Xi=a}+1)/(t+K)r_t(a \mid X_{\leq t}) = (\#\{i \leq t \mid X_i = a\} + 1)/(t + K). Later, the predictor specialises to the training topics: the ID loss converges to the ID Bayes-optimal floor (blue dotted), while the OOD loss rises as in overfitting. Code: https://github.com/fracagnetta/random-topic-model.

∗^{*}Correspondence to: fr.cagnetta@gmail.com.

1 INTRODUCTION

Large language models generalise in ways that were not anticipated in training. In particular, novel behaviours can be elicited simply via prompting, including few-shot learning of supervised tasks (Brown et al., 2020) or adopting different roles depending on a set of instructions (Shanahan et al., 2023). This In-Context Learning (ICL) mechanism is not captured by the classical learning-theoretic paradigm, where a model is evaluated on fresh samples from the same the distribution it was trained on. Nevertheless, ICL is a critical source of both generality and risk (Anwar et al., 2024) in modern AI systems, calling for a theory of what trained models do beyond their training distribution.

An early theoretical attempt framed ICL as implicit Bayesian inference of the latent concept behind the prompt (Xie et al., 2022). This framework has found extensive support: trained transformers behave as Bayes-optimal estimators for many families of tasks (Garg et al., 2022), and how such estimators are represented and acquired during training is understood in simple settings (Nichani et al., 2024; Edelman et al., 2024; Lu et al., 2025; Zhang et al., 2025; Bondaschi et al., 2026). However, other settings admit distinct classes of predictors, and models transition between them as the training conditions vary (Akyürek et al., 2023). Raventos et al. (2023) identified task diversity as the key control parameter: When trained at low diversity, the transformer can only memorise the training tasks, acquiring general ICL abilities only above a certain task-diversity threshold.

The role of task diversity has been explored in synthetic data, revealing that the competition between the two behaviours extends along training time (Singh et al., 2023; Reddy, 2024; Nguyen & Reddy, 2025; Park et al., 2025; Gibson et al., 2026), culminating in a hierarchical Bayesian framework that fits the model behaviour to a weighted mixture of the memorising and generalising predictors (Carroll et al., 2025; Wurgaft et al., 2025). This framework is phenomenological: still missing is a microscopic theory that predicts the favoured solution from data properties alone, and how much data it takes for one solution to give way to the other.

Our Contribution Here we provide this theory in a synthetic dataset containing the minimal required ingredients: a task diversity parameter D, a vocabulary of size K, a document length T and a corpus of N training documents. Task diversity enters as a mixture of D latent topics from which documents are generated (Fig. 1, left). In this setting, we provide:

• A solvable model of strategy competition. We characterise analytically two distinct Bayes-optimal predictors: a specialised one, relying on knowledge of the D topics in the data-generating process, and an in-context one, equivalent to the add-one estimator from statistics under our data model (Sec. 2).

• A first-principles learning-curve theory for specialisation. We derive the specialisation learning curve in a Bayesian learning setting: the excess loss over the asymptotic floor is DK/(2NT)DK/(2NT) at leading order—a function of the number of training tokens per parameter of the data-generating process (Sec. 3).

• A statistical mechanism for the in-context phase. The in-context predictor is linear in the token counts, and a learner that estimates this map from data acquires it at a sample complexity set by its K2K^{2} parameters and by the information that a context carries about the next token: both independent of D. Hence, comparison with the specialisation scale gives a task-diversity threshold above which (see Fig. 1) the in-context solution emerges first, it remains stable in a window that widens with task diversity, then it gives way to the specialised solution (Sec. 4).

• Parameter-free agreement with trained transformers. Across vocabularies, task diversities, sequence lengths and corpus sizes, transformer in-distribution losses collapse as functions of NT/DKNT/DK, converging to the specialised solution at the predicted rate. Non-trivial ICL performance develops at the predicted, D-independent sample complexity, then vanishes again with specialisation (Sec. 5).

• Separation from classical spectral methods. Reconstructing the topics from low-order moments (Hsu et al., 2012; Anandkumar et al., 2014) requires resolving a Marchenko–Pastur bulk of eigenvalues, which takes N∼D2KN \sim D^2 K samples: a worse scaling with task diversity than the Bayes-optimal NT∼DKNT \sim DK followed by trained transformers (Sec. 5.1).

1.1 RELATED WORK

Mixtures of tasks as ICL testbeds. Finite task mixtures underlie most theoretical studies of ICL: linear regression (Garg et al., 2022; Akyürek et al., 2023; Raventos et al., 2023; Lu et al., 2025; Zhang et al., 2025; Bordelon et al., 2026; Letey et al., 2026), classification (Reddy, 2024; Nguyen & Reddy, 2025), and mixtures of Markov (Edelman et al., 2024; Park et al., 2025; Bondaschi et al., 2026; Gibson et al., 2026) and hidden Markov (Riechers et al., 2025; Ray et al., 2026) models. The “Balls & Urns” setting of Wurgaft et al. (2025) is precisely the random topic model studied here, and their memorising and generalising predictors are our specialised and in-context ones. Our work complements their phenomenological Bayesian framework: where they assume a scaling law and a low-Kolmogorov-complexity bias, we derive the sample complexities of the two predictors from first principles. Thus, our approach does not require estimating Kolmogorov complexities and fitting parameters, but rather prescribes a simple picture where the functional simplicity of the ICL solution promotes its emergence as long as it leads to a noticeable loss gain with fewer data.

Representations, Belief-state geometry and task vectors. Due to next-token pretraining, transformers linearly represent the belief state over latent structure (Shai et al., 2024). In particular, mixtures of independent components, or non-ergodic sources, give rise to a telescopic geometry, which sparsifies along the context (Ray et al., 2026). The random topic model is the simplest non-ergodic source, and our results predict the sample dependence of the resulting belief geometry. Beyond controlled data structures, ICL has been linked to task vectors encoding the inferred task (Hendel et al., 2023), whose geometry distinguishes dual modes of task inference corresponding to our two predictors (Yan et al., 2026). Overall, these approaches describe how a trained network implements the chosen strategies; ours predicts when the data favours each.

Learning theories of ICL. Most analytical studies of learning focus on the infinite-data and infinite-diversity limit. Lu et al. (2025) is closest to us: they solve linear attention trained on Gaussian regression in a proportional asymptotic limit, obtaining sharp learning curves and a genuine transition with increasing task diversity. However, since their linear model cannot fit the specialised predictors exactly, they did not observe the crossover from in-context to specialised predictor as the number of data increases. Furthermore, our setup can be extended to mixtures of Markov chains (Gibson et al., 2026) and generic hidden Markov models (Riechers et al., 2025).

References (55)