LESSON 22 ยท PROMPT ENGINEERING

Few-shot Prompting

Few-shot prompting teaches an LLM the pattern you want by showing a small number of input โ†’ output examples inside the prompt. It is especially useful when instructions alone are not enough to communicate labels, tone, structure, or edge-case behavior.

28 min readโ€ขBeginnerโ€ขGenerative AI

By the end of this lesson, you will be able to:

  • Explain the difference between zero-shot and few-shot prompting.
  • Choose useful demonstrations instead of random examples.
  • Use few-shot prompts for classification, extraction, and formatting.
  • Build and test a few-shot workflow with Python.
  • Recognize when examples add cost without improving quality.
1

What Is Few-shot Prompting?

Few-shot prompting means giving the model a small set of task-specific demonstrations before asking it to solve a new input.

ZERO-SHOT

Instruction only

Classify the review as positive or negative.

Review: "The delivery was fast."

The model must infer the desired mapping from the instruction alone.

FEW-SHOT

Instruction + examples

Review: "Amazing product." โ†’ positive
Review: "Stopped working." โ†’ negative

Review: "The delivery was fast." โ†’

The examples demonstrate the exact pattern before the new input.

Core idea:Examples are not extra decoration. They are demonstrations of the behavior you want the model to imitate.
2

How Few-shot Prompting Works

A good few-shot prompt usually contains an instruction, several representative examples, and one new input.

INSTRUCTIONDefine the taskClassify intent
+
EXAMPLESShow mappingsInput โ†’ label
+
NEW INPUTUnseen requestUser message
โ†’
OUTPUTFollow patternOne label
EXAMPLE AFormatting

Show two examples that transform raw text into the exact JSON shape you want.

EXAMPLE BInternal labels

Show how phrases map to labels such as BILLING_REFUND or ACCOUNT_LOCKED.

3

Few-shot vs Zero-shot

Few-shot is not automatically better. It trades a longer prompt for a clearer demonstration of the target behavior.

Zero-shotBest first choice when the task is simple and instructions are enough.
Few-shotUseful when the mapping, style, or format is difficult to explain precisely.
Prompt costEvery example consumes tokens and can increase latency and request cost.
EvaluationMeasure both approaches on the same unseen test inputs.
Practical rule:Start with zero-shot. Add examples only when they solve a measurable failure.
4

Example 1: Customer Support Classification

Internal labels are a strong use case because their meaning may not be obvious from the names alone.

Classify the customer message as exactly one of:
BILLING, TECHNICAL, SHIPPING, ACCOUNT.
Return only the label.

Message: "I was charged twice for the same order."
Label: BILLING

Message: "My password reset link has expired."
Label: ACCOUNT

Message: "The tracking page has not updated for four days."
Label: SHIPPING

Message: "The app crashes whenever I upload a photo."
Label:
Expected pattern:The demonstrations teach both the category mapping and the requirement to return one label only.
5

Example 2: Structured Extraction

Few-shot examples can also demonstrate a strict output shape.

DEMONSTRATION 1
Text: "Ravi ordered 3 monitors."
Output: {"name":"Ravi","item":"monitor","quantity":3}
DEMONSTRATION 2
Text: "Maya ordered 2 keyboards."
Output: {"name":"Maya","item":"keyboard","quantity":2}
Now extract the same fields.
Text: "Arun ordered 5 laptops."
Output:

The examples communicate field names, value types, and formatting. In production, still validate the returned JSON rather than trusting it blindly.

6

Choose Examples Carefully

The quality of the demonstration set matters more than simply adding more examples.

01 ยท REPRESENTATIVEMatch real user inputs
02 ยท DIVERSECover different cases
03 ยท CORRECTNo mislabeled examples
04 ยท CONSISTENTSame output pattern
05 ยท SHORTDo not waste tokens
WEAK SET

Three nearly identical examples

All examples show easy positive reviews. The model learns little about negative or ambiguous cases.

BETTER SET

Representative coverage

Use clear positive, clear negative, and one realistic borderline case if that pattern exists in your data.

7

Run a Few-shot Experiment in Python

Compare a zero-shot baseline against a few-shot prompt on the same test messages.

</> Python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

examples = """Message: I was charged twice.\nLabel: billing\n\nMessage: My password reset link expired.\nLabel: account\n\nMessage: Tracking has not moved for four days.\nLabel: shipping"""

message = "The checkout button throws an error every time."

prompt = f"""Classify the customer message as exactly one of:
billing, technical, shipping, account.
Return only the label.

{examples}

Message: {message}
Label:"""

response = client.responses.create(
    model="gpt-5-mini",
    input=prompt,
)

label = response.output_text.strip().lower()
allowed = {"billing", "technical", "shipping", "account"}

if label not in allowed:
    raise ValueError(f"Unexpected label: {label}")

print(label)

Important: Keep your API key in an environment variable. Few-shot examples improve guidance, but your application should still validate model output.

8

Order and Consistency Matter

Examples should use one stable pattern. Contradictory demonstrations make the prompt harder to follow.

INCONSISTENT
Input: Great service
Sentiment: positive

Text: Terrible quality
Answer = NEGATIVE

Different field names and casing create needless ambiguity.

CONSISTENT
Text: Great service
Label: positive

Text: Terrible quality
Label: negative

The relationship is simple and repeated in the same structure.

Engineering tip:Treat demonstrations like training data in miniature. Bad examples can teach bad behavior.
9

How Many Examples Should You Use?

There is no universal magic number. Use the smallest set that gives stable gains on your evaluation data.

1โ€“2 examplesUseful for showing a simple format or style.Example:Text โ†’ JSON field extraction.
3โ€“5 examplesOften enough to demonstrate several categories.Example:Support intent routing.
More examplesCan cover edge cases but increase prompt size.Risk:Higher cost and latency.
Dynamic examplesSelect only examples similar to the current input.Use:Larger production systems.

Do not add ten examples just because ten seems safer. If three examples produce the same accuracy, the extra seven are wasted context and money.

10

Common Few-shot Mistakes

1Random examples

Examples do not represent real inputs.

Fix: choose from actual task patterns.
2Wrong labels

One incorrect demonstration can steer future outputs badly.

Fix: review example quality.
3Too many examples

Prompt size grows without measurable benefit.

Fix: evaluate smaller sets.
4No output rule

Examples show a pattern but the instruction stays vague.

Fix: use instruction + demonstrations together.
11

Few-shot + Role + Context

Prompting techniques can be combined. Each part should solve a different problem.

ROLESupport triage agent
+
CONTEXTAllowed categories
+
FEW-SHOTExample mappings
+
NEW INPUTCustomer message
Do not over-prompt:If the examples already make the behavior obvious, adding long role descriptions and repeated rules may only consume context. Keep each component because it earns its place.
12

Mini-Project: Few-shot Ticket Router

Build the few-shot version of the support router from the Zero-shot lesson and compare them fairly.

1. Create labelsbilling, technical, shipping, account.
2. Add examplesChoose one strong example per major category.
3. Build a test setUse messages not shown in the prompt.
4. Run zero-shotRecord output and accuracy.
5. Run few-shotUse the same model and test inputs.
6. Compare trade-offsAccuracy, consistency, tokens, latency, and cost.
Success condition:Keep few-shot only if the demonstrations produce a useful improvement that justifies the extra prompt tokens.
PRACTICE

Few-shot Challenge

Create a prompt that maps short feedback messages to exactly one label: bug, feature_request, praise.

Write a clear instructionAdd 3 demonstrationsKeep format consistentAdd a new unseen message
QUICK QUIZ

Test yourself

What makes a prompt few-shot?

30-Second Recap

  • Few-shot prompting includes a small number of task-specific examples inside the prompt.
  • Examples can communicate label mappings, formatting, tone, and subtle behavior.
  • Choose representative, correct, diverse, and consistent demonstrations.
  • More examples are not automatically better because they consume context, tokens, and latency.
  • Validate model output even when demonstrations are strong.
  • Compare few-shot against a zero-shot baseline on the same unseen test set before using it in production.