Few-Shot Prompting
Giving examples to steer output style and format
Skills taught: llms.few-shot
Zero-Shot vs One-Shot vs Few-Shot
Including concrete input-output examples in the prompt teaches the model the exact format and style you want. It's the most reliable way to get consistent structured output, because models imitate patterns far more reliably than following written descriptions.
- Zero-Shot
- No examples — just instructions. Fast but less reliable for precise formats. 'Classify sentiment as POSITIVE/NEGATIVE/NEUTRAL.'
- One-Shot
- One example plus the actual task. Good baseline improvement with minimal token cost.
- Few-Shot
- 2–10 examples. The sweet spot for most tasks. More examples improve consistency but cost more tokens.
import anthropic
client = anthropic.Anthropic()
# Few-shot: 3 examples covering each label
system = """Classify the sentiment of product reviews.
Respond ONLY with one word: POSITIVE, NEGATIVE, or NEUTRAL.
Examples:
Review: "This laptop is incredible — best purchase I've made!"
Sentiment: POSITIVE
Review: "Battery died after 2 months. Very disappointed."
Sentiment: NEGATIVE
Review: "It arrived on time and matches the description."
Sentiment: NEUTRAL"""
test_reviews = [
'Review: "The keyboard feels a bit mushy but I love the screen."',
'Review: "Absolutely useless. Returned immediately."',
'Review: "Good value for the price. Does what it says."',
]
for review in test_reviews:
response = client.messages.create(
model="claude-haiku-5-5",
max_tokens=10,
temperature=0,
system=system,
messages=[{"role": "user", "content": review}]
)
print(f"{review[:50]}... → {response.content[0].text.strip()}")
Few-Shot for Structured Data Extraction
import anthropic, json
client = anthropic.Anthropic()
system = """Extract structured data from business card text. Return JSON only.
Example input: "Dr. Sarah Chen, CTO at Nexus AI, sarah.chen@nexusai.io, +1-415-555-0100"
Example output: {"name":"Sarah Chen","title":"CTO","company":"Nexus AI","email":"sarah.chen@nexusai.io","phone":"+1-415-555-0100"}
Example input: "Marcus Johnson - Lead Developer | Buildify Corp\nm.johnson@buildify.com"
Example output: {"name":"Marcus Johnson","title":"Lead Developer","company":"Buildify Corp","email":"m.johnson@buildify.com","phone":null}"""
card_text = "Priya Sharma, VP of Engineering, TechFlow Inc. priya@techflow.io | 650-555-9876"
response = client.messages.create(
model="claude-haiku-5-5",
max_tokens=200,
temperature=0,
system=system,
messages=[{"role": "user", "content": card_text}]
)
data = json.loads(response.content[0].text)
print(data)
Best Practices for Few-Shot Examples
- Cover all classes: For classification, include at least 1 example of each label — equal representation prevents the model biasing toward the most-seen class
- Include edge cases: Show what to do when input is ambiguous or malformed
- Use 3-5 examples: More is not always better — it uses up context. 3 good examples typically beats 10 mediocre ones
- Match distribution: Examples should reflect real production inputs, not cherry-picked easy ones
- Last example matters most: The model weighs the most recent example more heavily — put your most representative example last
Tip: Use 3–5 examples that cover edge cases and all output labels. More examples are not always better — they use up context window and can introduce conflicting patterns.
Warning: Bad examples actively hurt performance. If your examples contain errors, inconsistencies, or don't cover edge cases, few-shot can perform WORSE than zero-shot. Always test examples on a held-out set.
Frequently Asked Questions
What is the difference between zero-shot and few-shot?
Zero-shot has no examples — the model uses only instructions. Few-shot includes examples. Few-shot is more reliable for precise formats and edge cases. Zero-shot is cheaper and faster.
How many examples should I use?
3-5 examples is the sweet spot. For classification, 1-2 examples per class. For complex extractions, 3-4 diverse examples. More examples help diminishingly and cost more tokens.
Should examples go in the system prompt or user message?
Either works. For Claude, examples in the system prompt are weighted heavily and don't repeat per request (cost-efficient). Examples in the user message are more flexible for dynamic tasks.