arXiv:2610.05717stat.MLOct 2026

Manuscript Offprint

arXiv:2610.05717

Retrieval-Based In-Context Learning: A Domain Adaptation Framework

Yilun Zhu, Naihao Deng, Yingcong Li, Naichen Shi, Clayton Scott

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

Abstract

In-context retrieval (ICR) is a retrieval-based form of in-context learning (ICL) in which demonstrations are retrieved from a source database based on similarity to the query, rather than sampled independently. In this work, we formulate ICR as a type of domain adaptation problem, where the source distribution P of the database may differ from the target distribution Q of the test query-label pair. We investigate the performance of ICR under a flexible class of distributional shifts that substantially extends prior work [Li et al., 2024b, Guo et al., 2025], and establish theoretical guarantees that quantify the benefits and pitfalls of this learning paradigm. Our theory is verified by experiments on synthetic and language tasks.

1 Introduction

In-context learning (ICL) is the ability of a pretrained model to predict the label of a query point using input-output demonstrations provided in its context, without updating the model's parameters. In most theoretical models of ICL, these demonstrations are modeled as i.i.d. draws from the same distribution that governs the query-label pair [Garg et al., 2022, Guo et al., 2024]. Throughout this paper, ICL refers to this standard setting with i.i.d. demonstrations.

In this work, we consider retrieval-based ICL, in which the input-output demonstrations are selected as a function of the query point. In particular, given access to a large database of demonstrations, a retrieval algorithm selects examples whose feature representations are similar to that of the query point. It has been observed empirically that such retrieved demonstrations can substantially improve performance over randomly sampled demonstrations [Liu et al., 2022, Rubin et al., 2022, Izacard et al., 2023, Huang et al., 2024]. Following recent work, we refer to this setting as in-context retrieval (ICR) [Luo et al., 2024, Guo et al., 2025].

Prior work has begun to explain the benefits of ICR theoretically, but several important regimes remain unexplored. In particular, existing statistical models [Li et al., 2024b, Guo et al., 2025] are restricted to linear-Gaussian settings. Moreover, while these models allow the retrieved demonstrations and the query-label pair to be governed by different marginal distributions, the regression function remains the same: in other words, they model covariate shift, but not a posterior drift that changes the optimal predictor.

We introduce a nonparametric statistical framework for binary classification that casts ICR as a domain adaptation problem admitting both covariate shift and posterior drift. The source domain governs the corpus of demonstrations, the target domain governs the query-label pair, and the predictor operates on a query-dependent subset of retrieved source examples. Throughout, we treat the pretrained model as frozen and study the statistical effect of the demonstrations selected for its context, abstracting away from transformer-level mechanisms for implementing in-context learning.

Within this framework, we characterize the performance of ICR in terms of four key quantities: the context size n, the corpus size m, the degree of posterior drift, and distributional properties of the domains (in particular, the smoothness of the class posterior). Under a nearest-neighbor model of retrieval and prediction,

*University of Michigan. allanzhu@umich.edu

†^{\dagger}University of Michigan. dnaihao@umich.edu

‡^{\ddagger}New Jersey Institute of Technology. yingcong.li@njit.edu

§^{§}Northwestern University. naichen.shi@northwestern.edu

^{¶}University of Michigan. clayscot@umich.edu we establish a finite- (m,n)(m, n) excess risk bound for ICR. This bound yields two main conclusions. First, the benefit of retrieval: given a sufficiently large corpus size mm, the ICR bound decays faster in nn than the ICL rate. Second, the price of posterior drift: when the source and target posteriors disagree, the ICR excess risk contains an irreducible bias that no learning rule with access only to the source corpus can avoid. Consequently, ICR can be asymptotically inconsistent, whereas ICL remains consistent.

Thus, ICR is preferable under a few-shot regime (small n) with a sufficiently large corpus and limited posterior drift; otherwise, using i.i.d. target demonstrations is the safer choice. We support these findings with experiments on synthetic data and a sentiment-analysis task with annotator disagreement.

References (57)