Few-shot prompting for consistency
What this covers
- Reach for examples when detailed instructions still produce inconsistent output
- Use examples to demonstrate ambiguous-case handling and the reasoning behind it
- Reduce fabrication in extraction with format-varied examples
- Keep the set to two to four examples rather than maximizing coverage
- Explain how examples generalize to patterns they do not contain
- Show one example on each side of a boundary rather than one side only
Key terms
- Few-shot examples
- A small set of examples included in a prompt to show the model the expected behavior.
- Structured output
- Model output constrained to a defined format that software can read reliably.
When instructions have already failed
Few-shot examples are the most effective technique for getting consistently formatted output that a person or program can use once detailed instructions have not delivered it. The signal to switch is not complexity — it is inconsistency across runs on similar input.
Two to four, chosen for ambiguity
The useful count is small: two to four targeted examples. General prompting guidance suggests three to five; the difference does not matter much, and what both agree on is that the number is small and the selection is what counts. Choose examples from the ambiguous cases, not the obvious ones. Common cases are usually already handled correctly, so repeating them adds little. Use the space where the model must make a difficult choice, and show why one action was chosen over a plausible alternative.
An example that shows only input and output teaches one mapping. An explanation of why the output is correct teaches the decision rule that can be applied to a new case.
Generalization, not matching
Well-chosen examples teach a decision rule that can apply to new cases. They do more than show the exact inputs they contain. That's why four examples near a difficult boundary can teach more than twenty repetitive examples.
Distinguishing acceptable patterns from genuine issues is the same technique aimed at false positives: show one of each, with the reasoning, and precision improves without a rule for every case.
The following examples show acceptable and unacceptable handling of the same kind of error:
Input: catch (err) { logger.warn(err); return cached; }
Output: skip
Why: the failure is handled and the fallback is deliberate
Input: catch (err) { }
Output: flag
Why: the error is discarded, so the failure is invisible at runtimeThe Why: lines are the part that generalizes. A third case neither example contains — a catch that only increments a counter — is decided by the same question: is the failure still visible afterward?
Extraction and hallucination
Examples are also the lever for reducing hallucination in extraction. Show correct handling of the messy realities — informal measurements, inline citations versus bibliographies, methodology stated up front versus buried in a paragraph.
If a required field comes back empty or invented, show correct extraction from several document structures. The examples teach the model how the same field can appear in different layouts.
few-shot examplesoutput formatextraction
Field note — common misconceptions
- MythThat examples only teach the exact cases shown
- ActuallyExamples showing the reasoning extend the same judgment to novel patterns never shown.
- MythThat more examples are always better than well-chosen ones
- ActuallyTwo to four examples chosen at the ambiguous cases beat twenty repetitive ones.
- MythThat examples are for format only, not judgment
- ActuallyAn example that shows why one action was chosen over another teaches a judgment, not a mapping.
Guided review
Review this lesson as a study deck
Review the lesson's main ideas in five guided slides, then test yourself with three flashcards.
Open Task 4.2 study deckCross-domain reasoning
Connect this idea
Evaluation continues through delivery
Clear criteria, examples, repeated testing, continuous integration, independent review, and human review all provide evidence that a change works as intended.
- 3.5 · Apply iterative refinement techniques for progressive improvement
- 3.6 · Integrate Claude Code into CI/CD pipelines
- 4.1 · Design prompts with explicit criteria to improve precision and reduce false positives
- 4.6 · Design multi-instance and multi-pass review architectures
- 5.5 · Design human review workflows and confidence calibration
Applied practice
Practice this lesson in a lab
Use a related lab to create a decision, implementation or diagram, evidence record, and review.
- Lab 8 · Structured extraction and batch evaluation
Compare extraction strategies against labeled synthetic data without hiding critical-field failures.