Few-Shot Prompting
By the Syncfusion DevTutorials team · Last updated
Teach the model a pattern with examples: one-shot and few-shot, laying out examples, examples as chat turns, choosing varied and balanced examples, dynamic few-shot, and the cost of examples.
Some tasks are hard to explain in words but easy to show. "Write the tag in our format" means nothing to a model. But if you show it two tickets with their tags, it copies the pattern for the third ticket.
This is called few-shot prompting. You put a few worked examples in the prompt, and the model follows their pattern. It fixes most of the cases where zero-shot prompting fails.
The main idea: when you can't explain the format or style clearly, show it with 2 to 5 good examples.
A Real-World Example
Think about training a new team member to write release notes.
- You could write a long guide about the tone, the length, and the structure.
- Or you could give them three release notes your team already wrote, and say "Write the next one like these."
The second way is faster and works better. They copy the length, the tone, and the layout without you having to name each rule. Models learn from examples in the same way, all inside one prompt.
What You Will Learn
In this lesson, you will learn:
- What one-shot and few-shot prompting mean.
- How to lay out examples so the model can see the pattern.
- How to write few-shot examples as chat messages.
- How to choose good examples: varied, balanced, and close to the input.
- What examples cost, and how many to use.
- The common mistakes people make with few-shot prompts.
The "Try it" cards run in the Prompt sandbox at the bottom of the page, using a real model in your browser. Compare a zero-shot prompt with a few-shot prompt on the same input to see the difference.
One-Shot and Few-Shot
- One-shot means there is one example in the prompt.
- Few-shot means there are a few examples, usually 2 to 5.
Each example is a pair: an input, and the output you want for it. After the examples comes the new input, and the model writes the output for it.
The examples set a pattern of input and output. The new input follows the same pattern, so the most likely next text is an output in the same shape.
This works because the model has one job: guessing the next text. After three "Input, Output" pairs and then a fourth "Input", the most likely next text is an output that follows the same pattern.
Lay Out the Examples
Keep every example in exactly the same layout. End the prompt at the exact place where the model should start writing.
Build a few-shot prompt
const examples = [
{ input: "Export to Excel crashes with 10,000 rows.", output: "Grids > Export > Excel" },
{ input: "The date picker shows the wrong month.", output: "Calendars > Date Picker > Display" },
];
function fewShot(instruction, examples, newInput) {
const shots = examples.map((e) => "Ticket: " + e.input + "\nTag: " + e.output).join("\n\n");
return instruction + "\n\n" + shots + "\n\nTicket: " + newInput + "\nTag:";
}
console.log(fewShot("Tag each ticket in our format.", examples, "PDF export cuts off the last column."));
Expected output:
Tag each ticket in our format.
Ticket: Export to Excel crashes with 10,000 rows.
Tag: Grids > Export > Excel
Ticket: The date picker shows the wrong month.
Tag: Calendars > Date Picker > Display
Ticket: PDF export cuts off the last column.
Tag:
The prompt ends with Tag:. So the next text the model writes is the tag, following the pattern of the examples.
Try it. Prompt A is zero-shot. Prompt B shows two examples.
Prompt A has to guess what "our format" means. Prompt B shows the format, so the reply looks like Grids > Export > PDF.
Few-Shot as Chat Messages
With chat models, you can also write each example as a pair of messages: a user message with the input, and then an assistant message with the output. The model sees these as earlier turns in the chat, and it answers the last message in the same way.
Examples as chat turns
const examples = [
{ input: "Export to Excel crashes.", output: "Grids > Export > Excel" },
{ input: "Date picker shows the wrong month.", output: "Calendars > Date Picker > Display" },
];
const messages = [{ role: "system", content: "Tag each ticket in our format. Reply with the tag only." }];
for (const e of examples) {
messages.push({ role: "user", content: e.input });
messages.push({ role: "assistant", content: e.output });
}
messages.push({ role: "user", content: "PDF export cuts off the last column." });
messages.forEach((m) => console.log(m.role.padEnd(9), m.content));
Expected output:
system Tag each ticket in our format. Reply with the tag only.
user Export to Excel crashes.
assistant Grids > Export > Excel
user Date picker shows the wrong month.
assistant Calendars > Date Picker > Display
user PDF export cuts off the last column.
Both layouts work. Apps often use chat turns, because this keeps the examples separate from the instruction.
Easy Way to Remember: the model copies everything about your examples: the length, the tone, the layout, and even the punctuation. Write examples that look exactly like the reply you want.
Choose Good Examples
The examples matter more than the instruction. Good examples are:
- Varied. They cover different kinds of input, not three versions of the same input.
- Balanced. If you use labels, each label appears about the same number of times.
- Correct. A wrong example teaches the model a wrong pattern.
- Close to the real input. Examples that are like the new input help the most.
If your examples are not balanced, the model leans toward the label it saw most often. The next box checks the balance.
Check that examples are balanced
const examples = [
{ text: "Love it!", label: "positive" },
{ text: "Great support.", label: "positive" },
{ text: "Works well.", label: "positive" },
{ text: "It crashed.", label: "negative" },
];
const counts = {};
for (const e of examples) counts[e.label] = (counts[e.label] || 0) + 1;
const values = Object.values(counts);
const balanced = Math.max(...values) - Math.min(...values) <= 1;
console.log(JSON.stringify(counts), balanced ? "balanced" : "unbalanced");
Expected output:
{"positive":3,"negative":1} unbalanced
With three positive examples and only one negative, "positive" looks like the usual answer. Add a negative example, or remove a positive one.
Pick Examples Close to the Input
When you have many examples, you don't need to send all of them. Choose the ones most like the new input. This is called dynamic few-shot. Real apps use embeddings for this, but a simple word match shows the idea.
Pick the most similar examples
const pool = [
{ input: "Excel export crashes with big files.", output: "Grids > Export > Excel" },
{ input: "Chart legend overlaps the title.", output: "Charts > Legend > Layout" },
{ input: "CSV export adds an empty row.", output: "Grids > Export > CSV" },
{ input: "Date picker shows the wrong month.", output: "Calendars > Date Picker > Display" },
];
function overlap(a, b) {
const wa = new Set(a.toLowerCase().match(/[a-z]+/g));
return b.toLowerCase().match(/[a-z]+/g).filter((w) => w.length > 3 && wa.has(w)).length;
}
const newInput = "PDF export crashes on large files.";
const best = pool
.map((e) => ({ ...e, score: overlap(newInput, e.input) }))
.sort((a, b) => b.score - a.score)
.slice(0, 2);
best.forEach((e) => console.log(e.score, e.input));
Expected output:
3 Excel export crashes with big files.
1 CSV export adds an empty row.
The code picks the two export examples. The chart and calendar examples would not help with an export ticket.
How Many Examples?
Every example adds tokens. More tokens mean a slower reply, a higher cost, and less space left in the context window. The next box measures the cost.
Measure what examples cost
const example = "Ticket: Export to Excel crashes with 10,000 rows.\nTag: Grids > Export > Excel\n\n";
const tokensPerExample = Math.ceil(example.length / 4);
for (const n of [1, 3, 5, 10]) {
console.log(n + " examples: about " + n * tokensPerExample + " tokens");
}
Expected output:
1 examples: about 20 tokens
3 examples: about 60 tokens
5 examples: about 100 tokens
10 examples: about 200 tokens
Short examples cost very little. Long ones, like whole emails, add up fast. Here is a good rule:
- Start with 2 or 3 examples.
- Add more only if your test results get better.
- Stop when adding more examples does not raise the score.
Common Traps
- Copying the content. If every example is about Excel, the model may say "Excel" for a PDF ticket. Use varied examples.
- The pull of the last example. Models tend to give the same answer as the last example. Mix up the order of the labels.
- Wrong examples. The model copies mistakes just as closely as good answers. Check every example.
- Examples that go against the instruction. If the instruction says "one word" but the examples use three words, the model usually follows the examples.
Test a few-shot prompt like any other prompt. This card runs a prompt with two examples on four new tickets, and uses a regular expression to check the shape of each tag.
Never put private data, like real customer names or emails, into your examples. The examples are sent with every request, and the model may copy what is in them into its replies.
Common mistakes
| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Examples that are all alike | "Three Excel examples show the format well." | Use different topics and inputs. | The model writes "Excel" for every ticket. |
| Unbalanced labels | "Most of our reviews are positive anyway." | Use about the same number of examples for each label. | The model leans toward the most common label. |
| Mixed layouts | "Small changes in layout don't matter." | Keep every example in the same layout. | The replies keep switching between layouts. |
| Too many examples | "More examples always help." | Start with 2 or 3, and test before you add more. | Prompts become slow and cost more, but the results are no better. |
| A wrong example | "One small mistake is fine." | Check the output of every example. | The model copies the mistake. |
| Prompt doesn't end where the output starts | "The model knows where to answer." | End with the output label, like Tag:. | The model writes another ticket instead of the tag. |
Exercises
In each exercise, the last line should give back a value. Press Check to compare your value with the expected answer.
What to Remember
- One-shot has one example. Few-shot has a few, usually 2 to 5.
- Examples show a pattern of input and output, and the model continues that pattern.
- Keep every example in the same layout, and end the prompt where the output starts.
- Examples can also be chat turns: the user's input, then the assistant's output.
- Good examples are varied, balanced, correct, and close to the input.
- Examples cost tokens. Add more only when your test results get better.
Next: Role Prompting, where you use system prompts and roles to set who the model is and who it writes for.