Language models given a few worked examples in their prompt, called in-context learning, often give different answers when the instruction wording changes slightly, when the answer options are relabeled, or when one of the examples is noisy. Systems that pick these examples automatically usually rely on a dual encoder. A dual encoder is a retriever that turns the query and each candidate example into separate vectors, so the whole example pool can be indexed ahead of time and searched quickly. These retrievers are trained to find examples that make the correct answer likely under one fixed prompt. Nothing in their training asks whether an example keeps the answer correct when the prompt is perturbed.
We propose to put that property into the retriever's embedding space. For each training query and candidate example, a small scoring model is run under many instruction phrasings and alongside noisy companion examples. The training label rewards examples that keep the answer correct and steady across those variants. At test time the retriever costs no more than an ordinary dense lookup. It needs no gradients, no repeated sampling over prompt variants, and no access to the target model's internals. The claim to test is whether "this example anchors the model" is a property of the query–example pair that a dual encoder can learn, and whether it carries over to larger, unseen models and unseen instruction templates.
Core Claim
Prior work suggests stability is uneven across demonstrations, which would make it learnable. ProSA measures prompt sensitivity and finds that adding a single demonstration gives the largest drop in sensitivity. It also ties sensitivity to low decoding confidence. On the Relation between Sensitivity and Accuracy in In-Context Learning shows that predictions that change under paraphrase or reordering are more often wrong, with Pearson $r$ down to −0.40. It then uses that signal to abstain (SENSEL). That method needs many forward passes per query and can only abstain; it cannot repair the prompt.
Our claim is that how much a demonstration anchors the model varies from one demonstration to another, and that a dual encoder trained on stability-aware labels can retrieve the anchoring ones. This should raise worst-case accuracy across instruction templates and across noisy pools, without hurting average accuracy. Stability here means the gain is beyond what likelihood-based retrieval already provides.
The contrast with the closest work is direct:
- Learning To Retrieve Prompts for In-Context Learning (EPR) trains a dual encoder from the likelihood $P(y \mid x, p)$ that a scoring model assigns to the correct answer. That label uses one prompt format and a clean pool.
- UPRISE extends this to many tasks and shows the retriever transfers from a 2.7B scorer to much larger models. It still retrieves by input similarity under fixed templates.
- MDR does target robustness. It reranks UPRISE candidates using negative log-likelihood plus the largest eigenvalue of the Fisher information matrix, a gradient-based measure of how sharply the model's features react to perturbation. Its retriever stays frozen, and the reranking needs white-box gradients for every query at inference.
We move MDR's goal into the retriever's training signal. We replace its gradient measure with a black-box behavioral measure, and the cost is paid once, offline.
Research Program
Label construction. For a training query $x$ with gold answer $y$ and a candidate demonstration $d$, we build a set $\mathcal{T}$ of perturbed prompts. Each prompt draws an instruction paraphrase, an option relabeling or permutation, small formatting changes of the kind reported in The Butterfly Effect of Altering Prompts, and with some probability a label-corrupted companion demonstration. The stability label is
$$s(x,d) = \mathbb{E}_{t \in \mathcal{T}}\big[\log P_t(y \mid x, d)\big] - \beta \, \mathrm{Std}_{t \in \mathcal{T}}\big[\log P_t(y \mid x, d)\big],$$
where $P_t$ is the scoring model's probability under perturbed prompt $t$ and $\beta \ge 0$ sets the trade-off. With $\beta = 0$ and only one $t$, this reduces to the EPR label.
Training. Positives and hard negatives are taken from the top and bottom of $s$ among similarity-retrieved candidates. They are trained with the same contrastive loss and backbone as EPR and UPRISE, so the only change is the label.
Scorer. We use GPT-Neo-2.7B, matching both prior retrievers, so that transfer to larger models is tested rather than assumed.
Separating stability from likelihood. The key analysis question is whether $s$ contains information beyond mean likelihood. Before training anything, we will measure the partial correlation between the variance term and downstream worst-template correctness, controlling for mean likelihood. If that partial correlation is near zero, the stability term adds nothing over likelihood. The project would then fall back to the fixed-budget EPR comparison below, and we would report the negative result.
Baselines
Naive references. These are random demonstrations and plain semantic nearest-neighbor retrieval with an off-the-shelf encoder, in the style of LM-BFF. They show how much of the effect comes from any retrieval at all.
EPR and UPRISE. These are the strongest trained dual-encoder competitors. Both will be retrained by us on the same tasks, with the same backbone, the same scorer, and the same number of training steps. Our labels cost $|\mathcal{T}|$ times more scorer calls, so we also train a budget-matched EPR. It spends the extra calls on scoring $|\mathcal{T}|$ times more candidates per query under the single default template. This is the fair version of the "just more likelihood labels" competitor.
MDR. This is the strongest existing robustness-aware method. It runs at inference on the target model, with $K'=20$ and $K=3$ as in its paper. We report both its accuracy and its per-query cost in wall-clock time and gradient computations. It requires white-box access, so it is evaluated only on open models.
Ablation. Setting $\beta=0$ while keeping the perturbed scoring isolates the variance term from simple averaging over templates.
The file reports no accuracy figures for EPR, UPRISE, or MDR on these benchmarks. We therefore report "no reported result found on Lacuna" for their headline numbers and rely on our own controlled reruns.
What counts as winning. Our retriever must improve worst-template accuracy and reduce the spread of accuracy across held-out templates relative to budget-matched EPR. It must do this with no more than a small loss in mean accuracy. It must also match or exceed MDR's robustness at the cost of a single dense lookup.
Why not just…?
Why not just use MDR's reranker? MDR is the real competitor on accuracy, and it may win on white-box models because it sees the target model directly. Our case rests on cost and access: no gradients at inference, and usability with API-only models. The head-to-head experiment reports accuracy against cost. If MDR is both much more robust and the cost gap is small, our contribution narrows to the black-box setting, and we will say so.
Why not just train EPR with more likelihood labels, since confidence already predicts sensitivity? This is the most serious threat. ProSA's confidence–sensitivity link suggests likelihood may already capture most of the stability signal. The budget-matched EPR baseline and the partial-correlation analysis are designed to settle this. If budget-matched EPR is as stable as our retriever, the idea is refuted.
Why not just ensemble over prompt variants at test time, or abstain like SENSEL? Ensembling multiplies inference cost by the number of variants. Abstention, as in SENSEL, lowers coverage instead of fixing the answer. We include a test-time ensemble of default-template EPR prompts with matched inference compute, and we report accuracy at full coverage.
Why not just filter noisy demonstrations after retrieval? Local Perplexity Ranking replaces noisy candidates with clean neighbors after retrieval, but it assumes clean examples are the majority and breaks down when noise exceeds 60–70%. We run it on top of both EPR and our retriever. If it closes the gap at moderate noise, our noise-robustness claim is limited to the high-noise regime.
Proposed Experiments
Benchmarks. We train and evaluate on EPR's semantic-parsing tasks (BREAK, MTOP, SMCALFLOW) and on the FLAN task clusters used by UPRISE. UPRISE reported weaknesses on commonsense tasks, so those get their own analysis.
Evaluation protocol.
- Held-out templates: every query is evaluated under instruction paraphrases and option formats never seen in training.
- Noisy pools: we inject 0–80% label noise into the candidate pool, following the setting of the noise-robustness study, using its text-generation tasks.
- Misleading context: we add a stress test on SciAux from Thinking in a Crowd, checking whether stability-retrieved demonstrations reduce the rationalization of misleading context in Qwen3-8B's thinking mode.
Target models. We evaluate on BLOOM-7.1B, OPT-66B, and Qwen3-8B. These test transfer from the 2.7B scorer.
Metrics.
- Mean accuracy or exact match.
- Worst-template accuracy.
- Standard deviation of accuracy across templates.
- Rate of answer flips between paired templates.
- Inference cost per query.
Decisive checks.
- Our retriever must beat budget-matched EPR on worst-template accuracy and template spread on held-out templates and across all three target models.
- The $\beta=0$ ablation must fall between EPR and the full method.
- Gains must survive when the evaluation templates come from a perturbation family (for example, rare-symbol option labels as in When Benchmarks are Targets) that was entirely excluded from training labels.
If the third check fails, the retriever has learned specific templates rather than general anchoring.
References
- ProSA: Assessing and Understanding the Prompt Sensitivity of LLMs
- On the Relation between Sensitivity and Accuracy in In-Context Learning
- Learning To Retrieve Prompts for In-Context Learning
- UPRISE: Universal Prompt Retrieval for Improving Zero-Shot Evaluation
- MDR: Model-Specific Demonstration Retrieval at Inference Time for In-Context Learning
- The Butterfly Effect of Altering Prompts: How Small Changes and Jailbreaks Affect Large Language Model Performance
- Making Pre-trained Language Models Better Few-shot Learners
- On the Noise Robustness of In-Context Learning for Text Generation
- Thinking in a Crowd: How Auxiliary Information Shapes LLM Reasoning
- When Benchmarks are Targets: Revealing the Sensitivity of Large Language Model Leaderboards