Prompt Engineering — talk to AI like a pro — 2. Few-shot: learning by example

17 min read min de lecture
Chapter 02

Few-shot: learning by example

Chapter 2 of 10 · 20%

Chapter objectives

  • Understand the power of examples
  • Choose the right number and the right type of examples
  • Show a pattern rather than describe it

Show, don’t describe

Sofia spends twenty minutes describing her brand's tone: "dynamic but not aggressive, accessible but credible, warm but professional"... The result never looks like what she imagines. No wonder: those adjectives are subjective, and the model has an average interpretation of them that isn't hers.

The solution comes down to one principle: show, don't describe. Rather than explaining a style, give 2-3 input → output pairs that illustrate it. The model is a machine for spotting and extending patterns: concrete examples convey the format, tone, level of detail and implicit choices far better than any abstract instruction. This is called few-shot prompting: "few shots" = a few examples.

PROMPT
Turn features into benefits. Examples:
Feature: "5000 mAh battery" → Benefit: "2 days without recharging"
Feature: "end-to-end encryption" → Benefit: "nobody reads your messages, not even us"
Now: "8-core processor" →

Notice what the examples convey without it being written anywhere: the target length (short), the register (spoken, concrete), the structure (outcome for the user, not technical feat), and even the "not even us" trick that sets the tone. Describing all that would have taken a paragraph — and would have worked less well.

Zero-shot, one-shot, few-shot: three regimes

The vocabulary is simple. Zero-shot: you ask without any example — it's the default regime, sufficient for common tasks the model knows well. One-shot: a single example, useful for anchoring a simple format. Few-shot: 2 to 5 examples, the regime of choice as soon as the format or tone is specific to your use case.

Zero-shotNo examples. Quick to write, perfect for generic tasks (summarizing, translating, explaining). Risk: the model applies ITS default format, not yours.
One-shotOne example. Anchors a simple format (a headline template, a card structure). Risk: the model over-imitates the single example, down to its accidental details.
Few-shot (2-5)Several varied examples. The model generalizes the pattern instead of copying a case. The best effort/quality ratio for repetitive formats and brand tones.

Few-shot has a cost: your examples consume space in the context window and writing time. Good practice is therefore progressive: start zero-shot; if the output is fine, no need to do more. If the format or tone drifts, add examples. It's an escalation, not a systematic reflex.

How many examples? The 2-to-5 rule

2 to 5 examples are enough in the vast majority of cases. Below 2, the model can't distinguish the pattern from the accident (is this output short because it MUST be short, or by chance?). Beyond 5, the gains become marginal: you eat context and time with no measurable improvement.

More important than the number: diversity. Choose examples that cover the different cases the task will encounter — an easy case, a tricky case, an edge case — rather than five variations of the same comfortable case. If Sofia only shows technical smartphone features, the model will stumble on "2-year warranty". One non-technical example in the mix solves the problem in advance.

Also think about order: the model gives slightly more weight to the last examples. Put the one most similar to the inputs you're going to process last. And keep a strictly identical format from one example to the next: same separator, same punctuation, same casing.

Pattern consistency: your implicit contract

Your examples form an implicit contract: everything constant across your examples will be interpreted as a rule; everything that varies will be interpreted as free. If two of your examples end with a period and the third doesn't, you're sending a contradictory signal — and the output will be inconsistent in turn.

This is the number one cause of failed few-shots: not too few examples, but examples that contradict each other on a detail you didn't notice. Before blaming the model, reread your examples like a proofreader: comparable lengths? identical structure? consistent tone? same grammatical person? The devil is in those details.

If the output doesn't follow the pattern, it's often because your examples aren't consistent enough with each other. Reread them asking yourself: what is constant? what varies? The model asks itself exactly those questions.
flowchart LR
  E1["Example 1: input → output"] --> P["Pattern spotted: format, tone, length"]
  E2["Example 2: input → output"] --> P
  E3["Example 3: edge case → output"] --> P
  P --> N["New input"]
  N --> S["Output that follows the pattern"]
  E2 -.->|"Inconsistent example"| X["Blurred pattern → unpredictable output"]
Few-shot: consistent examples draw a pattern; a single discordant example blurs it.

Before/after: Sofia's brand tone

Descriptive version (the one that fails):

PROMPT
Rewrite this product description with a dynamic but not aggressive, accessible but credible tone: "Our software optimizes your teams' schedule management."

The result systematically falls back into generic marketing: "Boost your teams with an innovative solution...". Few-shot version (the one that works):

PROMPT
Rewrite product descriptions in our brand tone. Examples:

Before: "Our HR module automates leave tracking."
After: "Leave requests? Approved in 30 seconds, no spreadsheet, no lost email."

Before: "Our dashboard centralizes your metrics."
After: "All your numbers in one place. You open it, you know, you decide."

Now:
Before: "Our software optimizes your teams' schedule management."
After:

Why it works: the examples show the short sentences, the three-beat rhythm, the direct address to the reader, the concrete quantified benefit — everything that "dynamic but not aggressive" failed to convey. Sofia never described her tone again: she shows it.

When few-shot shines (and when it fails)

Few-shot excels on tasks with a precise, repetitive format: rephrasing product descriptions, classifying messages into categories, extracting information into a fixed template, generating homogeneous variants, imitating a brand tone. Wherever you can say "do it like this", it beats the abstract instruction.

It is however not very useful, even counterproductive, elsewhere. On complex reasoning tasks, examples of final answers teach nothing about the path to get there (we'll see better in chapter 3). On open creative tasks, too many examples lock the model into your cases and impoverish variety. And on tasks the model already masters perfectly, it's wasted effort.

Build yourself a small stock of "canonical" examples per recurring task: your 3 best product cards, your 3 best posts. This stock becomes the raw material of your few-shot prompts — and later of your templates.
🛠️ Your turn

Context

Sofia has to turn a list of 15 product features into customer benefits for the new website. The first three zero-shot attempts give wordings that are correct but inconsistent: some long, some short, with tones varying from one line to the next. Impossible to put them side by side on the same page. Yet she has two old product cards whose style she loves — exactly the raw material needed for a few-shot.

Instructions

  1. Pick a repetitive task from your daily work (descriptions, headlines, template replies).
  2. Select 2-3 existing outputs you're truly satisfied with: these will be your examples.
  3. Format them as strictly identical input → output pairs (same separator, same punctuation).
  4. Check their diversity: add an edge case or atypical case if all your examples look alike.
  5. Add the new input to process and run the prompt.
  6. Compare with the zero-shot version: note what the examples conveyed without any explicit instruction.
  7. If the output drifts, look for the inconsistency between your examples before rewriting everything.
Hint — Varied examples covering the edge cases beat repetitive examples. And put the example closest to the inputs you'll process last.

In summary

  • Showing 2-3 examples beats describing a style: the model extends patterns better than it interprets adjectives.
  • Zero-shot for generic tasks, few-shot as soon as the format or tone is specific: escalate progressively.
  • 2 to 5 examples are enough; beyond that, you consume context with no measurable gain.
  • Example diversity matters more than quantity: cover the edge cases.
  • Everything constant across your examples becomes an implicit rule; any inconsistency blurs the pattern.
  • Put the most representative example last: it carries a bit more weight.
  • Few-shot shines on repetitive, format-heavy work; it is useless on the trivial and limiting on open creative work.

Quiz — check your understanding

1. How many examples should you aim for in few-shot?

A few varied examples are enough; too many examples waste context with no measurable gain.

2. Why might the output not follow the pattern?

Inconsistent examples (length, punctuation, tone) blur the pattern to imitate. Reread them before blaming the model.

3. What is zero-shot?

Zero-shot = no example provided. It's the default regime, sufficient for generic tasks.

4. What do examples convey that an abstract instruction conveys poorly?

"Dynamic but not aggressive" can be interpreted in infinite ways; two well-chosen examples cannot.

5. On which type of task is few-shot the least useful?

On open creative work, too many examples lock the model into your cases and reduce the variety of proposals.

Auteur(s)

R

REHOUMA Haythem

Haythem Rehouma est un ingénieur et architecte IA et cloud, formateur et enseignant technique, avec un profil orienté IA médicale, AWS, MLOps, LLM/RAG et vision par ordinateur.