Back

Prompt Engineering Courses · Lesson PE.4

Giving Examples (Few-Shot)

CODE SCOPE PROMPT ENGINEERING SERIESLESSON PE.4 OF PE.1–PE.12

Show, don't just tell: two or three examples teach the AI your exact pattern better than a long explanation.

Free PDF of this lesson — save it, print it, revise offline. The video is the same lesson on the Code Scope YouTube channel.

Introduction

Imagine you want the AI to turn messy shop items into neat product lines like this: "Red Cotton Shirt — Rs 850 — Medium". You could describe the format in a paragraph. Or you could show two examples and say "do the rest like this". The examples win almost every time, because they show the pattern instead of describing it.

This technique is called few-shot prompting: "few" because you give a few examples, "shot" because each example is one try the AI learns from. Two or three good examples usually beat ten lines of explanation — for formats, styles, classifications, translations and anything with a pattern.

(Roman Urdu: AI ko tareeqa samjhana ho to lambi wazahat ki bajaye 2-3 misaalain dikhao. Misaal pattern sikhati hai, wazahat sirf lafz deti hai.)

Key points

  • Examples show the pattern; descriptions describe it. For formats, tone and style, showing is stronger than telling.
  • Two or three examples are enough. One example can be a coincidence; five or more waste space and can confuse. Three is the sweet spot for most tasks.
  • Make examples match the real task. If the real items are shop products, your examples must be shop products — not book titles. The AI copies the shape AND the subject.
  • Keep examples short and consistent. Same structure every time, same labels, same order. Inconsistent examples teach an inconsistent pattern.
  • Put examples BEFORE the real task. Order: instruction, then examples, then "Now do the same for:" with your real input. The AI follows the last pattern it saw.
  • Examples also teach what NOT to do. One "wrong" example with a correction ("Do not do it like this") can prevent a repeated mistake — use sparingly, one is enough.
  • Examples do not replace the instruction. Keep a one-line task ("Convert each item to this format:") above the examples, so the AI knows what the examples are examples of.
  • Beginners get stuck on what counts as a good example. A good example is a solved version of the EXACT job: same input shape, same output shape, same labels. If your example solves a different job, it teaches a different pattern.
  • Cover the edges, not just the easy case. If your task has a tricky case (a negative review, a missing size, a long name), include one example of it. The AI copies the easy pattern happily; it is the edge case that breaks without an example.

Examples

Example 1 — Product lines in your format

Convert each shop item below into this format: Name — Price — Size.
Examples:
Cotton shirt red, 850, M -> Red Cotton Shirt — Rs 850 — Medium
Blue jeans, 1200, L -> Blue Jeans — Rs 1200 — Large
Now do the same for:
Green kurta, 950, S
Black shoes, 2200, 42

What you will get: "Green Kurta — Rs 950 — Small" and "Black Shoes — Rs 2200 — 42", in exactly your format, including the "Rs" you only showed, never asked for. That is the power of examples: the AI copies details you did not even mention.

Example 2 — Classifying comments

Classify each customer message as Happy, Angry, or Question.
Examples:
"Great quality, will order again!" -> Happy
"My parcel is 5 days late!" -> Angry
"Do you deliver to Multan?" -> Question
Now classify:
"The colour is different from the photo."
"Thanks for the fast delivery!"
"Is this shirt available in XL?"

What you will get: Angry, Happy, Question — in that order. Without examples, the AI might invent labels like "Complaint" or "Positive" and break your system. Examples lock the labels down.

Example 3 — Tone by example

Rewrite each sentence in a polite, formal tone.
Examples:
"Send me the price." -> "Could you please share the price?"
"I want it tomorrow." -> "I would appreciate delivery by tomorrow, if possible."
Now rewrite:
"Give me a discount."

What you will get: something like "Could you please offer a discount?" — polite and formal, because the examples showed what polite and formal mean in practice, not just as words.

Example 4 — Before and after: sorting homework headings

Without examples:

Make headings for my notes.

With examples:

Turn each rough heading into a clean heading like this:
Examples:
photosynthsis procs -> Photosynthesis: How Plants Make Food
water cycl stps -> The Water Cycle: Steps
Now do the same for:
cell divisn types
human heart parts

Without examples the AI invents its own heading style each time. With two examples it copies your style: capital letters, colon, plain subtitle. That is the whole point — your examples are your style guide.

Example 5 — Before and after: yes/no answers in your exact words

Answer each question with only YES or NO, then one short reason.
Examples:
"Is Lahore the capital of Pakistan?" -> NO — Islamabad is the capital.
"Is water made of hydrogen and oxygen?" -> YES — H2O.
Now answer:
"Is the sun a star?"
"Do fish breathe with lungs?"

Without the examples, the AI may write a paragraph for each question. With them, it knows the answer must be one word plus one reason — no more, no less.

Example 6 — One wrong example that prevents a mistake

Rewrite each shop message politely. Do NOT add a discount offer unless the customer asked for one.
Example of what NOT to do:
"Give me the price." -> "Here is the price, and here is a 20% discount!" (WRONG — nobody asked for a discount.)
Correct example:
"Give me the price." -> "Could you please confirm which item? I will share its price right away."
Now rewrite:
"Send it tomorrow."

One labelled wrong example stops a mistake that a long rule often does not. Use it only for the one mistake that keeps repeating.

Real-world case — Where this is actually used

Online shops use few-shot prompts every day to sort customer messages: a handful of labelled examples (Happy, Angry, Question) turn hundreds of incoming messages into sorted piles, so angry customers get answered first. Teachers use the same pattern to mark short answers: three marked examples teach the AI their marking style, and the rest of the class gets marked in the same style. In both cases the examples, not a long rulebook, carry the standard.

Quick recap

  • Few-shot = a few examples that teach the pattern.
  • Two or three examples, short and consistent, matching the real task.
  • Order: instruction, examples, then the real input.
  • Examples lock down format, labels and tone better than descriptions.
  • One "wrong" example can prevent a repeated mistake.

Common mistakes

  1. One example only — the AI may copy its surface details instead of the pattern. Fix: give two or three, with variety.
  2. Examples that do not match the task — book examples for shop items teach the wrong subject. Fix: make examples from the same kind of data.
  3. Inconsistent examples — different orders, different labels, different punctuation. Fix: keep every example in exactly the same shape.
  1. Examples that are all the same — three examples of happy customers teach nothing about angry ones. Fix: vary your examples so each important case appears at least once.
  2. Hiding the examples at the end — examples after the real task get copied less reliably, because the AI follows the last pattern it saw. Fix: always put examples before "Now do the same for:" and your real input.

Practice

  1. Pick a format you use often (a WhatsApp product message, a homework heading, a bill line). Write 3 examples in that format, then give the AI 3 new items and check it copies the format exactly.
  2. Take a classification task (good/bad reviews, easy/hard questions) and write examples for each label. Test whether the AI keeps your labels or invents new ones.
  1. Pick a repeating job (renaming files, formatting names, sorting messages). Write 3 examples, then test the AI on 5 new items. How many came back in exactly your format?
  2. Take a prompt where the AI invented its own labels. Add 2 examples with your labels and re-run it. Did it keep your labels this time?
  3. Write a prompt with one correct example and one labelled wrong example for a mistake you keep seeing. Test whether the wrong example actually stops the mistake.

Practice quiz

Tap an answer to check it — a right answer turns green, a wrong one turns red and shows the right answer. Nothing is scored or saved; this is just for practice.

1. What is few-shot prompting?

2. How many examples are usually enough?

3. Where do the examples go in the prompt?