Prompt Engineering & Structured Output

Few-Shot Examples (2–4)

9 分で読めます

Few-Shot Examples (2–4)

Few-shot prompting means showing Claude 2–4 worked examples of the input → output mapping you want, before giving it the real input. Examples are often more powerful than prose instructions because they demonstrate format, tone, edge-case handling, and the exact transformation all at once. The model pattern-matches against them.

Why 2–4?

  • Zero-shot (no examples) relies entirely on instructions and the model's priors — fine for simple, common tasks.
  • One-shot anchors format but a single example can be over-fit; the model may copy incidental details.
  • 2–4 examples is the sweet spot for most classification, extraction, and formatting tasks: enough to triangulate the pattern and cover representative variety without bloating the prompt or burning tokens.
  • More than a handful rarely helps for these tasks and increases cost and latency. (When you genuinely have many labeled examples, fine-tuning or retrieval is the better tool — not stuffing 50 examples into every request.)

Make examples representative, not redundant

The examples should span the decision boundaries you care about, including at least one tricky or edge case. If you are classifying sentiment, include a clearly negative, a clearly positive, and one mixed/ambiguous example so the model sees how you want ambiguity resolved. Redundant near-identical examples waste tokens and teach nothing new.

Format with clear delimiters

Use consistent, unambiguous structure so the model can tell examples apart from the live request. XML-style tags work well with Claude:

xml

Three examples cover positive, negative, and mixed — the model now knows how to handle each, and the output format is demonstrated rather than described.

Few-shot and consistency

Examples sharply reduce output drift. If your examples always emit lowercase enum values and never include trailing prose, the model will follow suit. This pairs naturally with structured output: a few JSON examples teach the exact shape, and tool_use (Module 2) then enforces it. Examples shape behavior; schemas guarantee structure.

Common pitfalls

  • Unbalanced examples — three positives and one negative biases the model toward "positive."
  • Leaking the answer — examples that include reasoning you do not want copied verbatim.
  • Inconsistent formatting — one example uses single quotes, another double; the model imitates the noise.

Exam focus

Know the heuristic: 2–4 representative examples, including an edge case, beat both zero-shot and a wall of examples for typical classification/extraction tasks. Examples demonstrate format and edge-case handling more reliably than prose. Keep examples balanced and consistently formatted, and delimit them clearly (e.g., XML tags) so they are distinguishable from the live input.