How to Write Few-Shot Examples That Actually Help
Good few-shot examples are relevant, varied and correct, and they are formatted exactly like the output you want. A small set of well-chosen examples usually beats a long list of similar ones. Aim for a handful that cover the range of inputs, including at least one tricky case, and make sure each example shows the behaviour you would be happy to see in production.
Principle 1: Be diverse, not repetitive
If all your examples look alike, the model learns that every input looks like that. Cover different input types, lengths and difficulty. For a ticket classifier, include a short ticket, a long rambling one and one with a typo. For a summariser, include a short document and a long one.
Principle 2: Include a hard case
Easy examples teach little because the model would get them right anyway. Spend your example budget on the cases where the answer is not obvious, and show the right call. This is where examples beat instructions.
Principle 3: Match the real format exactly
The model copies structure. If your examples show JSON with keys in a given order, it will usually follow that order. If your real inputs will include extra whitespace or a footer, include that in at least one example. Mismatched format is a common cause of surprising output.
Principle 4: Keep examples correct and consistent
One wrong label in five examples can confuse the model more than you expect. Review every example as carefully as production code. Make sure similar inputs get similar labels across examples.
Principle 5: Label them clearly
Wrap examples in tags so the model knows they are demonstrations, not part of the real input. This also helps your own code manage them.
EXAMPLES = [
{
"input": "App crashes on startup since the update.",
"output": '{"label": "bug", "urgency": "high"}',
},
{
"input": "can u tell me if theres a student discount??",
"output": '{"label": "question", "urgency": "low"}',
},
{
"input": "I cancelled in March but was billed again today and the invoice shows the wrong company name.",
"output": '{"label": "billing", "urgency": "medium"}',
},
]
def render_examples(examples):
blocks = []
for i, ex in enumerate(examples, 1):
blocks.append(
f'<example id="{i}">\n'
f"<ticket>{ex['input']}</ticket>\n"
f"<answer>{ex['output']}</answer>\n"
f"</example>"
)
return "<examples>\n" + "\n".join(blocks) + "\n</examples>"
SYSTEM = (
"Classify each support ticket. Reply with JSON only, using the same keys "
"as the examples.\n\n" + render_examples(EXAMPLES)
)
print(SYSTEM)
The script prints a system prompt with three demonstrations: a clear bug, a casual question with a typo and a mixed billing case. Each uses the same JSON shape. Pass SYSTEM to the system parameter and send the real ticket as the user message.
How many examples?
There is no universal number. Start with two or three. Add more only if tests show a gap, and stop when the gain flattens. Past a point, extra examples cost tokens without helping, and they raise the risk of copying quirks.
Testing for copying
After adding examples, run inputs that differ from them on purpose. Check whether outputs reuse phrases from the examples. If they do, you can rewrite examples to be more varied, reduce their number, or add an instruction like "do not reuse wording from the examples".
Dynamic example selection
When you have many labelled examples, you can pick the few most similar to the current input, using the retrieval methods covered in the RAG track. This keeps the context small while giving relevant demonstrations. It also means you need to evaluate that selection, since a poor match can mislead the model.
Where to put examples in the prompt
Put examples after the instructions that describe the task and before the real input. This lets the model read the rule, see it demonstrated and then apply it. In an agent, examples that never change are good candidates for the stable, cacheable part of the system prompt. Examples that you select per request belong later, next to the user's input, so they do not break the cache for everything before them.
If an agent uses tools, consider showing an example of a good tool call sequence: the tool chosen, the arguments and how the result was used. This teaches the pattern of work, not only the final answer.
Common mistakes
- Using only easy, similar examples.
- Examples whose style you do not want copied (for example, overly long answers).
- Leaving out the format details that production inputs have.
- Never updating examples after the task changes.
Treat examples as part of your codebase: review them, version them and test them like any other input to your system.
Comments
No comments yet. Have a question or a tip? Be the first to share it.