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

Yilun Zhu, Naihao Deng, Yingcong Li, Naichen Shi, Clayton ScottarXiv 2026

TL;DR

This paper provides a formal theoretical framework for In-Context Retrieval (ICR), treating it as a domain adaptation problem. The authors prove that while ICR offers significantly faster convergence rates than standard in-context learning (ICL), it introduces an irreducible bias—a "price of posterior drift"—that can make it asymptotically worse than random sampling if the retrieval database doesn't perfectly align with the target task.

What Problem Does This Paper Address?

In-context learning (ICL) allows large language models to perform tasks by seeing a few examples in their prompt. Standard ICL assumes these examples are drawn randomly from the same distribution as the query. However, practitioners often use In-Context Retrieval (ICR)—a core component of Retrieval-Augmented Generation (RAG)—to pick examples most similar to the query from a massive database.

While ICR works well empirically, we lack a deep understanding of its statistical trade-offs. Specifically, what happens when the database (the source domain) differs from the actual task at hand (the target domain)? Prior theory was mostly limited to simple linear-Gaussian models. This paper moves into the more complex world of nonparametric binary classification to answer: When does retrieval help, and when does it hurt?

Methods

The authors frame ICR as a domain adaptation problem. They distinguish between two types of distribution shifts:

  1. Covariate Shift: The inputs in the database look different from the query.
  2. Posterior Drift: The "ground truth" labels for the same input might differ between the database and the target task (e.g., different annotators disagreeing on sentiment).

To analyze this, they model the in-context learner as a nearest-neighbor (NN) majority-vote classifier. This is a mathematically tractable proxy for how Transformers behave. They use a tool called Relative Signal Strength (RSS) to quantify how much the source and target distributions disagree.

The core of the method is deriving excess risk bounds—mathematical guarantees on how much the model's error exceeds the theoretical minimum (Bayes risk)—as a function of the context size ($n$), the database size ($m$), and the degree of distribution shift.

Figure 1: Standard ICL vs ICR.

Datasets

The theory is validated using two distinct setups:

  • Synthetic Weierstrass Family: A 1D synthetic dataset where the authors can precisely control the "roughness" (smoothness $\alpha$) of the target function and the amount of posterior drift ($\epsilon$).
  • Sentiment Analysis (Díaz et al., 2018): A real-world dataset where multiple annotators rated the same sentences. Because annotators often disagree, each annotator represents a slightly different "target distribution," naturally creating the posterior drift the authors aim to study. They tested this using the Qwen3.5 (4B and 27B) model family.

Key Insights and Ideas

  • The Efficiency of Retrieval: In standard ICL, most random examples in the context are "far" from the query and thus uninformative. In ICR, the retriever ensures all $n$ examples are "near" the query. Consequently, ICR's error decays much faster as you add more examples to the context.
  • The Smoothness Advantage: Standard ICL is sensitive to how "rough" the target function is; if the task is complex, you need many more examples. ICR, however, is largely insensitive to smoothness, provided the database is large enough.
  • The Irreducible Bias: If there is a mismatch between the database labels and the target task (posterior drift), ICR hits a "performance floor." No matter how many examples you retrieve, you cannot overcome the inherent bias of the source data.
  • Consistency vs. Inconsistency: Standard ICL is "consistent"—with enough random examples, the error eventually goes to zero. ICR can be "inconsistent"—it might start strong but eventually get overtaken by ICL because it can't shake its initial bias.

Key Results

  • Theoretical Convergence: The authors prove that ICR converges at a rate of roughly $n^{-1/2}$, whereas standard ICL converges at a much slower rate that depends on the dimensionality and smoothness of the data.
  • The Crossover Point: In experiments, ICR dominates ICL in "few-shot" regimes (small $n$). However, as $n$ increases, ICL eventually wins if there is significant posterior drift.
  • Corpus Saturation: The benefit of a larger database ($m$) eventually plateaus. The authors provide a formula for the "critical corpus size" beyond which adding more data to the database doesn't help.
  • Model Scale: Experiments with larger models (Qwen 27B vs 4B) showed that increased model capacity does not remove the irreducible bias caused by posterior drift, confirming the theory that this is a data-level problem, not a model-level one.

Limitations and Future Work

  • Binary Classification: The current theoretical framework is specialized for binary classification. Extending this to multi-class problems or regression is a natural next step.
  • Nearest-Neighbor Proxy: While the authors show that models like TabPFN and Qwen behave similarly to NN classifiers, a more direct analysis of Transformer attention mechanisms would be valuable.
  • Retrieval Quality: The work assumes a "perfect" nearest-neighbor retriever. In practice, retrieval is often done via imperfect embeddings, which could introduce additional errors not captured here.

Summary updated

BibTeX
@misc{zhu2026retrieval,
  author = {Yilun Zhu and Naihao Deng and Yingcong Li and Naichen Shi and Clayton Scott},
  title = {{Retrieval-Based In-Context Learning: A Domain Adaptation Framework}},
  year = {2026},
  eprint = {2610.05717},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2610.05717},
}
Figure