Few-Shot Prompting with Examples

Reviewed & published by Brayan K

Sometimes the fastest way to get exactly the output you want is to show the AI a few examples. That’s few-shot prompting: you give a handful of input → output pairs, then your real request, and the model copies the pattern.

Part of the free Prompt Engineering course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

When you give no examples at all — just an instruction — that’s zero-shot. Both are useful. This lesson shows when to use each and how to write examples that work.

What You'll Learn in This Lesson

1 Zero-shot vs few-shot

Zero-shot gives the AI just an instruction. Few-shot first shows it a few worked examples so it can copy the exact pattern, style, or labels you want.

StyleWhat you give
0️⃣ Zero-shotJust the instruction, no examples.
📎 Few-shotA few input → output examples, then your request.

Rule of thumb: start zero-shot. If the format or style isn’t right, add two to five clear examples and try again.

2 Examples in action

Classifying reviews. The zero-shot prompt may format the answer however it likes; the few-shot one locks in your exact labels:

Works, but the wording of the answer is unpredictable.

The examples fix the exact label format you want.

Try it for real

The template above is the whole trick, so here it is as code. A few-shot prompt is a string you assemble from input → output pairs; build it yourself and you can see exactly what changes when you add, remove, or reorder examples. Nothing here calls a model — this is the prompt you would send, plus the one check worth running on it before you do.

# A few-shot prompt is nothing more than a string you ASSEMBLE from examples.
# Build it in code and you can see exactly what the model receives - and how the
# number and order of examples changes it. No model calls: this is the prompt
# side of the conversation only.

examples = [
    ("Loved it, five stars", "Positive"),
    ("Too slow and it broke", "Negative"),
    ("Great value for money", "Positive"),
    ("Never buying this again", "Negative"),
]

def build_prompt(examples, query, k):
    """Assemble a prompt from the first k examples, then the real question."""
    lines = ["Classify each review as Positive or Negative."]
    for text, label in examples[:k]:            # k = 0 gives a zero-shot prompt
        lines.append(f"Review: '{text}' -> {label}")
    lines.append(f"Review: '{query}' -> ?")     # the model's job is to fill the ?
    return "\n".join(lines)

query = "Works fine but the box was crushed"

# 1) Zero-shot: instruction only.
print("=== zero-shot (k=0) ===")
print(build_prompt(examples, query, k=0))

# 2) Few-shot: the same call with three examples in front of the question.
print()
print("=== few-shot (k=3) ===")
print(build_prompt(examples, query, k=3))

# 3) Order is part of the prompt. Reverse the list and the example nearest the
#    question changes from Positive to Negative - a different prompt, same data.
print()
print("=== few-shot, reversed order (k=3) ===")
print(build_prompt(examples[::-1], query, k=3))

# 4) Count matters too: every example you add is text the model must read.
print()
for k in (0, 2, 4):
    print(f"k={k}: prompt is {len(build_prompt(examples, query, k))} characters")

# 5) Consistency check. The model copies whatever pattern you show, so two
#    spellings of one label is a bug in the examples, not in the model.
messy = examples + [("Decent enough", "positive"), ("Broke in a week", "NEG")]
print()
print("labels in clean set:", sorted({label for _, label in examples}))
print("labels in messy set:", sorted({label for _, label in messy}))

# Run it. The first lines you should see are below; the rest follows.

# ✅ Expected output:
# === zero-shot (k=0) ===
# Classify each review as Positive or Negative.
# Review: 'Works fine but the box was crushed' -> ?
# 🎯 YOUR TURN — assemble a few-shot prompt, then check the labels are consistent.

examples = [
    ("Loved it", "Positive"),
    ("Too slow", "Negative"),
    ("Great value", "Positive"),
]

def build_prompt(examples, query, k):
    lines = ["Classify each review as Positive or Negative."]
    # 1) Only the first k examples go into the prompt.
    for text, label in examples[:___]:
        # 👉 replace ___ with  k
        # 2) Each example line shows the input AND its ideal output.
        lines.append(f"Review: '{text}' -> {___}")
        # 👉 replace ___ with  label
    lines.append(f"Review: '{query}' -> ?")
    return "\n".join(lines)

print(build_prompt(examples, "Arrived broken", k=2))

# 3) Collect every distinct label. A consistent set has exactly two spellings here.
labels = sorted({label for _, label in ___})
# 👉 replace ___ with  examples
print("labels:", labels)
print("consistent:", len(labels) == 2)

# ✅ Expected output:
# Classify each review as Positive or Negative.
# Review: 'Loved it' -> Positive
# Review: 'Too slow' -> Negative
# Review: 'Arrived broken' -> ?
# labels: ['Negative', 'Positive']
# consistent: True

Common Mistakes (and the fix)

⏱ Test Yourself — Timed Quiz

10 quick questions, 12 seconds each. Instant feedback — beat the clock!

🎉 Lesson Complete

Practice quiz

What is 'few-shot' prompting?

  • Asking many questions at once
  • Giving the AI a few input-output examples before your real request
  • Using few words
  • Taking a few screenshots

Answer: Giving the AI a few input-output examples before your real request. Few-shot means showing example pairs so the AI copies the pattern.

What is 'zero-shot' prompting?

  • Asking with no examples, just the instruction
  • Asking zero questions
  • A failed prompt
  • A prompt with zero words

Answer: Asking with no examples, just the instruction. Zero-shot gives only the instruction, with no examples.

Why do examples help the AI?

  • They show the exact pattern, style, or format you want
  • They slow it down
  • They are decorative
  • They reset the chat

Answer: They show the exact pattern, style, or format you want. Examples demonstrate the desired output so the AI can mimic it.

A good few-shot example shows…

  • An input and the matching ideal output
  • Only an input
  • Only an output
  • A random sentence

Answer: An input and the matching ideal output. Pairing input with its ideal output teaches the mapping you want.

Few-shot is especially useful when…

  • The task has a specific format or style hard to describe in words
  • You want a one-word answer
  • You have no examples
  • You want randomness

Answer: The task has a specific format or style hard to describe in words. When style or format is tricky to explain, examples do the explaining.

How many examples does 'few-shot' usually mean?

  • Zero
  • A small handful, often two to five
  • Hundreds
  • Exactly one thousand

Answer: A small handful, often two to five. A few clear examples, typically two to five, is enough.

If your examples are inconsistent, the AI will…

  • Ignore them perfectly
  • Likely produce inconsistent output too
  • Always be correct
  • Crash

Answer: Likely produce inconsistent output too. The model copies your pattern, so mixed examples give mixed results.

Which is a few-shot prompt?

  • Classify this review.
  • Review: 'Loved it' -> Positive. Review: 'Too slow' -> Negative. Review: 'Great value' -> ?
  • act as a critic
  • summarize

Answer: Review: 'Loved it' -> Positive. Review: 'Too slow' -> Negative. Review: 'Great value' -> ?. It provides labeled examples before the new item to classify.

Zero-shot is a fine choice when…

  • The task is simple and the format is obvious
  • You need a very specific custom format
  • Examples would confuse
  • Never

Answer: The task is simple and the format is obvious. Simple, common tasks often work with no examples at all.

The main benefit of few-shot prompting is…

  • More randomness
  • More consistent, on-pattern answers that match your examples
  • Shorter prompts only
  • It removes the need for a task

Answer: More consistent, on-pattern answers that match your examples. Examples lock in the style and format for consistent results.

Continue this course

Frequently asked questions

What is the difference between zero-shot and few-shot prompting?

Zero-shot means you give the AI only an instruction with no examples, like 'Classify this review as positive or negative'. Few-shot means you first show a few worked examples, pairs of input and the ideal output, before your real request. Zero-shot is quick for simple tasks; few-shot shines when you need a specific style or format the AI should copy.

Why do examples make such a difference?

An example shows the AI the exact pattern you want far more clearly than a description can. If you label two or three sample reviews as Positive or Negative and then give a new one, the model sees the mapping and follows it. Examples are especially powerful for formatting, tone, and classification tasks that are hard to put into words.

How many examples should I include?

Usually a small handful, often two to five, is plenty. The goal is to make the pattern unmistakable without bloating the prompt. Make sure your examples are consistent with each other, because the model copies whatever pattern they show. Inconsistent examples lead to inconsistent answers.

When should I just use zero-shot?

Use zero-shot when the task is simple and the expected format is obvious, such as 'Summarize this paragraph in one sentence'. Reach for few-shot when the output needs a particular structure, label set, or voice that is easier to demonstrate than to describe. Start simple and add examples only if the zero-shot result is not on target.