Show Examples
How few-shot prompting uses worked examples to fix a model's output format and judgment, how to choose good examples, and where examples fail.
On this page
Few-shot prompting means including a few worked examples of the task in your prompt, each with an input and the output you want, before the real input. Prompts with no examples are called zero-shot. Examples are the most direct way to control the output format, the tone and the way borderline cases are judged, because those are easier to show than to describe.
Without examples, the model picks its own format and its own labels. The replies can all be reasonable and still be unusable by a program or a spreadsheet that expects one consistent shape.
How examples steer the model
A model continues text in the most likely way (What Is an LLM). After three input and output pairs, followed by a fourth input with the output left blank, the most likely continuation is an output that matches the first three. One example therefore shows the format, the length, the vocabulary and how a borderline case is handled, without a single extra rule.
The same task with and without examples
Suppose you want to tag app store reviews so that you can count them in a spreadsheet. This is the zero-shot version.
Each line is reasonable, but none of them fits a spreadsheet. The order of the fields changes, the topics are invented as the model goes, one line has two topics, and one line has commentary. The next version adds three examples.
Every line now has the same format, including details that nobody wrote down as rules: lowercase text, hyphens in multi-word topics and one topic per review. The mixed example also showed the model how to handle a review that is partly positive.
The topic general on line 3 never appeared in an example. No listed topic fit, so the model made up a new one in the correct format. Examples show a pattern but do not set its limits. If only certain values are allowed, state them in the instruction, for example "Topics: performance, bug, feature-request, other."
Choosing good examples
The model picks up everything that your examples have in common, including things you did not intend to teach. Four choices matter most.
Vary the examples
Use different lengths, topics and answers. If all three example reviews are negative, the model leans toward labelling new reviews as negative, because that is the strongest pattern in the prompt.
Include a hard case
One edge case, such as a mixed review or an input that fits no category, teaches the model more than three easy cases. It is also the place where your own judgment is least obvious, so it is where the model needs the most guidance.
Separate the examples clearly
Wrap each example in tags such as <example>, so that the model never mistakes an example for the real input and your own code can tell them apart.
Use two to five examples
Two to five examples are usually enough. More examples add little and make the prompt longer, which costs more and gives the model more text to be distracted by. If five varied examples do not produce the behavior you want, the cause is more likely a missing instruction than a missing example.
Balance and order the examples
Give each label or kind of answer about the same number of examples, because a label that appears more often in the prompt is predicted more often. The order matters as well: the examples closest to the real input tend to have the most influence, so do not put the same label last every time. If you notice the output favouring one answer, shuffle the examples and check whether the bias moves with them.
Test on inputs that are not examples
A prompt can reproduce its own examples perfectly and still fail on new inputs. Keep a small set of real inputs with the correct answers, leave them out of the prompt, and run the prompt on them after every change. When one fails, a good fix is to add it, or a similar case, as an example, and to test on a different input afterwards.
Keeping examples as data
When a program builds the prompt, store the examples as data and generate the prompt text from them. Replacing an example is then a change to one entry, and every example is formatted in the same way.
function buildFewShotPrompt(instruction, examples, newInput) {
const parts = [instruction, ""];
for (const ex of examples) {
parts.push("<example>");
parts.push(`Review: "${ex.input}"`);
parts.push(`Tags: ${ex.output}`);
parts.push("</example>");
}
parts.push("", `Review: "${newInput}"`, "Tags:");
return parts.join("\n");
}
const examples = [
{ input: "Search is fast now, finally.", output: "topic=performance; sentiment=positive" },
{ input: "Can't attach photos anymore.", output: "topic=bug; sentiment=negative" },
];
console.log(buildFewShotPrompt("Tag the review. Copy the format exactly.",
examples, "Battery drain is terrible."));def build_few_shot_prompt(instruction, examples, new_input):
parts = [instruction, ""]
for ex in examples:
parts.append("<example>")
parts.append(f'Review: "{ex["input"]}"')
parts.append(f'Tags: {ex["output"]}')
parts.append("</example>")
parts += ["", f'Review: "{new_input}"', "Tags:"]
return "\n".join(parts)
examples = [
{"input": "Search is fast now, finally.", "output": "topic=performance; sentiment=positive"},
{"input": "Can't attach photos anymore.", "output": "topic=bug; sentiment=negative"},
]
print(build_few_shot_prompt("Tag the review. Copy the format exactly.",
examples, "Battery drain is terrible."))The prompt ends with Tags:, which is the blank in the figure above. The most likely next text is one line of tags in the same shape as the examples.
Storing examples as data also lets you choose them per request. A larger application can keep dozens of labelled examples and put only the few most similar to the current input into the prompt. This keeps the prompt short and gives the model examples that are relevant to the case it has to handle.
Pitfalls with examples
When to use examples and when to use instructions
Use examples when the difficulty is the shape of the answer: a fixed output format, a house style, or a labelling scheme with judgment calls. Use instructions for hard limits such as the allowed labels. In most prompts you need both, with the instruction stating the rules and the examples showing what following them looks like.
Examples do not help much when the answer depends on working something out, because the model copies the shape of the answer and not the reasoning behind it. For those tasks see Give the Model Room to Think. When a failing prompt gives inconsistent output, check the examples first: look for one answer that appears in all of them, for an example that breaks your own rules, and for a case that has no example.
This prompt keeps labelling messages as billing, even obvious bug reports. Find the problem with the examples and rewrite the prompt.
Show a sample answer
All three examples have the same answer, so the strongest pattern is "the answer is billing". The model never learns other labels exist.
The rewritten prompt has one example for each label, so no answer is favoured. The "other" example covers messages that fit nowhere, and the list of labels stops the model from inventing a fifth. The feature-request example mentions exporting on purpose, to show that the topic does not decide the label. What decides it is whether something is broken or missing.
Check yourself
Why does adding input and output examples change the format of a model's reply?
Your examples are all negative reviews. What is the likely side effect?
With good examples, the model still invented a new topic, general. What is the best fix?
What to practise next
When the answer depends on working something out, read Give the Model Room to Think. For replies that a program has to read, see Structured Output.