Few-Shot Prompting
Few-Shot Prompting — A Clear, Step-by-Step Guide
Teach the model with a few small examples. Learn what to include, how many examples to use, and how to structure them.
1) What is Few-Shot?
Few-shot prompting means you show the model a small set of examples of the task (inputs with correct outputs) before asking it to solve a new, similar case.
2) Building Blocks (Simple Definitions)
Instruction: The main command for the task. Example: "Classify reviews by sentiment."
Example (shot): A pair of Input and Output that demonstrates the correct style/format.
Demonstration set: The small collection of examples you include (usually 2–5).
Constraints: Rules the model must follow (labels, word limits, “JSON only”, etc.).
Output format: The exact layout for the answer (often JSON).
New input (query): The fresh case the model must solve after seeing the examples.
3) A Simple Recipe
- Write a short instruction for the task.
- Add 2–5 small, varied examples (each with input and correct output).
- State constraints and output format.
- Provide the new input to solve.
- (Optional) Add acceptance checks or a role if needed.
4) Prompt Patterns
Instruction: Classify the sentiment as "positive", "negative", or "neutral". Return JSON only.
Examples:
Input: "The battery lasts all day."
Output: {"label":"positive"}
Input: "The camera is blurry and overheats."
Output: {"label":"negative"}
Input: "It works okay for now."
Output: {"label":"neutral"}
New Input:
"Delivery was late, but the product quality is excellent."
Instruction: Extract {"invoice_no":string, "amount_in_inr":number, "date":"YYYY-MM-DD"|null}. Return JSON only.
Examples:
Text: "Invoice INV-77 for ₹1250 was paid on 5 Jan 2025."
Output: {"invoice_no":"INV-77","amount_in_inr":1250,"date":"2025-01-05"}
Text: "Paid ₹900, invoice X-9. No date mentioned."
Output: {"invoice_no":"X-9","amount_in_inr":900,"date":null}
New Text:
"Please process payment of ₹1,499 for invoice AB-12, meeting on 14 Aug 2024."
Instruction: Rewrite to a polite, professional tone in ≤20 words. Return only the rewritten sentence.
Examples:
Original: "Send the file now."
Rewritten: "Please share the file as soon as possible."
Original: "You missed the deadline."
Rewritten: "Could you share an update on the deadline when you can?"
New Text:
"Why is this still pending?"
5) Fully Worked Examples
"Classify reviews. Here are many examples..." (100+ lines)
- Too many examples can exhaust context and add noise.
- Examples don’t show all labels or edge cases.
- No fixed output format (hard to check results).
Instruction: Classify as "positive", "negative", or "neutral". Return JSON only.
Examples:
Input: "Works like a charm."
Output: {"label":"positive"}
Input: "It keeps crashing."
Output: {"label":"negative"}
Input: "Quality is acceptable."
Output: {"label":"neutral"}
New Input:
"UI looks good, but the app still freezes at login."
Instruction: Categorize support tickets as {"category":"bug|feature|billing|other","priority":"low|medium|high"}. Return JSON only.
Examples:
Ticket: "Login fails when password has special characters."
Output: {"category":"bug","priority":"high"}
Ticket: "Please add dark mode in settings."
Output: {"category":"feature","priority":"medium"}
Ticket: "I was charged twice for the same month."
Output: {"category":"billing","priority":"high"}
New Ticket:
"Is there a way to export reports as CSV?"
6) Do / Don’t (with reasons)
| Do | Don’t | Reason |
|---|---|---|
| Use 2–5 examples that cover the space. | Include dozens of random samples. | Too many examples can confuse or exceed context. |
| Highlight edge cases in examples. | Repeat similar easy cases only. | Coverage teaches the model how to react to tricky inputs. |
| Keep outputs short and consistent. | Use different formats in each example. | Consistency makes it easy for the model to copy the pattern. |
7) Practice Tasks
- Create a few-shot prompt that categorizes IT incidents into Security, Performance, Usability, Other.
- Write a prompt with 3 examples that extracts name, email, and phone into JSON.
- Design a prompt that rewrites short updates into a cheerful tone using two examples.
8) Glossary
- Shot: An example pair of input and output.
- Demonstration set: The group of examples you provide.
- Context window: The token limit the model can read.
9) Summary
Few-shot prompting teaches the model by example. Include clear instructions, 2–5 varied examples, and keep outputs consistent. After the examples, provide your new input and let the model follow the demonstrated pattern.