Few-Shot Prompting (In-Context Learning)
Drop 3–8 worked examples into the prompt — the model infers task format, output structure, and constraints without any fine-tuning or weight updates.
Intent & Description
🎯 Intent
Steer the model toward a specific output format, task interpretation, or domain style by showing examples — cheaper and faster than fine-tuning when you have a clear task spec you can demonstrate.
📋 Context
Zero-shot prompting relies entirely on the model’s pre-trained knowledge of the task format. When the desired output structure is unusual, domain-specific, or has constraints the model might not infer from description alone, a few worked examples communicate the spec more precisely than instructions can.
💡 Solution
Prepend K examples of (input, expected output) pairs before the target input in the prompt. Examples should be: representative of the target distribution, diverse (not all identical patterns), and correctly labeled — wrong few-shot examples degrade performance. Typical K = 3–8. For classification tasks, balance examples across classes. For generation tasks, examples demonstrate the output format the model should follow. Zero-shot (no examples) is always the baseline to beat — if few-shot doesn’t improve over zero-shot, your examples may be hurting rather than helping.
Real-world Use Case
📌 TL;DR
Show don’t tell — 3–8 worked examples communicate output format more precisely than instructions. Free to implement, immediate results. Always compare against zero-shot baseline first.
Advantages
- No training or weight updates required — works on any model via the API with zero infrastructure
- More precise than natural language instructions for specifying output format and style
- Fast iteration — swap examples in seconds vs. hours for fine-tuning
Disadvantages
- Takes up valuable context window tokens — 8 long examples can consume 1–2K tokens
- Model quality sensitive to example selection and ordering — bad examples degrade performance
- Doesn’t generalize as deeply as fine-tuning — breaks on inputs very different from the examples provided