Selecting Few-Shot Examples: Choose Demonstrations That Fit the Task
Examples in a few-shot prompt demonstrate how the model should map inputs to outputs for a task. More examples, or merely more similar examples, do not automatically make a better prompt. Check that each example is correct, relevant to likely inputs, representative of important cases, and not redundant. Choose between a small static set and per-input retrieval based on task stability, input diversity, latency, and cost.
Examples in a few-shot prompt demonstrate how the model should map inputs to outputs for a task. More examples, or merely more similar examples, do not automatically make a better prompt. Check that each example is correct, relevant to likely inputs, representative of important cases, and not redundant. Choose between a small static set and per-input retrieval based on task stability, input diversity, latency, and cost.
This guide covers choosing examples from an existing candidate pool and validating the selection. It does not cover creating a labeled dataset or automatically generating candidates. Research findings cited here apply to the models, tasks, and evaluation settings studied; they are not guarantees for every model.
What do examples change?
Few-shot examples pair an input with a desired output to show a task pattern in context. Instructions can define a category abstractly; examples show how it applies to concrete cases. They can demonstrate not only the answer but also formatting, handling of edge cases, and desired concision. If an example conflicts with the instructions or has a wrong label, it may confuse the model about which rule to follow.
Example selection can affect both performance and consistency. Brown et al. describe in-context learning as using demonstrations in a prompt to perform a task without updating model weights. Liu et al. (KATE, 2022) found that retrieving examples close to an input in embedding space could outperform random examples in their studied tasks. But selecting only the nearest examples can repeat the same pattern and miss other required subskills. An EMNLP 2023 coverage-based selection study found that, for some compositional tasks, the coverage of the selected set can matter beyond each example’s individual similarity.
These results depend on particular data, models, retrieval methods, and evaluation settings. There is no general rule that similarity or diversity always wins. Compare approaches on your task and model.
Validate the candidate examples first
Before choosing a retrieval strategy, curate the pool:
Keep examples whose answers, labels, and output formats have been checked. Do not treat model-generated labels as ground truth without review.
Include realistic wording, lengths, languages, and difficulty. A Korean-only set does not guarantee the same behavior for other languages.
Cover common cases and important boundary cases or failure modes. Rare cases can deserve inclusion when the cost of misclassification is high.
Remove personal or sensitive details, and check provenance and permissions so training, evaluation, and production inputs are not improperly mixed.
Investigate failed examples. A flawed demonstration may accidentally teach the wrong rule.
If correctness is uncertain, review or repair the examples first. A retrieval algorithm can find an incorrect answer more effectively, but cannot make it correct.
What selection strategies are available?
| Strategy | How it selects | Useful when | Main risk |
|---|---|---|---|
| Static representative set | A person chooses a small set covering task types and exceptions | Inputs are stable and low latency or cost matters | Weak coverage misses new cases |
| Random sample | Samples from the candidate pool | Establishing a baseline or working with a small pool | Results vary as examples change between runs |
| Similarity retrieval | Retrieves semantically or lexically related examples | Wording varies while task patterns recur | Redundant examples or misleading similarity scores |
| Diversity/coverage selection | Chooses a set that spans distinct subtypes or salient features | Inputs combine several conditions | Diversity can drift away from the core task |
| Model-scored or learned selector | Estimates candidate utility or learns a selection policy | Sufficient evaluation data and operational resources exist | Leakage, overfitting, complexity, and added inference cost |
Start with simple baselines: compare a fixed representative set, random selection, and top-similarity retrieval on the same evaluation inputs. If similarity yields duplicates, set per-category limits or add examples that cover different aspects of the input. Tune the selector on data separate from the final model, prompt, and evaluation conditions to reduce overfitting.
Balance similarity and coverage
An example close to the current input can match its context or vocabulary. But similar wording does not prove that its label or solution applies. “I want to close my account” may share words with a refund request while expressing cancellation intent. Treat similarity as a candidate signal, then check the task category and answer.
When selecting a set, consider both each example’s relevance and its overlap with examples already chosen. If an input says, “Delivery was fast, but the product differed from its description,” three examples only about delayed delivery may be less useful than examples that separately demonstrate delivery satisfaction and product mismatch. This illustrates selection logic; it is not a measured model result.
The order of examples should also be fixed or tested. Some studies place more similar examples first, but no single order is best for every model and prompt. If order sensitivity is plausible, compare permutations on an evaluation set. Keep delimiters between instructions and examples, and the format across examples, consistent.
Evaluate and maintain the selection policy
**Define the task.** Document input types, label definitions, output format, important exceptions, and error costs.
**Establish baselines.** Compare a prompt without demonstrations and the current static set on representative evaluation inputs. If model guidance recommends starting zero-shot, include that baseline too.
**Compare selection strategies.** Use the same inputs, model version, generation settings, and metrics for random, static, similarity, and coverage strategies. Change only example selection so the result is interpretable.
**Evaluate diverse cases.** Review overall performance and errors by label, language, length, and boundary type, along with format compliance, latency, and token cost. Keep candidate demonstrations separate from evaluation answers to avoid leakage.
**Version the policy.** Record candidate sources and labels, selection method, embedding or search settings, ordering, and prompt version. Revalidate when data or task definitions change.
For per-input retrieval in production, include retrieval latency and index maintenance in the cost. If a few stable demonstrations suffice, dynamic retrieval may add complexity without value. If input types or contexts change substantially, a fixed set may become stale.
Example: classify customer messages
Hypothetical task: Assign one label—refund, cancel, delivery, or other—and return JSON. The candidate pool has typical examples for each category and boundary cases that are easy to confuse.
First prepare examples with verified labels. A static version includes a small number representing all four categories and key boundaries. A dynamic version retrieves examples similar to each new message, then checks category coverage so it does not repeatedly return only one category. “The item arrived late” and “The item differed from its description” are both complaints but may express different intents, so label definitions must distinguish them.
Compare the versions on the same held-out evaluation set. Measure not only accuracy, but per-category errors, JSON failures, ambiguous-message handling, response time, and token use. This is a hypothetical design example, not a reported test result.
Common problems
**Similar examples repeat:** Check category redundancy rather than relying only on top similarity; add coverage constraints.
**Examples conflict with instructions:** Align labels and demonstrations, and state rules for ambiguous cases.
**Adding examples makes results worse:** Check context limits and truncation, remove inaccurate or contradictory demonstrations, and compare again on the same inputs.
**Retrieved examples are off-task:** Similar words may express different intent. Check task category and answer validity, not only retrieval score.
**Test improves but production worsens:** Check whether evaluation inputs represent production and whether demonstrations leaked into the evaluation set.
Summary
Few-shot examples help when they are correct, align with instructions, and demonstrate important input types. Retrieve relevant examples when useful, but check redundancy and set coverage rather than selecting by similarity alone. Compare static, random, and retrieval-based approaches on the same evaluation set, and review quality, error types, format, latency, and cost. Version examples and selection rules, then re-evaluate when the data or model changes.
References
[Brown et al., “Language Models are Few-Shot Learners” (2020)](https://arxiv.org/abs/2005.14165) — GPT-3 few-shot demonstrations specified in text without parameter updates.
[OpenAI API: Prompt engineering](https://developers.openai.com/api/docs/guides/prompt-engineering) — examples in prompts and prompt construction.
[OpenAI API: Reasoning best practices](https://developers.openai.com/api/docs/guides/reasoning-best-practices) — try zero-shot first for reasoning models and add closely aligned examples when useful.
[Liu et al., “What Makes Good In-Context Examples for GPT-3?” (DeeLIO 2022)](https://aclanthology.org/2022.deelio-1/) — retrieval of nearby examples and comparisons with random baselines in studied tasks.
[Gupta et al., “Coverage-based Example Selection for In-Context Learning” (EMNLP 2023)](https://aclanthology.org/2023.findings-emnlp.930/) — independent similarity ranking can be redundant; set coverage may matter in some compositional tasks.