---
title: Prompt Template & Example Selection
url: https://www.emergentmind.com/topics/prompt-template-and-example-selection
type: topic
---

# Prompt Template & Example Selection

Prompt Template and Example Selection encompasses a suite of methodologies for configuring input to Large Language Models (LLMs) so that maximal performance is achieved on downstream tasks, particularly when only small labeled datasets are available. Core dimensions include the explicit formatting of instructions and demonstrations (“prompt-template design”), algorithmic selection of in-context examples (“example selection”), the use of ensemble and automatic search techniques, and the evaluation and optimization of prompt-template-exemplar pairs. These mechanisms underpin modern prompt engineering, critical for adaptation and deployment of LLMs in areas such as e-commerce, code generation, translation, automated scoring, and classification.

## 1. Foundations of Prompt Template Design

Prompt templates encode the structure, verbalization, and formatting of inputs to LLMs. Template design directly affects output accuracy; poor choices can reduce output quality to chance-level [2401.06766]. Templates are specified across:

- **Input verbalizer**, $\mathsf{v}_I(x)$: Defines how raw input x is introduced, e.g., “input: {}”, “sentence: {}”;
- **Output verbalizer**, $\mathsf{v}_O(y)$: Specifies label phrasing, such as “label: {}”, “sentiment: {}.”;
- **Intra-separator**: Delimiters between input and output, typically spaces or newlines;
- **Inter-separator**: Delimiters between examples, e.g., blank lines or “### Example ###”.

In “Examples as the Prompt” (EaP) [2503.13518], the template is a concatenation of:
> [SYSTEM_PROMPT]  
> Example 1: Input: {...} Output: {...}  
> ...  
> “Now you are given a new query: Input: {q} Output:”

For the lightweight variant EaPₗᵢₜₑ, the SYSTEM_PROMPT is dropped for a single anchor line.

In machine translation and other domains, prompt templates include explicit role descriptions, example pair enumeration, and directives for output formatting (e.g., requesting “only the translation result” in [2510.03748]).

## 2. Example Selection Algorithms

Selecting representative, relevant, and diverse in-context examples is essential for robust ICL. Methods are broadly categorized as:

### 2.1 Clustering-Based Global Selection  
EaP [2503.13518] uses k-means clustering over feature embeddings (TF-IDF or LLM-based) of a labeled pool $\mathcal{E}$:
$$
J = \sum_{j=1}^K \sum_{x \in C_j} \|\phi(x) - \mu_j\|^2
$$
Each cluster centroid yields a global exemplar $e^*_j = \arg\min_{x \in C_j}\|\phi(x)-\mu_j\|$.

### 2.2 Local (Per-Query) Retrieval  
For each query $q$, embed $q$ as $u = \phi(q)$, then select top-$M$ nearest examples from $\mathcal{E}$ using:
$$
\mathrm{sim}(u,v) = \frac{u\cdot v}{\|u\|\cdot\|v\|}
$$
with implementations leveraging FAISS or RapidFuzz for sublinear nearest neighbor search.

### 2.3 Hierarchical/Quality-Driven Methods  
TreePrompt [2510.03748] iteratively asks the LLM to label example pairs in a tree structure (good, neutral, bad), then expands on high-quality branches via k-NN. Adaptive Few-Shot Prompting (AFSP) can merge similarity scores from multiple embedding spaces.

### 2.4 Spectral Methods  
For tabular data, [2506.20451] computes similarity via Jaccard index on LLM tokenizations, builds a $k$-NN graph, and uses the Laplacian eigengap to infer the minimal number of demonstration clusters required:
$$
k = \arg\max_{1 \leq i < n}(\lambda_{i+1}-\lambda_i)
$$

### 2.5 Mutual Information Maximization  
Templates are selected to maximize estimated mutual information $I(X;Y) = H(Y) - H(Y|X)$ over an unlabeled pool, with only LM calls required [2203.11364]. This reliably identifies high-quality templates without ground-truth data.

## 3. Automated Prompt and Template Optimization

Recent work eliminates manual prompt engineering via automatic, data-driven prompt generation.

- **Adaptive Selection of Prompting Techniques** [2510.18162]: Task clusters are formed via semantic embedding and k-means. Each cluster is mapped to a set of prompt elements (“techniques”) stored in knowledge-base JSON. User-provided descriptions are embedded, matched to clusters, and fused into prompts with dynamically weighted techniques according to historical frequency and performance.

- **Input-Output Coverage Maximization**: For tasks requiring few-shot exemplars, select example subsets $E^*$ by maximizing the sum of pairwise similarities, sorted by difficulty or diversity.

- **Successive Halving for Template Search** [2506.08669]: Prompt candidates are evaluated iteratively on increasing validation sets, halving candidates at each round, until the best template remains.

- **Blueprints for Reasoning** [2506.08669]: Structured, reusable high-level guides (“blueprints”) are generated for SLMs by LLMs, selected by validation accuracy, and refined via automatic prompt optimization (APO).

## 4. Empirical Performance and Evaluation Metrics

Prompt-template and example selection strategies result in statistically significant improvements:

| Approach         | Domain                    | Best Reported Gain                                                                             |
|------------------|--------------------------|-----------------------------------------------------------------------------------------------|
| EaP/EaPₗᵢₜₑ      | E-commerce classification | Navigation Pos Prec 0.8910 vs 0.8198 (+8.9%), throughput +71.6% [2503.13518]                   |
| TreePrompt+AFSP  | English→Persian MT        | COMET improvement 0.01–0.02 over vanilla KNN/AFSP [2510.03748]                                |
| Spectral/Graph   | Tabular classification    | “Stable” performance close to random-best across multiple datasets and LLMs [2506.20451]       |
| PromptRefine     | Indic ICL                | Token-F1 +8.26 vs CEIL; chrF₁ +3.28 in MT [2412.05710]                                        |
| Complexity-based | NER tagging               | +5 F1 GPT-4 CoNLL2003; +28.85 in GPT-j [2403.03861]                                           |
| MI-based         | General NLP tasks         | Achieves 90% of gap from mean to best prompt accuracy, zero labels needed [2203.11364]         |
| Automated Gen.   | BBEH                     | +4.1pp arithmetic mean over original [2510.18162]                                             |
| AES              | Essay scoring (GPT)       | Example selection enables GPT-3.5 to outperform some GPT-4; robust template orderings matter [2411.18924] |

Metrics include precision/recall, BLEU/ROUGE, Macro-F$_1$, QWK, COMET, and throughput (items/sec).

## 5. Systematic Taxonomies and Best-Use Practices

Prompt-with-Me [2509.17096] proposes a four-dimensional taxonomy for LLM-driven software engineering prompts:

- **Intent**: Objective (e.g. “Code Generation”, “Review”).
- **Author Role**: Persona/discipline of the author.
- **SDLC Stage**: Lifecycle phase.
- **Prompt Type**: Structural paradigm (template, zero-shot, few-shot).

Automatic classification (hybrid ML: MLP and Random Forest) ensures near-real-time tagging for structured management, with downstream performance evident in improved developer adoption, reduced duplication, and enhanced reproducibility.

Best-practice highlights across studies:

- Monitor template and example pool quality; periodically reselect or retrain global exemplars [2503.13518].
- Specify and report template dimensions; do not transfer “best” templates across setups/model families [2401.06766].
- Use ensembles of 4–5 templates; average distributions for increased accuracy and reduced variance [2401.06766].
- For diversity and robustness, fine-tune retrievers via contrastive or DPP losses after cross-bank alignment [2412.05710].
- Employ explicit coverage/diversity maximization—avoid redundancy and class imbalance [2503.13518, 2412.05710].

## 6. Limitations, Caveats, and Future Directions

Despite consistently positive impacts, several limitations are noted:

- Template sensitivity remains high; optimal formatting and example selection do not universally transfer [2401.06766].
- Example selection can introduce majority/recency bias (AES), varying by model/version [2411.18924].
- Automated prompt optimization (e.g. via instruction refinement) exceeds example selection in effect size, but combining both gives only incremental benefit and requires matching train/test policies [2412.06432].
- For mutual information-based template selection, low MI values are indicative of model incapability—not a guarantee of optimality [2203.11364].
- Empirical gains are task- and domain-dependent; ablation and adaptation per deployment are recommended.

Emerging directions include fine-grained, context-length-aware selection, ensembling of prompt-generation strategies, integration with software versioning and health monitoring, and robust multilingual alignment for cross-resource ICL.

## 7. Representative Implementations and Pseudocode Overview

The following table summarizes key algorithms for template and example selection:

| Name                 | Selection Principle            | Pseudocode/Procedure Location              |
|----------------------|-------------------------------|--------------------------------------------|
| EaP Global/Local     | K-means + ANN                 | [2503.13518], Section 2                    |
| TreePrompt           | LLM-labeled tree + K-NN/AFSP  | [2510.03748], Sec. 2-3                     |
| Spectral Demonstration| Eigengap over token Jaccard   | [2506.20451], Sec. 3-5                     |
| MI-Max Template      | Mutual Information over unlabeled| [2203.11364], Sec. 3                       |
| Multi-prompt Evaluation| IRT + plug-in estimator      | [2405.17202], Algorithm 1                  |
| Complexity-based NER | Weighted C_sent               | [2403.03861], Section 3                    |
| Automated Prompt Generation | Coverage max, cluster-based| [2510.18162], Sections 3-4                 |

By systematically combining taxonomized templates, robust example selection, and automated optimization, practitioners can derive high-performing prompt configurations that generalize across real-world tasks, models, and operational constraints.

Source: https://www.emergentmind.com/topics/prompt-template-and-example-selection