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.
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.
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.
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.
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.
How Few-shot Prompting Works
A good few-shot prompt usually contains an instruction, several representative examples, and one new input.
Show two examples that transform raw text into the exact JSON shape you want.
Show how phrases map to labels such as BILLING_REFUND or ACCOUNT_LOCKED.
Few-shot vs Zero-shot
Few-shot is not automatically better. It trades a longer prompt for a clearer demonstration of the target behavior.
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:
Example 2: Structured Extraction
Few-shot examples can also demonstrate a strict output shape.
Text: "Ravi ordered 3 monitors."
Output: {"name":"Ravi","item":"monitor","quantity":3}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.
Choose Examples Carefully
The quality of the demonstration set matters more than simply adding more examples.
Three nearly identical examples
All examples show easy positive reviews. The model learns little about negative or ambiguous cases.
Representative coverage
Use clear positive, clear negative, and one realistic borderline case if that pattern exists in your data.
Run a Few-shot Experiment in Python
Compare a zero-shot baseline against a few-shot prompt on the same test messages.
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.
Order and Consistency Matter
Examples should use one stable pattern. Contradictory demonstrations make the prompt harder to follow.
Input: Great service Sentiment: positive Text: Terrible quality Answer = NEGATIVE
Different field names and casing create needless ambiguity.
Text: Great service Label: positive Text: Terrible quality Label: negative
The relationship is simple and repeated in the same structure.
How Many Examples Should You Use?
There is no universal magic number. Use the smallest set that gives stable gains on your evaluation data.
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.
Common Few-shot Mistakes
Examples do not represent real inputs.
Fix: choose from actual task patterns.One incorrect demonstration can steer future outputs badly.
Fix: review example quality.Prompt size grows without measurable benefit.
Fix: evaluate smaller sets.Examples show a pattern but the instruction stays vague.
Fix: use instruction + demonstrations together.Few-shot + Role + Context
Prompting techniques can be combined. Each part should solve a different problem.
Mini-Project: Few-shot Ticket Router
Build the few-shot version of the support router from the Zero-shot lesson and compare them fairly.
Few-shot Challenge
Create a prompt that maps short feedback messages to exactly one label: bug, feature_request, praise.
Classify the feedback as exactly one of:
bug, feature_request, praise.
Return only the label.
Feedback: "The export button freezes."
Label: bug
Feedback: "Please add dark mode."
Label: feature_request
Feedback: "The new dashboard is excellent."
Label: praise
Feedback: "It would be useful to filter reports by team."
Label: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.