Few-shot prompting
Few-shot prompting is a way of teaching a task inside the prompt by adding a few solved examples as earlier user and assistant turns.
Last updated: 30 Sep, 2026 · groq 1.7 · gpt-oss-120b on Groq
Describing a task only goes so far. Showing solved examples often works better. This lesson continues the file from System prompts: ask, tickets and structured come from there.
Three solved examples
examples = [
{"role": "user", "content": "You took money from my card two times"},
{"role": "assistant", "content": '{"category": "billing", "priority": 4}'},
{"role": "user", "content": "Where is my delivery? It is a week late"},
{"role": "assistant", "content": '{"category": "shipping", "priority": 3}'},
{"role": "user", "content": "Can I change the email on my account?"},
{"role": "assistant", "content": '{"category": "other", "priority": 2}'},
]Three made-up tickets, one per category, each followed by the answer you want. They go between the system prompt and the real ticket, so the model reads them as a conversation it has already had. None of them is one of the seven test tickets.
Syntax: [system, *examples, user]. *examples spreads the list's items into the new list.
Sorting with the examples in front
for text, expected in tickets:
messages = [{"role": "system", "content": structured}, *examples, {"role": "user", "content": text}]
print(f"{expected:9} {ask(messages)}")billing {"category": "billing", "priority": 4}
shipping {"category": "shipping", "priority": 3}
billing {"category": "billing", "priority": 4}
other {"category": "other", "priority": 2}
shipping {"category": "shipping", "priority": 4}
billing {"category": "shipping", "priority": 4}
other {"category": "shipping", "priority": 4}- The same five tickets are right, in the same JSON shape.
- The fee and the cancellation are still shipping. The examples show a double charge, a late delivery and an account change. None is about a fee or a cancellation, so they gave the model nothing to go on for those two.
- Two priorities changed from the structured-prompt run, and the categories did not.
Why examples change the answer
In Next-token prediction, every token in front of the answer changes the next token's scores. Solved examples put answers in front of the model: after a double charge came billing, so billing becomes likelier after the next charge. The model is not trained by them; its weights do not change. It continues a pattern it can see.
Instructions vs examples
| Instructions (system prompt) | Examples (few-shot) | |
|---|---|---|
| Teaches | The rules, in words | The pattern, by showing it |
| Covers edge cases | Only the ones you write down | Only the ones you show |
| Tokens | Sent with every request | Sent with every request, usually more |
When examples help most
- When the output format is fiddly and easier to show than to describe.
- When the model keeps making the same mistake: an example of exactly that case.
- With smaller models, which follow patterns better than long instructions.
Related
- Previous: System prompts
- Next: Structured output
- Reference: OpenAI prompt engineering guide
- Write a fourth example of your own about cancelling an order, with
other, and run it again. - Put the three examples in a different order.
- Remove the system message and keep only the examples.
You understood something today that you didn't yesterday.