Few-Shot Prompting: Teach AI by Example in 5 Minutes
You don’t need to fine-tune a model to teach it new tricks. You just need good examples.
Few-shot prompting is the art of showing an AI what you want by giving it 2-5 examples, then letting it pattern-match on your actual task. It’s fast, flexible, and works shockingly well.
Zero-Shot vs Few-Shot vs Many-Shot
Zero-shot: No examples. Just instructions.
Classify this review as positive or negative: "The battery dies after 2 hours."
Few-shot: 2-5 examples before your actual task.
Review: "Best purchase I've made this year!" โ Positive
Review: "Broke after one week." โ Negative
Review: "It's okay, nothing special." โ Neutral
Review: "The battery dies after 2 hours." โ
Many-shot: 10+ examples. Useful for nuanced tasks but eats context window.
For most tasks, 3-5 examples hit the sweet spot between accuracy and efficiency.
Why Few-Shot Works
Large language models are pattern completion engines. When you provide examples, you’re not “training” the model โ you’re activating the relevant patterns it already learned during pre-training.
The examples serve three purposes:
- Define the task without ambiguous instructions
- Set the format by demonstration
- Calibrate the difficulty and expected depth
The Golden Rules of Example Selection
Rule 1: Diversity Over Quantity
Three diverse examples beat ten similar ones.
Bad (all similar):
"I love this product!" โ Positive
"Amazing quality!" โ Positive
"Best ever!" โ Positive
"The screen cracked." โ ?
Good (covers the spectrum):
"I love this product!" โ Positive
"The screen cracked on day one." โ Negative
"It works fine, nothing special." โ Neutral
"The screen cracked." โ ?
Rule 2: Include Edge Cases
If your task has tricky cases, show one in your examples.
"Not bad at all, actually pretty good." โ Positive
"I wouldn't say it's terrible." โ Neutral
"Could be worse, I guess." โ Negative
Sarcasm, double negatives, and ambiguity are where few-shot really earns its keep.
Rule 3: Match Your Target Format Exactly
If you want JSON output, your examples should output JSON. If you want bullet points, your examples should use bullet points.
Input: "Python web framework"
Output: {"name": "Flask", "category": "web", "difficulty": "beginner"}
Input: "JavaScript runtime"
Output: {"name": "Node.js", "category": "runtime", "difficulty": "intermediate"}
Input: "Container orchestration"
Output:
The model will mirror your format precisely.
Practical Templates
Text Classification
Classify the support ticket priority.
"My account is hacked and money is missing" โ P0-Critical
"The app crashes when I upload photos" โ P1-High
"Can you change the font size option?" โ P3-Low
"Login page loads slowly on weekends" โ P2-Medium
"I can't access any of my files since the update" โ
Data Extraction
Extract structured data from the job posting.
Posting: "Senior React Developer at Stripe, NYC, $180-220k, 5+ years exp"
Result: {role: "Senior React Developer", company: "Stripe", location: "NYC", salary: "$180-220k", experience: "5+ years"}
Posting: "ML Engineer, Google, Remote US, $200-280k, PhD preferred"
Result: {role: "ML Engineer", company: "Google", location: "Remote US", salary: "$200-280k", experience: "PhD preferred"}
Posting: "DevOps Lead at Shopify, Toronto, CAD 150-190k, 7+ years"
Result:
Style Transfer
Rewrite the sentence in a casual, friendly tone.
Formal: "We regret to inform you that your application has been unsuccessful."
Casual: "Hey, unfortunately we won't be moving forward with your application this time."
Formal: "Please be advised that the system will undergo maintenance."
Casual: "Heads up โ we're doing some maintenance on the system."
Formal: "Your inquiry has been received and will be processed within 5 business days."
Casual:
Code Generation
Write a Python function based on the description.
Description: "Check if a string is a palindrome"
Code:
def is_palindrome(s: str) -> bool:
cleaned = s.lower().replace(" ", "")
return cleaned == cleaned[::-1]
Description: "Find the most common element in a list"
Code:
def most_common(lst: list):
from collections import Counter
return Counter(lst).most_common(1)[0][0]
Description: "Merge two sorted lists into one sorted list"
Code:
Advanced: Chain Few-Shot with CoT
Combine few-shot examples with chain-of-thought reasoning for maximum accuracy:
Question: If a shirt costs $25 and is 20% off, what do you pay?
Thinking: Original price is $25. Discount is 20% of $25 = $5. Final price = $25 - $5 = $20.
Answer: $20
Question: A laptop is $800 with a 15% discount plus 8% tax on the discounted price. What's the total?
Thinking:
This gives the model both the pattern (few-shot) and the reasoning approach (CoT).
When Few-Shot Fails
- Task is too novel. If the model has never seen anything like your task during training, examples alone won’t help. You might need fine-tuning.
- Examples are misleading. Bad examples teach bad patterns. Garbage in, garbage out.
- Context window overflow. Too many examples leave no room for the actual task. Keep it lean.
Recommended Gear
The Art of Prompt Engineering with ChatGPT
View on Amazon โKey Takeaways
- 3-5 diverse examples usually outperform lengthy instructions.
- Your examples define the task, format, and difficulty โ choose them carefully.
- Always include at least one edge case.
- Combine with CoT for complex reasoning tasks.
- Test with your actual data, not just toy examples.
Few-shot prompting is the closest thing to a universal technique in prompt engineering. Master it, and you’ll handle 80% of tasks without any fancy tooling.