All guides
BeginnerSelf-pacedFree GuideSection 11/50

Few-shot prompting — Learning by example

Explore generative AI through prompts, assistants, multimedia and response checking. Work through sections and exercises at your own pace.

💡

Free content, available without an account. Tools and models change: use the current model guide to check their versions. Models and sources

Section 10.2.3 : Few-shot prompting — Learning by example

🎯 Learning objective

Mastering few-shot prompting: guiding the model by providing concrete examples to obtain results that follow exactly the desired format, tone, and logic. This is the most powerful technique for consistency and customization.


Few-shot: teaching by example

Few-shot prompting involves providing a few examples (typically 2 to 5) before asking the actual question. The model learns the pattern from the examples and applies it to the new input. It's a form of in-context learning — the model isn't retrained, it understands the pattern from the examples.

Imagine you're onboarding an intern. Rather than explaining for 30 minutes how to write a meeting summary, you show them 3 exemplary summaries and say "Do the same". That's exactly what few-shot prompting does.

Structure of a few-shot prompt

The structure is always the same: instruction → examples → actual question.

[General instruction describing the task]

Example 1:
Input: [example input 1]
Output: [expected output 1]

Example 2:
Input: [example input 2]
Output: [expected output 2]

Example 3:
Input: [example input 3]
Output: [expected output 3]

Now:
Input: [your actual question]
Output:

Concrete few-shot examples by use case

1. Support ticket classification (format + categorization)

Classify each ticket into a category and urgency level.
Strict format: Category: [X] | Urgency: [High/Medium/Low]

Ticket: "I can't log in since this morning"
Category: Authentication | Urgency: High

Ticket: "Is it possible to export my data as CSV?"
Category: Feature | Urgency: Low

Ticket: "The app crashes when I open the reports tab"
Category: Bug | Urgency: High

Ticket: "How do I change the interface language?"
Category: User support | Urgency: Low

Ticket: "Your competitor offers a built-in chat feature, 
is this planned for your product?"
Category:

2. Product description generation (consistent tone + style)

Write an e-commerce product description in this exact style.
Each description: emoji + hook sentence + 3 features + 
closing sentence. Less than 50 words total.

Product: Bluetooth headphones
Description: 🎧 Dive into crystal-clear sound. 30h battery life, 
active noise cancellation, and ultra-light 180g design. 
Your everyday audio companion, from the office to the commute.

Product: Urban backpack
Description: 🎒 The city-dweller's ally. 15" laptop compartment, 
IPX4-certified waterproof fabric, and 12 organized pockets. 
From the coffee shop to the boardroom, it follows you everywhere.

Product: Smart thermos
Description: 🌡️ Your drink, always at the right temperature. 
LED temperature display, keeps hot 12h/cold 24h, 
500ml stainless steel. The companion for busy mornings.

Product: Sport smartwatch
Description:

3. Structured extraction (precise JSON format)

Extract named entities in the following JSON format.
Required fields: company, product, location, date.
If a field is absent, use null.

Text: "Apple announced the iPhone 16 in Cupertino on September 9."
JSON: {"company": "Apple", "product": "iPhone 16", 
"location": "Cupertino", "date": "September 9"}

Text: "Tesla will deliver the Cybertruck to European customers 
from Berlin in March 2025."
JSON: {"company": "Tesla", "product": "Cybertruck", 
"location": "Berlin", "date": "March 2025"}

Text: "Sanofi's new medication will be available 
in pharmacies in the first quarter."
JSON: {"company": "Sanofi", "product": "new medication", 
"location": null, "date": "first quarter"}

Text: "Google presented Gemini 3.1 at I/O in Mountain View 
on May 14."
JSON:

4. LinkedIn hook writing (personal style)

Rewrite this raw information as an engaging LinkedIn hook.
Style: punchy first line (hook), then 3-4 lines 
of context, then conclusion with a question.

Raw info: "I automated 40% of my tasks with AI"
LinkedIn post: 
I eliminated 3 hours of repetitive work per day.

Not by hiring someone.
Not by working less.
By using ChatGPT + Zapier on 5 key workflows.

The result: 40% of my tasks run on autopilot.
What's the next task you're going to automate?

Raw info: "Our startup went from 0 to €100K MRR in 8 months"
LinkedIn post:
€100K in monthly recurring revenue.

8 months ago, it was €0.
No fundraising. No paid ads.
Just a product that solves a real problem + word of mouth.

The 3 decisions that changed everything ⬇️
What's YOUR strategy to reach PMF?

Raw info: "I changed careers at 40 to join tech"
LinkedIn post:

How many examples to provide?

NumberNameWhen to useToken cost
1one-shotSimple format, familiar taskLow
2-3standard few-shotGood quality/cost balance for 80% of casesModerate
4-5rich few-shotComplex format, requires strong consistencyHigh
6+many-shotRarely useful — risk of overloading contextVery high

3 golden rules for choosing your examples

  1. →Diversity: your examples should cover the various cases of your task. For a 3-category classification, include at least one example of each.
  2. →Quality: mediocre examples produce mediocre results. Use your best real examples.
  3. →Representativeness: examples should resemble the real cases you'll process. Overly simple examples produce overly simple results.

Few-shot pitfalls to avoid

PitfallWhat happensSolution
Too similar examplesThe model thinks all cases are identicalVary examples (positive/negative, short/long)
Contradictory examplesThe model is confused about the patternCheck consistency between examples
Too many examplesConsumes tokens without improvement3 examples suffice in 80% of cases
Poorly formatted examplesThe model reproduces format errorsReview your examples carefully
Example orderThe model is biased by the last examplePut the most "normal" case last

Zero-shot vs Few-shot: the decision tree

Zero-shot vs Few-shot — Selection guide

Always start with zero-shot. If the result isn't satisfactory, switch to few-shot.
CriterionZero-shotFew-shot
Examples providedNone2-5 examples
Best forStandard tasks (summary, translation)Custom formats, unique styles
Creation speedVery fast (a few seconds)Slower (finding/creating examples)
Format consistencyVariableExcellent (reproduces pattern)
Token consumptionLowHigher (+30-50%)
Quality on custom task★★★☆☆★★★★★
Ideal whenTask is clear and standardSpecific format or unique style

← Scroll horizontally →

Practical exercise: building your own few-shots

Duration: 20 minutes

  1. →Choose a recurring task in your work (email writing, classification, meeting summary)
  2. →Write 3 high-quality examples (real input + ideal output)
  3. →Test the few-shot prompt on a new real case
  4. →Compare with the same case in zero-shot — note the difference
  5. →Save this prompt in your template library (cf. section 10.2.8)
Quick Check

What is 'in-context learning' in LLMs?

Quick Check

How many few-shot examples are recommended for most use cases?

Your progress

Progress is self-reported and stored in your browser. The final completion record is not a qualification or an independent assessment.

View progress and completion record

Learn AI, from prompts to agents

15 free guidesQuizzes, diagrams and workshopsNo account needed

One practical AI workflow in your inbox

Methods, resources and new exercises. Free, unsubscribe at any time.