Few-Shot Examples
Show, do not just tell: well-chosen input and output examples teach format, tone and edge cases faster than paragraphs of rules.
Beginner · about 3 hours · difficulty 2/5
Learn first: Clear Instructions and System Prompts
Few-shot prompting includes a handful of example inputs with ideal outputs. Models imitate examples strongly, which makes them the fastest way to lock in a format or style.
Choosing examples
- Diverse: cover the range of inputs, including tricky and edge cases, not five variations of the easy case.
- Representative of the output you want: the model copies length, tone and structure, including mistakes.
- Labelled clearly: wrap each example in tags or headings so the model can tell examples from the real input.
- Three to five good examples usually beat twenty mediocre ones.
Pitfalls
- Overfitting to examples: if all examples mention the same product, outputs will too. Vary surface details.
- Order and recency effects: the last example can have outsized influence.
- Leaking into answers: the model may reuse facts from examples. Use obviously fictional data.
- Long example sets cost tokens on every request; prompt caching helps.
Dynamic few-shot
For large tasks, store a library of examples and retrieve the most similar ones per request using embeddings. This combines prompting with the retrieval ideas coming later.
Good enough to move on
You can improve a classification or rewriting prompt by adding diverse examples and measure the improvement on a test set.
Key concepts
- Zero-shot vs few-shot
- Choosing diverse examples
- Formatting and delimiting examples
- Avoiding example overfitting
- Dynamic example retrieval
Mini project
Build a support-ticket classifier, measure zero-shot accuracy on 40 labelled tickets, then add 4 diverse examples and measure again; report which categories improved.
Check yourself
All your examples are short, and now every output is too short for complex inputs. What happened and how do you fix it?
Free resources
- Prompting Guide: few-shot prompting (DAIR.AI)
- Anthropic prompt engineering overview (Anthropic)