Few-shot prompting: a complete guide
Stop explaining the task and just show it. That's the whole of few-shot prompting: drop a handful of input-output examples into the prompt, and the model reads the pattern off them and applies it to your new input — no fine-tuning, no parameter updates, no ML expertise. It's the practical middle ground between zero-shot (cheap but inconsistent) and fine-tuning (powerful but needs data and compute), and with just 2–5 well-chosen examples it typically buys 20–40% over zero-shot. Brown et al. (2020) first showed with GPT-3 that a large model can infer a task from demonstrations alone — the result that kicked off in-context learning (ICL).
See it work
Sentiment classification is the cleanest way to feel the difference. Ask cold and the model can't even agree with itself on the format, let alone the label. Show it three examples and it snaps into line.
Input: "The plot dragged but the acting saved it."
Zero-shot, asked 3 times:
→ "Mostly positive, though there are some criticisms..."
→ "I'd say neutral-to-mixed."
→ "Positive. The reviewer praises the acting." ← three runs, three formats, two labels
Few-shot:
Review: "Best meal I've had all year." Sentiment: Positive
Review: "Cold food, slow service." Sentiment: Negative
Review: "It was fine, nothing special." Sentiment: Neutral
Review: "The plot dragged but the acting saved it." Sentiment:
→ "Mixed" ← one word, same every run
Same model, same question. The three examples did three jobs at once: pinned the output format (one label, not a paragraph), showed the allowed vocabulary, and demonstrated how to weigh competing signals. On real sentiment tasks that jump looks like 65% to 89% accuracy with five examples. You didn't teach the model what sentiment is — you showed it what an answer looks like.
The mental model
Few-shot prompting is conditional text completion. The model sees Example 1, Example 2, Example 3, New input → ? and predicts the next text by matching the pattern the examples established. The demonstrations tilt its probability distribution toward outputs that fit the pattern, making them far more likely than the generic response.
Show, don't tell. Three examples often convey intent better than three paragraphs of instructions.
The deeper story is meta-learning — "learning to learn." The model didn't learn your task during the prompt; it learned how to learn from demonstrations during pretraining. Each few-shot prompt is a small Bayesian update: the examples are the prior, and the model shifts its posterior over outputs to match them. That's why it works with no gradient steps — the conditioning happens entirely in the forward pass.
The trade-offs you're balancing:
- Verbosity vs performance — examples cost tokens but raise quality.
- Context budget vs example count — limited space forces quality over quantity.
- Generalization vs memorization — too-similar examples cause overfitting.
- Diversity vs relevance — varied examples generalize; targeted ones activate the right pattern.
The assumptions it rests on: the model saw similar patterns in pretraining, your examples represent the real distribution, the task is demonstrable as input-output pairs, and the examples plus query fit the context window. When those fail — novel capability, misleading examples, blown context — few-shot fails with them.
How it works
It's single-pass: the model processes examples and query together, then generates in one shot. But internally there's a clear sequence.
- Example processing. The model encodes all examples; attention links patterns across them and shifts internal representations toward the demonstrated task.
- Pattern extraction. It infers the implicit rule — the mapping, the commonalities, the format — and builds the conditional
P(output | input, examples), activating aligned pretrained knowledge. - Query processing. The new input is read against that example-conditioned state; the model hunts for the analogous pattern.
- Generation. It produces output matching the demonstrated format and stops at the learned boundary. No iteration, no updates.
Why it works
Examples help through four distinct channels:
- Task specification. Demonstrations communicate intent more precisely than instructions — "show don't tell."
- Format alignment. Examples pin down exact structure and style, dropping format violations from 30–50% to 5–15%.
- Disambiguation. When instructions are vague, examples resolve the interpretation concretely.
- Distribution conditioning. Examples shift probability toward task-relevant outputs and suppress low-quality ones.
Dominant factors
Not all of an example's job matters equally. Ranked by share of effectiveness:
| Factor | Share | Why |
|---|---|---|
| Example relevance | 40% | Similar examples activate the correct pattern |
| Example correctness | 30% | One wrong example can poison every output |
| Example count | 15% | More helps — until it doesn't |
| Format consistency | 10% | Uniform structure reinforces the pattern |
| Example ordering | 5% | Recency bias weights later examples more |
Emergent behaviors
Watch for these — two help, two hurt. Models generalize beyond the shown examples and hold format adherence even on creative tasks. But they also amplify bias baked into examples, and fall for shortcut learning — latching onto a spurious correlation (sentence length, say) instead of the intended signal.
Where it shines
Few-shot lands 10–40% over zero-shot across a wide span of tasks, with no training cost and instant adaptation — change the examples, change the behavior.
- Text classification: sentiment (65% → 89% with 5 examples), topic, intent, spam.
- Named entity recognition: 15–25% F1 gains over zero-shot.
- Code generation: the MANIPLE framework (2024) showed a 17% lift in bug fixes via algorithmic example selection; examples also carry coding conventions.
- Translation: adaptive selection (AFSP, 2025) improved BLEU by 8–12%.
- Data extraction & format conversion: examples lock the target structure (JSON, CSV, markdown).
- Clinical NLP: 15–30% accuracy gains on diagnosis classification and note parsing.
- Customer support: few-shot intent classification improves routing accuracy 25–35%.
- Legal analysis: contract-clause extraction reaches 70–80% of expert performance with domain examples.
It also reaches further than you'd expect — protein-structure annotation, time-series forecasting, preference learning, regulatory research — anywhere a task can be shown as input-output pairs.
The format dividend is underrated. Beyond accuracy, examples make outputs parseable. Structured-output compliance jumps to 85–95% with three well-formatted examples, versus 50–70% from instructions alone. For anything feeding a downstream system, that reliability is often worth more than the accuracy bump.
When to use it (and when not)
Reach for it when zero-shot is inconsistent, you have 2–10 representative examples on hand, the task has clear input-output structure, format consistency matters, or fine-tuning is too costly.
Skip it when the task needs deep multi-step reasoning (use chain-of-thought), you're on a reasoning model, examples are contradictory or noisy, or the context window can't hold them.
Reasoning models flip the rule. Research (2025) shows reasoning models like O1 and O3 degrade with few-shot — they prefer zero-shot. This reverses the old "few-shot is always better" wisdom. Check the model class before reaching for examples.
Model fit: GPT-3-scale (175B) is the floor for reliable ICL; 7B–20B models work with 5–7 examples but waver; GPT-4, Claude 3, Gemini Pro, and Llama 70B+ are the strong zone. Models below 7B parameters typically lack in-context learning entirely.
Example count: 2 is the minimum that shows a pattern, 3–5 is the sweet spot for most tasks, 6–8 helps high-variability or edge-case-heavy tasks, and 9+ rarely pays — diminishing returns and wasted context.
Budget the context. Each example runs 50–200 tokens; five examples plus query and output land around 500–2000 tokens total. You want at least a 4K context (8K+ comfortable). Latency rises modestly too — few-shot adds 20–50% over zero-shot's 1–3 seconds, though some APIs cache the example prefix.
Escalate to fine-tuning when you have 100+ quality examples, a stable task, high volume (above 10K queries/day makes per-call tokens expensive), and engineering for a training pipeline — fine-tuning typically wins another 10–20%.
| Variant | What it does | When |
|---|---|---|
| Standard few-shot | Static examples for every query | Stable, low-diversity tasks |
| KATE (semantic selection) | Retrieve K-nearest examples per query | High query diversity, large example pool |
| Few-shot CoT | Examples include reasoning steps | Math, logic, multi-step |
| Few-shot + RAG | Vector-retrieve examples at query time | 50+ examples, performance-critical |
| Few-shot + self-consistency | Sample many, majority-vote | High-variance outputs |
Structure and components
A few-shot prompt has five parts: an optional task instruction, the demonstrations (the example pairs), clear delimiters between them, the query input, and an output cue (which can be implicit).
Design principles
Keep examples immediately understandable; enforce uniform structure (consistency reinforces the pattern); cover varied cases for generalization; favor relevance to activate the right pattern; mind ordering (recency bias means your most important example goes last); use clear delimiters ("Input:", "Output:", newlines); and keep the language natural.
Patterns
The basic input-output skeleton:
Input: [example input 1]
Output: [example output 1]
Input: [example input 2]
Output: [example output 2]
Input: [new query]
Output:
A conversational variant swaps the labels for User: / Assistant: turns — useful for chat models and multi-turn behavior.
User: [question 1]
Assistant: [answer 1]
User: [question 2]
Assistant: [answer 2]
User: [new question]
Assistant:
The chain-of-thought variant adds reasoning between question and answer, which roughly doubles performance on math and logic:
Q: Roger has 5 tennis balls. He buys 2 cans of 3 balls each. How many now?
A: Roger started with 5. 2 cans × 3 = 6. 5 + 6 = 11. Answer: 11
Q: [new question]
A: Let's think step-by-step.
Scenario tweaks: raise diversity to 5–8 examples for high-variability tasks, use XML/JSON delimiters for complex formats, add a brief instruction before examples for ambiguous tasks, compress examples when context is tight, and seed domain terminology for specialized tasks. Watch the boundary conditions — few-shot breaks on contradictory examples, tasks beyond pretraining, or unrepresentative demonstrations, and it's capped by the context window (typically 2–20 examples).
Implementation
Workflow
- Baseline. Run zero-shot first to know what you're beating.
- Collect. Gather 5–10 real input-output pairs; verify correctness; ensure diversity.
- Draft. Format 3 examples consistently, add a brief instruction if needed, include the query.
- Test. Run 10 varied cases; measure accuracy or quality.
- Iterate. Find failure patterns, add examples that address them, reorder (important ones last), adjust formatting, re-test.
- Finalize. Document the prompt, record metrics, note edge cases.
Best practices
Example selection. Start with 3 correct, clear, unambiguous examples; scale to 5–7 only if the pattern demands it. Keep them concise, cover different input variations and edge cases, and don't cherry-pick only "perfect" cases. Order common cases first, edge cases last, with your most important example at the very end (recency bias). Choose a selection strategy: query-agnostic (a fixed diverse set) or query-specific (retrieve K-nearest per query via RAG).
Format and output control. Use identical formatting across all examples with unambiguous delimiters. Drop temperature to 0.0–0.2 for consistency, add stop sequences to prevent over-generation, and validate the output format programmatically.
Do and don't
Do verify every example before using it, test on inputs unlike your examples, balance relevance with diversity (a 60/40 split works), and version your example sets.
Don't use contradictory, erroneous, or biased examples; mix formatting styles; overload with near-duplicate examples; use few-shot on reasoning models; or test only on inputs similar to your examples.
Debugging
| Symptom | Root cause | Fix |
|---|---|---|
| Inconsistent outputs | Loose format, high temperature | Tighten delimiters, lower temperature |
| Pattern not learned | Noisy or unclear examples | Simplify examples; add a brief instruction |
| Overfitting | Examples too similar | Increase diversity; test on very different inputs |
| Context overflow | Too many/long examples | Cut to 3–5, compress, or switch to RAG |
| Verbatim reproduction | Memorization | Diversify examples; add adversarial cases |
| Spurious correlations | Shortcut learning | Vary the irrelevant features across examples |
Examples encode bias quietly. Few-shot amplifies five kinds — selection bias (cherry-picked sources), majority-label bias (favoring frequent labels), demographic bias (gender/race/age, especially harmful in hiring, lending, healthcare), content bias (sentiment imbalance), and framing effects (order and phrasing). Balance your example distribution even when reality is skewed, use neutral language, run counterfactual tests (swap demographics, measure output change), and audit periodically.
Testing
Build 20–50 test cases split roughly 60% common, 30% edge, 10% adversarial, covering happy path, boundary, invalid, ambiguous, and out-of-scope inputs. Reserve a holdout of 20–30 examples that never appear in prompts for final evaluation. For small datasets, cross-validate across different example combinations; A/B test competing example sets on the same inputs.
Use task-appropriate metrics — accuracy/precision/recall/F1 for classification, exact/partial match for extraction, BLEU/ROUGE/semantic similarity for generation, syntax and functional tests for code. And measure example quality itself: diversity (embedding distance), relevance (similarity to the test set), correctness (human validation), and coverage (do they span the input distribution?).
A useful stopping rule: plot performance against example count and stop adding when each new example buys under 2%.
Limitations
- Context window. With 4K–32K tokens and examples eating 200–1000, you're capped at 2–20 examples — often too few for complex or highly variable tasks.
- Task-complexity ceiling. Great at pattern matching, weak at deep reasoning or knowledge beyond what examples convey.
- Example dependency. Performance lives and dies by example quality; a single bad one can degrade everything, and no examples means no technique.
- Overfitting. Too-similar examples make the model memorize instead of generalize.
- Reasoning-model incompatibility. O1/O3 degrade with few-shot — a real reversal of conventional wisdom.
- Bias amplification. Subtle example biases magnify, and they're harder to spot than instruction biases.
- Cascading failures. One bad example → bad pattern → every output affected.
Solved inefficiently: complex multi-step reasoning (use CoT), knowledge-heavy tasks (use RAG or fine-tuning), highly creative generation (examples constrain it), thousand-category classification (context overflow).
Graceful degradation: if few-shot fails, fall back to zero-shot with a clear instruction; flag inputs far from all examples for human review; re-select examples periodically as production data drifts.
Advanced techniques
Example selection and ordering
- Semantic similarity (KATE). Encode examples and query with a sentence transformer, pick the top-K by cosine similarity. 10–20% better than random; available as LangChain's
SemanticSimilarityExampleSelector. - Diversity-based. Cluster the example space and select across clusters; balance ~60% most-similar with ~40% diverse to prevent overfitting.
- Complexity matching. Match example difficulty to query difficulty — 8–15% gains by avoiding difficulty mismatch.
- Strategic ordering. Recency bias is real; place the most representative example last, run typical-to-edge, and A/B test orderings.
- Dynamic vs static. Static is simpler and faster; dynamic (RAG-retrieved per query) performs better on diverse inputs; a hybrid keeps a static core plus 1–2 dynamic examples.
Reasoning and output control
Few-shot CoT (reasoning steps in each example) roughly doubles math/logic performance. Self-verification examples — answer, then check, then confirm/correct — cut errors 10–15% where verification is clear. For structured output, a few consistently-formatted JSON examples push compliance to 85–95%. Examples also transfer style and tone: demonstrate the voice and the model adopts it.
Interaction patterns
Conversational few-shot establishes multi-turn behavior; self-refinement examples (initial vs improved output) teach iterative polishing; and chaining runs multi-stage pipelines where each stage has its own examples and each output format feeds the next stage's input.
User-provided examples are an attack surface. When users supply examples, they can inject instructions, teach harmful patterns, or jailbreak via "innocent" demonstrations that sit in context for the real query. Sanitize and validate user examples, content-filter them, restrict their sources, and lock example sets for sensitive tasks.
Ecosystem
Examples that work on GPT-4 usually transfer to Claude and Gemini, though delimiters and structure may need adjusting — test across models, since some are more sensitive to example quality than others, and model updates can shift sensitivity.
Integration patterns: Few-shot + RAG builds a vector DB of 100–1000 examples and retrieves K=3–5 nearest per query. Few-shot + fine-tuning uses the tuned model for base performance and few-shot for edge cases. Few-shot + agents demonstrates tool-use patterns with examples selected by agent context.
Notable variants: KATE (2022, K-nearest selection, 10–20% over random), Conversational Few-Shot (2025, multi-turn structure, 10–15% over standard for chat), Adaptive Few-Shot (AFSP, 2025, per-input selection, 8–12% BLEU on translation), and MANIPLE (2024, statistical subset selection, 17% on bug fixes).
Transition from zero-shot: identify the failure patterns, collect 5–10 examples covering them, select 3–5 best, A/B test on ~50 queries, deploy if you clear ~15% improvement, then keep iterating.
Few-shot sits inside in-context learning (it's ICL's primary method), draws on meta-learning ("learning to learn"), and leverages transfer from pretraining. It's one specific tool within the broader prompt-engineering toolbox.
Future directions
Adaptive selection — systems that learn which examples work per query type via meta-learning — projects 20–30% gains over static sets. Personalized few-shot tailors example sets to individual users. Multi-modal few-shot mixes text, images, and code for vision-language models. Federated example learning aggregates examples across organizations without sharing raw data. And novel combinations are emerging: few-shot + active learning (request examples for uncertain cases), few-shot + explainability (examples as explanations), and few-shot + curriculum learning (progressive difficulty). The open frontiers: a theoretical account of ICL, better selection algorithms, cross-lingual transfer, ICL in smaller models, automated example generation, and a precise map of when few-shot helps versus hurts — the reasoning-model case being the sharpest open question.
The result that started it all. Brown et al. (2020) showed GPT-3 (175B) could learn a task from a few in-prompt examples with zero parameter updates — the demonstration that launched in-context learning. Everything above is engineering around that one surprising capability: a model that learns how to learn during pretraining, then applies it on the fly.
Summary
- Few-shot prompting puts a few input-output examples in the prompt; the model infers the pattern and applies it — no fine-tuning, typically 20–40% over zero-shot with 2–5 examples.
- It works through in-context learning: examples condition the model's output distribution in a single forward pass (Brown et al., 2020).
- Effectiveness is driven mostly by example relevance (40%) and correctness (30%); 3–5 examples is the sweet spot, ordered with the most important last.
- Reach for it when zero-shot is inconsistent and you have clean examples; skip it for deep reasoning (use CoT), reasoning models like O1, or when examples are noisy.
- Mind the failure modes — context limits, overfitting, bias amplification, and cascading failures from a single bad example.
- Scale up with KATE selection, few-shot CoT, and RAG retrieval; escalate to fine-tuning past ~100 stable examples and high volume.
Read Next
Start reading to get personalized recommendations
Explore Unread
Great job! You've read all available articles