Module 2 · Reasoning techniques
Few-shot and dynamic example selection
5 min read
Examples are the most powerful tool in prompt engineering: showing a good answer conveys in a few lines what paragraphs of instructions struggle to describe (lesson 1.2: 55% → 89%). But which examples, and how many? A popular idea is to pick, for each input, the examples that resemble it most. This lesson puts that idea to the test, with a counter-intuitive result that says a lot about what an example must bring.
The experiment
The task: classify contract clauses into eight types (confidentiality, termination, payment, liability, intellectual property, force majeure, jurisdiction, term). The system prompt gives the eight definitions. Test clauses are written with phrasings absent from the example bank, as real clauses will be:
def messages(clause: Clause, shots: list[Clause]) -> list[dict]:
out = [{"role": "system", "content": "Tu classes des clauses de contrat.\n" + DEFINITIONS}]
for s in shots:
out += [{"role": "user", "content": s.text}, {"role": "assistant", "content": s.label}]
return out + [{"role": "user", "content": clause.text}]
Examples are given as real exchanges (the clause, then the assistant's answer): the form closest to what the model must produce.
The strategies compared
def nearest(clause: Clause, bank: list[Clause], k: int) -> list[Clause]:
q = words(clause.text)
return sorted(bank, key=lambda c: cosine(q, words(c.text)), reverse=True)[:k]
def nearest_per_label(clause: Clause, bank: list[Clause]) -> list[Clause]:
"""Le plus proche exemple DE CHAQUE TYPE : la pertinence de la similarité, la couverture de tous les types."""
q = words(clause.text)
return [max((c for c in bank if c.label == label), key=lambda c: cosine(q, words(c.text)))
for label in CLAUSE_TYPES]
The results, over 160 test clauses (Qwen2.5-1.5B):
| Examples given | Accuracy |
|---|---|
| None (definitions only) | 0.61 |
| 8 fixed examples, one per type | 0.80 |
| The 8 examples nearest to the clause | 0.69 |
| The nearest example of each type (8 examples) | 0.71 |
Examples bring a lot: + 19 points with eight fixed examples. But the two "smart" selections, which choose different examples for each clause, do worse than the fixed examples.
Why the "nearest" do worse
Similarity-based selection looked like the smartest option; it loses 11 points to eight fixed examples. Analyzing the chosen examples explains why:
- the 8 nearest examples cover on average only 4 types out of 8;
- the right type is among them in only 56% of cases;
- the single nearest neighbor has the right type in barely 19% of cases.
Word similarity is a poor proxy for meaning: "The contract is entered into for two years" and "Either party may terminate the contract" share "contract", but not the type. The chosen examples then pull the model toward wrong types, instead of showing it the range of possible answers. Fixed examples have one decisive property: one per type. They show each class and its boundary.
Guaranteeing coverage is not enough, though: the nearest example of each type (0.71) does barely better than the plain nearest examples (0.69; the gap is within measurement noise, about ± 7 points over 160 cases). The most likely explanation: the seven examples of the other types are chosen because they resemble the clause: they are decoys, clauses of another type sharing its vocabulary. The model, which readily imitates the most similar example, is wrong more often. Fixed examples, on the other hand, are typical of their class, with no vocabulary link to the clause.
What a good example set brings
| Property | Why |
|---|---|
| Coverage | Every class, every format, every hard case is represented at least once |
| Diversity | Varied phrasings, so the model generalizes instead of copying a turn of phrase |
| Relevance | Examples close to the input help… if closeness is measured on meaning |
| Correctness | A wrong example is learned as well as a right one |
| Balance | Three examples of one class and one of another bias answers toward the first |
Dynamic selection becomes worthwhile when the bank is large and varied, when similarity is measured with embeddings (meaning, not words), and when it complements a base of fixed, typical examples instead of replacing it. Always measure it against that base.
How many examples?
No universal rule, but a method: add examples as long as the score improves on the evaluation set, while watching the cost. Each example adds tokens to every request (lesson 1.2: 990 tokens for two long examples). Prompt caching sharply reduces that cost for fixed examples placed at the start of the prompt (lesson 3.4) — one more argument for fixed, well-chosen examples, which dynamic selection prevents from being cached.
Key takeaways
- Examples are powerful, but their choice matters more than their number.
- Here, 8 fixed examples (one per type) beat the 8 "nearest" examples: 0.80 versus 0.69.
- Word similarity is a poor proxy for meaning; the nearest neighbors covered only 4 types out of 8.
- Even covering all 8 types, examples chosen for resemblance act as decoys: 0.71.
- Aim for coverage, diversity, correctness and balance; relevance comes next, measured with embeddings.
- Fixed examples can be cached; dynamic selection cannot.
Practice
- Run
examples/03_fewshot_and_chaining.pyand print, for three test clauses, the examples chosen by each strategy. - Replace word similarity with embedding similarity (for instance with FastEmbed) and rerun dynamic selection.
- Measure accuracy with 0, 1, 2 and 3 examples per type: where does the gain plateau?