prompt-engineering

Few-shot Learning

Learn what Few-shot Learning means in AI and machine learning, with examples and related concepts.

Definition

Few-shot learning is a prompt engineering technique where you include a few examples of the desired input-output behavior in your prompt, so the model learns the pattern and applies it to new inputs.

Instead of writing detailed instructions like “classify the sentiment, output only ‘positive’ or ‘negative’, consider sarcasm…”, you just show the model a few examples and it figures out the pattern:

Review: "This product changed my life!" → positive
Review: "Total waste of money." → negative
Review: "It's okay, nothing special." → neutral
Review: "Best purchase I've ever made!" → ???

The model sees the pattern and outputs “positive.” No explicit instructions needed.

This works because LLMs are powerful pattern matchers — give them examples and they generalize. It’s called “in-context learning” because the model learns the task from examples within the prompt context, not from additional training.

How It Works

Terminology:

Zero-shot  → No examples, just instructions
  "Classify this review as positive or negative: 'Great product!'"

One-shot   → One example
  "Example: 'Terrible quality' → negative
   Now classify: 'Great product!'"

Few-shot   → 2-10 examples
  "Example 1: 'Terrible quality' → negative
   Example 2: 'Love it!' → positive
   Example 3: 'It works fine' → neutral
   Now classify: 'Great product!'"

Many-shot  → 10-100+ examples (leveraging large context windows)

Why it works: the examples activate relevant patterns the model learned during pre-training. The model effectively recognizes “this looks like a classification task” and applies its general knowledge of sentiment to the new input.

When to Use Each Approach

ApproachBest ForAccuracy
Zero-shotSimple, well-known tasksGood
Few-shot (2-5 examples)Custom formats, edge cases, ambiguous tasksBetter
Many-shot (10-50 examples)Complex classification, domain-specific tasksBest

Why It Matters

Few-shot learning is often the first technique developers try when zero-shot prompting doesn’t produce consistent results. It’s fast, cheap, and surprisingly effective.

Example

from anthropic import Anthropic

client = Anthropic()

# Few-shot classification: categorize support tickets
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=50,
    messages=[{
        "role": "user",
        "content": """Categorize each support ticket. Output only the category.

Ticket: "I can't log in, it says my password is wrong" → account-access
Ticket: "How do I export my data as CSV?" → feature-question
Ticket: "The app crashes when I upload files over 10MB" → bug-report
Ticket: "I want to cancel my subscription" → billing
Ticket: "Your API returns 500 errors every morning around 3am" → """
    }]
)

print(response.content[0].text)
# → "bug-report"
# Few-shot for structured extraction
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=100,
    messages=[{
        "role": "user",
        "content": """Extract company info as JSON.

"Stripe was founded in 2010 by Patrick and John Collison in San Francisco."
→ {"company": "Stripe", "founded": 2010, "founders": ["Patrick Collison", "John Collison"], "hq": "San Francisco"}

"Anthropic was founded in 2021 by Dario and Daniela Amodei. It's based in San Francisco."
→ {"company": "Anthropic", "founded": 2021, "founders": ["Dario Amodei", "Daniela Amodei"], "hq": "San Francisco"}

"DeepMind was started by Demis Hassabis, Shane Legg, and Mustafa Suleyman in London in 2010."
→ """
    }]
)

print(response.content[0].text)
# → {"company": "DeepMind", "founded": 2010, "founders": ["Demis Hassabis", "Shane Legg", "Mustafa Suleyman"], "hq": "London"}
# Few-shot for code transformation
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=100,
    messages=[{
        "role": "user",
        "content": """Convert JavaScript to Python.

JS: const greeting = `Hello, ${name}!`;
PY: greeting = f"Hello, {name}!"

JS: const items = arr.filter(x => x > 5).map(x => x * 2);
PY: items = [x * 2 for x in arr if x > 5]

JS: const { name, age, ...rest } = person;
PY: """
    }]
)
print(response.content[0].text)
# → name, age = person["name"], person["age"]; rest = {k: v for k, v in person.items() if k not in ("name", "age")}

Tips for Better Few-shot Prompts

  1. Diverse examples — Cover different cases, not just easy ones
  2. Consistent format — Use the exact same input→output format in every example
  3. Include edge cases — If sarcasm is tricky, include a sarcastic example
  4. Order matters — Put the most relevant examples closest to the actual query
  5. 3-5 examples is usually enough — More doesn’t always help (unless using many-shot)

Key Takeaways


Part of the DeepRaft Glossary — AI and ML terms explained for developers.