Learning to Search Effective Example Sequences for In-Context Learning

Xiang Gao, Ankita Sinha, Kamalika Das

Introduction

Large Language Models (LLMs) have demonstrated remarkable capabilities in In-Context Learning (ICL), where they solve new tasks by observing a few examples provided in the prompt. However, LLM performance is notoriously sensitive to the composition of these prompts. Small changes in which examples are selected, the order in which they appear, or the total number of examples can lead to significant fluctuations in accuracy.

Most existing research treats these factors—selection, arrangement, and sequence length—as independent variables. This isolation often fails to capture the complex interdependencies between examples. This paper introduces BESC (Beam Search-based Example Sequence Constructor), a method that jointly optimizes these three factors for every specific query using a heuristic search approach.

BESC jointly models element composition, arrangement, and sequence length to select the optimal ICL example sequence.

The BESC Framework

BESC treats prompt construction as a search problem over an "explosive" search space of $O(N^L)$, where $N$ is the number of candidate examples and $L$ is the maximum sequence length. To make this search tractable, the authors propose a dual-encoder scoring model combined with beam search.

1. Dual-Encoder Scoring Model

The core of BESC is a scorer designed for efficiency. It consists of a Prefix Encoder, which embeds the current query and previously selected examples, and a Suffix Encoder, which embeds a candidate "next" example. The score for adding a specific example is the dot product of these two embeddings. This architecture allows for high-speed retrieval from vector databases during the search process.

2. Contrastive Training

The scorer is trained using a contrastive learning objective. During training, the model is presented with two different example sequences for the same query. A frozen LLM acts as a "teacher," providing ground-truth feedback based on which sequence results in a better output (e.g., higher accuracy or F1 score). The scorer is then optimized to predict which sequence the LLM would prefer.

During inference, BESC does not simply pick the single best next example (a greedy approach). Instead, it employs Beam Search to maintain a set of the top-$b$ most promising sequences, expanding them incrementally. Crucially, the model includes a special termination token ($\perp$). When the scorer assigns the highest value to this token, the search stops, allowing the model to automatically determine the optimal sequence length for a given query.

The BESC pipeline: (a) Training the scorer using contrastive loss against LLM ground truth, and (b) Inference using beam search to find the final example pick.

Experimental Results

The authors evaluated BESC across six diverse datasets—spanning classification (AGNews, SST, TREC) and reasoning (TabMWP, BREAK, GSM8K)—using four different LLM families (GPT-2, GPT-neo, GPT-3, and GPT-3.5).

  • Consistent Superiority: BESC outperformed standard baselines like k-Nearest Neighbors (kNN), Efficient Prompt Retrieval (EPR), and Q-learning approaches across all tested scenarios.
  • Significant Performance Gains: On the TREC dataset with GPT-3.5, BESC achieved an accuracy of 0.85, a substantial improvement over kNN (0.70) and random selection (0.55).
  • The "More is Better" Fallacy: The study confirmed that adding more examples often leads to a performance plateau or decline. BESC’s ability to self-terminate the sequence length was a key factor in its superior performance.
  • Zero-Shot Transferability: A BESC model trained on one set of tasks demonstrated the ability to generalize to entirely new tasks without additional training, outperforming standard retrieval baselines.

Conclusion

BESC demonstrates that modeling example selection, arrangement, and length holistically is essential for maximizing the potential of In-Context Learning. By transforming prompt construction into a learnable search problem, BESC provides a robust and efficient framework for generating high-quality, query-specific prompts that significantly enhance LLM performance across a wide range of tasks.

Summary updated

BibTeX
@inproceedings{gao2025learning,
  author = {Xiang Gao and Ankita Sinha and Kamalika Das},
  title = {{Learning to Search Effective Example Sequences for In-Context Learning}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.findings-naacl.342},
}
Figure