Intermediate10 min

Few-Shot Prompting Patterns

How to use few-shot examples to achieve consistent formatting, handle ambiguous cases, and generalize to novel patterns. Covers when few-shot beats zero-shot, how to select examples, and patterns for extraction, tool selection, and classification.

Quick Reference

  • Few-shot = providing 2-6 input/output examples before the actual task
  • Most effective for: consistent formatting, ambiguous-case handling, and extraction tasks
  • Examples should show reasoning for WHY a decision was made, not just the outcome
  • Include at least one example of each edge case you care about
  • Examples enable generalization to novel patterns -- Claude learns the PATTERN, not just the specific cases
  • Order matters: put the most representative example first and the trickiest edge case last
  • Few-shot reduces hallucination in extraction by showing what 'null' looks like
  • Diminishing returns after 5-6 examples for most tasks
  • Always include a 'negative example' showing what NOT to do or flag

When Few-Shot Outperforms Zero-Shot

Zero-shot (no examples) works well when the task is unambiguous: summarize this text, translate this sentence, answer this factual question. Few-shot becomes essential when there is ambiguity in the expected output -- when reasonable people could disagree on the correct answer, format, or level of detail.

ScenarioZero-shot sufficient?Few-shot needed?Why
Translate English to FrenchYesNoOutput format is unambiguous
Extract invoice fieldsPartialYesAmbiguity in field boundaries, what counts as a line item
Choose which tool to callPartialYesBorderline cases where multiple tools could apply
Classify support ticket severityNoYesSeverity thresholds are subjective without examples
Format code review outputNoYesMany valid formats; examples lock in the specific one you want
Distinguish real bugs from style issuesNoYesThe boundary is project-specific and must be demonstrated
The generalization principle

Few-shot examples are NOT just a lookup table. Claude extracts the underlying PATTERN from examples and applies it to novel inputs. Three examples of 'this code pattern is acceptable, don't flag it' teaches Claude to recognize similar acceptable patterns it has never seen -- not just the three you showed.