Lesson 2

Few-Shot Learning

Learning from examples

The Problem: Sometimes the AI doesn't understand exactly what format or style you want. How can you show it precisely what you need without lengthy explanations?

The Solution: Show, Don't Tell

Few-shot prompting means giving the model a few worked examples of the task inside the prompt, right before asking it to handle a new input. Instead of describing the rules in words, you show the model two to five input→output pairs and let it infer the pattern. It's like onboarding a new employee with sample work instead of a 10-page manual: people, and language models, often learn faster from demonstrations than from abstract instructions. Unlike Zero-Shot (no examples at all), few-shot teaches by demonstration.

The mechanism behind this is called in-context learning: the model is not retrained or updated, it simply conditions its next prediction on everything already in the prompt, including your examples. Because the examples sit in the context window, they steer the model toward the right format, tone, and decision boundary for a single request and then vanish, they have no lasting effect on the model's weights. This makes few-shot ideal when you need a very specific output shape (a JSON schema, a fixed label set, a house writing style) that is hard to specify precisely in prose. It also composes well with other techniques: for multi-step reasoning, pair it with Chain-of-Thought so each example also shows the reasoning, not just the final answer.

Tradeoffs and a worked example

The main costs are tokens and bias. Every example you add consumes context-window space and increases latency and price, and models are sensitive to example order and class balance, listing four positive reviews then one negative can skew predictions toward "positive". The fix is curation, not volume: 3-5 diverse, correctly labeled examples that cover the tricky edge cases usually beat 20 random ones. Concretely, to classify support tickets you might show: "My card was charged twice" → Billing, "The app crashes on login" → Bug, and "Can you add dark mode?" → Feature request. Faced with a new ticket like "I was billed after I cancelled", the model now reliably outputs Billing in the exact one-word format your examples established, no lengthy instructions required.

Think of it like training a new employee:

  • 1. Example 1: "When a customer says 'I'm angry', we respond: 'I understand your frustration...'"
  • 2. Example 2: "When they say 'This is broken', we respond: 'I'm sorry to hear that...'"
  • 3. Now you try: "Customer says 'I want a refund'..."
  • 4. Employee learns: The pattern of empathetic, helpful responses

Where Is This Used?

  • Custom Formatting: Specific output formats, styles, or structures
  • Domain-Specific Tasks: When you need industry-specific language
  • Classification: Teaching categories through examples
  • Data Transformation: Converting data from one format to another

Fun Fact: Research shows that just 3-5 well-chosen examples often work better than 20+ random ones. Quality over quantity! The examples should cover different cases and edge scenarios.

Try It Yourself!

Use the interactive example below to see how adding examples changes the AI's output. Try adding more examples and see how the pattern recognition improves.

Few-Shot Learning — Learning from Examples

📚 Few-shot — a technique where we show the model several examples BEFORE the task. More examples = better format understanding!

Choose a task:
Number of examples:
0
Prompt the model will receive:
Determine the sentiment of text
Input: Not bad, but could be better
Model output:
Zero-shot(no examples)
This expresses mixed feelings with slight disappointment...
ModeExamplesQualityWhen to use
Zero-shot0⭐⭐Simple tasks
One-shot1⭐⭐⭐Show format
Few-shot2-5⭐⭐⭐⭐⭐Complex/unusual tasks
Practical Prompt Examples

Ready-to-use templates for copying. Replace {review} with your text.

Zero-shotNo examples

Classify the review as positive or negative:
"{review}"

Suitable for simple tasks where the response format is clear from the instruction.

One-shotSingle example

Classify the review as positive or negative.

Example:
Review: "Great product, highly recommend!"
Answer: positive

Now classify:
Review: "{review}"
Answer:

Shows the exact response format. Sufficient for tasks with a clear pattern.

Few-shot (5)Multiple examples

Classify the review as positive or negative.

Examples:
Review: "Great product!"
Answer: positive

Review: "Terrible quality, waste of money"
Answer: negative

Review: "Fast delivery, everything works"
Answer: positive

Review: "Don't recommend, disappointed with purchase"
Answer: negative

Review: "Price/quality ratio is excellent"
Answer: positive

Now classify:
Review: "{review}"
Answer:

Ideal for complex tasks. Diverse examples cover more edge cases.

Usage tips:
  • Start with zero-shot, add examples only if results are imprecise
  • 3-5 examples are usually enough, more = more tokens without guaranteed better quality
  • Examples should be diverse and cover edge cases
  • Use consistent format for all examples (Input/Output, Question/Answer, etc.)
Key Insight

Few-shot examples work like "calibration" — they show the model the EXACT format you expect. 3-5 diverse examples are usually enough. More examples = more tokens, but not always better quality.

Frequently asked questions

How many examples should I use in few-shot prompting?

Typically 2-5 examples work best. Too few examples may not establish the pattern, while too many waste tokens and can confuse the model. Start with 3 diverse examples and adjust based on output quality.

When should I use few-shot instead of zero-shot prompting?

Use few-shot when: the output format is unusual or specific, the task requires domain-specific knowledge, zero-shot results are inconsistent, or you need the model to match a particular style or tone.

How do I choose good examples for few-shot prompts?

Pick diverse examples that cover edge cases, use consistent formatting across all examples, include both simple and complex cases, and order them from simple to complex. Avoid redundant examples that show the same pattern.

Does few-shot prompting work with all LLMs?

Yes, few-shot prompting works with all modern LLMs including GPT-4, Claude, Gemini, and open-source models. Larger models generally learn better from fewer examples, while smaller models may need more examples.

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskBeginnerWriting

Classify this review as positive, negative, or neutral: "The food was okay, but the service leaves much to be desired."

Without technique
Without technique
Prompt
Classify this review: "The food was okay, but the service leaves much to be desired."
Response

This is a mixed review. The customer notes both positive and negative aspects.

Tokens:35/28
Time:380ms
Quality:
With Few-Shot Learning
With technique
Prompt
Classify reviews as positive, negative, or neutral. Review: "Amazing place! Will definitely come back." Classification: positive Review: "Terrible experience. Would not recommend." Classification: negative Review: "It was fine, nothing special." Classification: neutral Review: "The food was okay, but the service leaves much to be desired." Classification:
Response

negative

👁️Examples show the answer format
🧠"okay" is weak positive, "leaves much to be desired" is negative
Overall tone leans negative
Tokens:125/5
Time:280ms
Quality:
Why this works

Few-shot examples showed the model the exact answer format (one word) and established a classification standard. Without examples, the model answers "in its own way".

1 / 5

Lesson Quiz

1 of 3

1.What is "in-context learning" in the context of few-shot prompting?

Practice Challenges

Create a free account to solve challenges

4 AI-verified challenges for this lesson

This lesson is part of a structured LLM course.

My Learning Path