Demonstration selection is the systematic process of choosing which specific few-shot examples to embed within a prompt's context to maximize a large language model's performance on a target task. Unlike random selection, it treats examples as engineered inputs, strategically optimizing for factors like relevance, diversity, and clarity to guide the model's conditional generation more effectively. This parameter-free adaptation is critical for reliable frozen model inference.
Glossary
Demonstration Selection

What is Demonstration Selection?
Demonstration selection is the core engineering process within in-context learning for strategically curating which few-shot examples to include in a prompt.
Effective strategies include semantic similarity selection, where a retrieval-augmented ICL system uses embedding-based retrieval to find examples closest to the user's query. Practitioners also optimize demonstration ordering and balance exemplar quality with demonstration diversity to cover the task's label space. This transforms static prompts into dynamic, query-aware interfaces that significantly improve inference-time adaptation without model retraining.
Core Demonstration Selection Strategies
The strategic selection of which examples to include in a prompt is a critical determinant of in-context learning performance. These core strategies define the methodology for choosing the most effective demonstrations.
Demonstration Diversity
A selection criterion focused on choosing a set of examples that collectively cover the broadest possible range of the task's input space and output variations. It combats bias from similar, redundant examples.
- Goal: To provide the model with a representative schema of the task, improving generalization to edge cases.
- Method: Can involve clustering example embeddings and selecting prototypes from each cluster, or using maximum marginal relevance to balance similarity to the query with novelty relative to already-selected examples.
- Outcome: Reduces the risk of the model overfitting to a narrow pattern present in a homogeneous set of demonstrations.
Complexity-Based Selection
This strategy matches the complexity or difficulty of the selected demonstrations to the perceived complexity of the query. The hypothesis is that models benefit from seeing examples of analogous difficulty.
- Complexity Heuristics: Can be measured by output length, syntactic complexity, the number of reasoning steps required, or scores from a separately trained difficulty estimator.
- Adaptive Prompting: For a simple query, simpler examples are chosen to avoid unnecessary noise. For a complex query, multi-step or intricate examples are retrieved to prime appropriate reasoning.
- Application: Particularly valuable for chain-of-thought prompting and multi-step reasoning tasks like math word problems.
Demonstration Ordering
The sequence of examples in a prompt is not neutral; it creates priming and recency effects. Strategic ordering is a key sub-component of selection.
- Recency Bias: Models often give disproportionate weight to the last few examples before the query. Critical demonstrations should be placed near the end.
- Curriculum Ordering: Arranging examples from simpler to more complex can guide the model's reasoning process, mimicking human pedagogical strategies.
- Random vs. Strategic: While random ordering is a common baseline, performance gains of 5-15% are possible with optimized ordering, such as placing the most query-relevant example last.
Label Space Coverage
For classification tasks, this strategy ensures the selected few-shot set includes at least one example of every possible output class (label). It explicitly teaches the model the complete label space.
- Mechanism: During retrieval, the process is constrained to return a subset where all labels in the task's ontology are represented, even if some are less semantically similar to the query.
- Why It Works: Prevents the model from ignoring rare classes or hallucinating labels not shown in the context. It provides a definitive reference set.
- Example: In intent classification for a customer service bot, the prompt must contain demonstrations for 'refund', 'technical support', 'billing inquiry', etc., regardless of the user's specific query.
Demonstration Selection
Demonstration selection is the strategic process of choosing which few-shot examples to include in a prompt to maximize a model's in-context learning performance on a target task.
Demonstration selection is the core engineering task within in-context learning (ICL). It involves curating the specific input-output pairs that condition a frozen model's response. The chosen seed examples define the label space and input-output mapping the model must infer. Poor selection leads to ambiguous priming and unreliable outputs, while optimal selection can yield performance approaching parameter-efficient fine-tuning. The goal is to provide a clear, generalizable task definition within the constraints of the context window.
Effective strategies move beyond random selection. Semantic similarity selection uses embedding-based retrieval to find examples most relevant to a query. Demonstration diversity ensures coverage of the task's variation. Retrieval-augmented ICL systems perform query-example matching dynamically for each request. The exemplar quality—clarity, correctness, and formatting—is paramount. This process is a form of inference-time adaptation, enabling gradient-free learning by shaping the model's conditional generation through curated context alone.
Comparison of Primary Demonstration Selection Criteria
A comparison of the core methodologies for selecting few-shot examples to optimize in-context learning performance.
| Selection Criterion | Semantic Similarity | Diversity-Based | Task-Specific Heuristics | Random Sampling (Baseline) |
|---|---|---|---|---|
Primary Mechanism | Retrieves examples most similar to the query using embedding cosine distance. | Selects examples to maximize coverage of the task's input space or label distribution. | Uses hand-crafted rules (e.g., difficulty, format) based on domain knowledge. | Chooses examples uniformly at random from the candidate pool. |
Requires Pre-Computed Embeddings | ||||
Adapts Dynamically Per Query | ||||
Typical Performance Gain | High (5-15% over baseline) | Medium (3-10% over baseline) | Variable (0-20%, highly domain-dependent) | 0% (Baseline) |
Computational Overhead | Medium (requires vector search per query) | Low (requires one-time clustering or scoring) | Low (rule-based filtering) | None |
Generalization Strength | Excels on queries similar to training data. | Excels on heterogeneous or out-of-distribution queries. | Excels when heuristics perfectly match task nuances. | Provides a neutral, unbiased baseline. |
Risk of Negative Transfer | Medium (can overfit to local patterns) | Low | High (if heuristics are poorly designed) | N/A |
Best Suited For | Retrieval-Augmented ICL, k-NN demonstration retrieval. | Demonstration diversity optimization, covering a broad label space. | Structured demonstrations, template-based examples with clear rules. | Initial prototyping and establishing a performance baseline. |
Frequently Asked Questions
Essential questions and answers on the strategic process of choosing and formatting examples to maximize a language model's in-context learning performance.
Demonstration selection is the strategic process of choosing which few-shot examples to include in a prompt to maximize a model's in-context learning (ICL) performance on a target task. It works by retrieving or crafting input-output pairs that are most relevant to a given user query and presenting them as conditioning context before the model generates its final answer. The core mechanism relies on the model's ability to infer the input-output mapping from these demonstrations and generalize it to the new query, all without updating its internal frozen model parameters. Effective selection is not random; it involves criteria like semantic similarity, demonstration diversity, and exemplar quality to construct a prompt that reliably steers the model toward the desired output format and reasoning pattern.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Demonstration selection is a core component of in-context learning. These related concepts define the strategies, mechanisms, and frameworks for constructing effective few-shot prompts.
In-Context Learning (ICL)
In-context learning (ICL) is the foundational paradigm where a pre-trained language model performs a new task by conditioning its response on a few input-output examples provided within the prompt, without updating its internal parameters. It is the mechanism that demonstration selection aims to optimize.
- Core Principle: The model infers the task pattern from the context alone.
- Parameter-Free: Relies on the model's pre-existing knowledge and attention mechanisms.
- Primary Use Case: Rapid task adaptation for frozen models.
Retrieval-Augmented ICL
Retrieval-augmented in-context learning is a dynamic technique that retrieves the most relevant demonstrations from a datastore for each individual query. It automates and optimizes demonstration selection at inference time.
- Dynamic Context: The prompt is constructed per query, not statically.
- Semantic Retrieval: Uses embedding-based retrieval (e.g., cosine similarity) to find examples.
- Key Benefit: Improves performance by ensuring high relevance between the query and the provided examples.
Demonstration Ordering
Demonstration ordering is the strategic arrangement of the sequence of few-shot examples within a prompt. The order can significantly impact model performance due to recency, priming, or positional bias effects.
- Recency Bias: Models may give disproportionate weight to the last example.
- Priming Effects: Early examples set a contextual frame for interpreting later ones.
- Common Strategies: Ordering by difficulty, similarity to the query, or diversity.
Exemplar Quality
Exemplar quality refers to the intrinsic characteristics of individual few-shot examples that determine their effectiveness. High-quality demonstrations are clear, correct, and task-relevant.
- Clarity: The input-output mapping must be unambiguous.
- Correctness: The output must be an accurate completion of the input.
- Relevance: The example must be a faithful instance of the target task.
- Impact: Poor-quality examples can confuse the model and degrade performance more than having no examples.
Semantic Similarity Selection
Semantic similarity selection is a predominant demonstration selection strategy. It chooses examples whose input is most semantically similar to the current query, typically using vector embeddings and a similarity metric like cosine distance.
- Mechanism: Encodes both the query and candidate examples into a shared embedding space (e.g., using OpenAI's
text-embedding-3-small). - k-NN Retrieval: Often implemented as a k-nearest neighbors (k-NN) search over the embedding space.
- Goal: Provide the model with the most directly analogous precedents for the new query.
Dynamic Few-Shot Prompting
Dynamic few-shot prompting is an adaptive technique where the selection, quantity, and sometimes formatting of demonstrations are determined on-the-fly for each query. It contrasts with using a fixed, static set of examples.
- Query-Adaptive: The system can retrieve more examples for complex queries and fewer for simple ones.
- Context Window Management: Dynamically balances the number of examples against other prompt components (instructions, query) to stay within token limits.
- Implementation: Often relies on a retrieval system and a controller that decides on demonstration count.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us