Few-Shot Prompting
Few-shot prompting includes worked examples in the prompt so the model learns the task's pattern from demonstrations instead of instructions alone.
Few-shot prompting is teaching a model the task by example: the prompt includes a handful of input→output demonstrations, and the model infers the pattern — format, style, decision boundary — from them.
It exploits in-context learning, the emergent ability of LLMs to pick up a task from demonstrations without any weight updates. Its sweet spot is everything that's easier to show than to describe: an exact JSON shape, the house convention for a route handler, where the line falls between "bug" and "feature request." One canonical example carries error handling, naming, and structure that would take a paragraph of brittle adjectives to specify — which is why it's a core prompt pattern for coding agents.
The craft is selection: short, varied examples that mark the task's boundaries, including the edge case the model fumbles. Contrast zero-shot (instructions only — the modern default for capable models on clear tasks) and see Few-Shot vs Chain-of-Thought vs Structured Prompting for when each technique earns its tokens.
Frequently asked questions
- How many examples should a few-shot prompt include?
- Two to five short, varied ones usually beat both one long example (overfits its specifics) and ten (diminishing returns, token cost, and on long prompts, diluted attention). Cover the boundaries: a typical case, an edge case, and the tricky case the model keeps getting wrong — including a 'when unsure, output X' demonstration.
- When does few-shot beat just writing better instructions?
- When the requirement is easier to show than say: exact output formats, house style, subtle classification boundaries, conventions like 'how we write API handlers.' Adjectives drift; demonstrations pin. For behavior that's genuinely rule-like ('never include PII'), instructions remain the right tool — most strong prompts use both.
Related
- Few-Shot vs Chain-of-Thought vs Structured Prompting: What to Use When (2026)When to reach for few-shot examples, chain-of-thought reasoning, or structured/output-constrained prompting — a 2026 decision guide to the core techniques.
- Zero-Shot PromptingZero-shot prompting asks a model to perform a task from instructions alone, with no examples — the default mode for capable modern LLMs.
- Prompt Patterns for Coding AgentsPractical prompting patterns: chaining, few-shot, context management, tool use, and output structuring.
- Chain-of-Thought (CoT)Chain-of-thought prompting has a model work through intermediate reasoning steps before answering — improving accuracy on multi-step problems.
- Prompt OptimizerDiagnose why a prompt underperforms and rewrite it with the technique that fixes it — clearer structure, few-shot examples, an explicit output contract, or reasoning scaffolding — returning an optimized prompt, the rationale for every change, and what to measure to confirm the lift. Use when a prompt is flaky, verbose, drifting in format, or just not good enough.
- Prompt TemplateA prompt template is a parameterized prompt — fixed instructions with variable slots — turning prompts from strings into versioned, testable components.