Learning path: Prompt and Context Engineering · Step 3 of 8
Few-Shot Prompting Done Right: How Many Examples, How Different, What Order, and the Leak That Lies
Sort support tickets into billing, bug, feature or other by showing the model four worked examples: an easy case, a hard case, an edge case and a near miss. Then pick the count, the order and the shape, and keep the examples out of your test set.
What you will learn
- Replace a growing list of rules with a few worked examples
- Pick a defensible example count and make each example teach something new
- Order examples and match their shape to the real input
- Keep the example pool and the test set apart with an overlap check
Before you start
- What an LLM Can and Cannot Do
- Explicit Criteria vs Vague Prompts
Instead of writing a long list of rules, you can show the model a few worked examples and let it pick up the pattern. That is few-shot prompting. The idea is simple, but four things decide whether it works: how many examples you use, how different they are, what order and shape they take, and one leak that can make your test scores lie to you.
Telling vs showing
The use case: support tickets come in, and we want to sort each one into billing, bug, feature or other.
The first idea is to tell the model the rules:
Classify the ticket as billing, bug, feature or other.
If it mentions money, it is billing, unless the money part is a
broken screen, then it is a bug, unless it asks for a new way to
pay, then it is a feature, unless the message is almost empty...
The rules keep growing, and the prompt gets harder and harder to read. The other way is to show: give the model a couple of tickets with the correct label next to each one.
Classify the ticket. Reply with the label only.
Ticket: I was charged twice this month.
Label: billing
Ticket: Export spins forever, I need my invoice.
Label: bug
A few good examples can replace a whole page of rules. The model picks up the pattern from what it sees.
A driving instructor's practice routes
How does a driving instructor get a learner ready for any road? Practise one route and the learner only knows that one street. Practise three straight roads and it is the same lesson three times; the first roundabout is a shock. Practise in town, on the highway, at a roundabout and in the rain, and the learner gets the idea of driving itself.
And here is the catch. Hand the learner the exact exam route in advance and they pass the test, then struggle on new roads. The practice routes are your few-shot examples. The exam is your test set. Make the practice different, and keep the two apart. That is this whole article in one picture.
Dial one: how many examples
Every example costs prompt space on every single call, so pick a count you can defend.
| Count | What happens |
|---|---|
| One | The model tends to copy it too closely, even the parts that don't matter, like its length or its wording. |
| Three to five | A good place to start for most tasks. Enough to show a pattern without making the prompt huge. |
| Ten or more | A longer prompt on every call, and the gain is often small. |
The rule of thumb: start with three, and grow the count only when a test proves it helps. Since every example rides along on every call, it also adds to the token bill; LLM Tokens, Context Windows and Cost shows how that adds up.
Dial two: make them different
Each example should teach something the others don't. Here is the ticket prompt with four examples and the slot for the new ticket at the bottom:
Classify the ticket as billing, bug, feature or other.
Reply with the label only.
Ticket: I was charged twice this month.
Label: billing
Ticket: Export spins forever, I need my invoice.
Label: bug
Ticket: hi
Label: other
Ticket: Can you email me invoices every month?
Label: feature
Ticket: {ticket}
Label:
- Easy case. Charged twice is plainly billing.
- Hard case. It says "invoice", so it smells like billing, but it is really a broken export button, so it is a bug.
- Edge case. An almost empty message that just says "hi". That goes to other.
- Near miss. It sounds like billing, but it asks for a new feature: invoices by email every month.
The swap test: if you can swap two examples and nothing new is taught, they are too alike. Replace one.
Order and shape
Two small details change results more than you would expect.
Order
Start with the simple case so it sets the basic pattern. Then the harder ones: edge cases and near misses. Put the most typical example last, because the last example tends to pull the hardest.
Don't treat this as a law, though. Order is a setting to test: shuffle the examples, run your tests again, and keep the order that wins.
Shape
Here the examples use Q: and A: with the label in capitals, but the new ticket arrives as Ticket: and Label::
Q: I was charged twice.
A: BILLING
Ticket: {ticket}
Label:
The fix is one shape everywhere:
Ticket: I was charged twice.
Label: billing
Ticket: {ticket}
Label:
Make every example look exactly like the real input: same labels, same keys, same casing.
The leakage trap
Now the big one. Few-shot examples must never come from the set you test on.
Here is how it happens. You have one file with every labelled ticket. You paste the first five into the prompt as examples, then you test on the whole file. The model has already seen some of the answers.
The clean way is two pools. An example pool in its own folder, picked by hand, and a separate test set that is never shown in a prompt. Then an overlap check makes sure no example text appears in the test set. In practice: search the test set for every example text, and if any one turns up, move it out of the test set or pick a different example.
This is the same idea as the train-test split in machine learning: whatever the model learns from must stay out of what you grade it on. Train-Test Split and Data Leakage shows the same trap with a scaler.
Warning sign: test scores look too good in development, then drop with real users. Check for leakage first.
Common mistakes and the fix
| Mistake | What goes wrong | Fix |
|---|---|---|
| One example | The model copies its surface details | Start with three to five |
| Look-alike examples | Breaks on the first unusual input | Easy, hard, edge, near miss |
| A different shape | The real input looks unfamiliar | Same keys, labels and casing |
| Examples from the test set | Scores you cannot trust | Separate pools plus an overlap check |
Treat your examples like code: version them, test them, and change them one at a time. Prompt Versioning and Testing in a Repo shows how.
Try it yourself
- Pick a task and write a few-shot prompt with three look-alike examples.
- Replace two of them with a hard case and a near miss, and compare the results.
- Search your test set for every example text to check for leaks.
To sum up: show, don't only tell. Start with three to five examples that are clearly different. Put the simple case first, match the real input shape, and test the order. And keep your examples and your test set apart.
Related course on Udemy
Become an AI Engineer: LLMs, RAG, Agents and MCP
Build real AI systems in Python: LLM APIs, RAG, evals, agents and MCP.View on Udemy ↗: Become an AI Engineer: LLMs, RAG, Agents and MCP (paid course, opens in a new tab)Related posts
The AI Stack in One Picture: Six Layers Every AI Product Is Built From
Every AI product is built from six layers: data, model, retrieval, orchestration, serving and ops, with evals and security across all of them. A restaurant analogy, one request traced through all six, and what an AI engineer actually builds around the model call.
Tokenization: What an LLM Actually Sees When You Type
A language model never sees letters, only token ids. Use tiktoken to watch strawberry become three ids, see a space or a capital change them, fix the letter-counting problem, and compare English, German and Hindi token counts.
LLM Tokens, Context Windows and Cost: What One Question Really Costs
Follow one four-word help desk question that becomes 911 input tokens. Learn what fills the context window, why chat memory grows every turn, how max_tokens cuts a reply, and how to price a call.
What an LLM Can and Cannot Do: One Prediction Loop, Its Strengths and Its Gaps
A large language model predicts the next token, over and over. That one loop makes it strong at drafting, summarising and classifying, and weak at fresh facts, exact maths, memory and guaranteed truth. See why with a three-line tiktoken script, and learn the fix for each gap.