Sometimes explaining what you want in words is harder than simply showing it. Few-shot prompting does exactly that: you include a few examples of the task, with inputs and the desired outputs, directly in the prompt. The model then continues the pattern for your new input. No training and no special software are needed.
Zero-shot, one-shot and few-shot
- Zero-shot: only instructions, no examples. "Classify this review as positive or negative."
- One-shot: one example included.
- Few-shot: several examples, typically between two and ten.
The idea of learning from examples inside the prompt, without changing the model's weights, is called in-context learning.
A simple example
Suppose you want short, consistent sentiment labels for customer messages.
Classify each message as POSITIVE, NEGATIVE or NEUTRAL.
Message: "The delivery was fast and the packaging was great."
Label: POSITIVE
Message: "I waited two weeks and nobody answered my emails."
Label: NEGATIVE
Message: "I ordered the blue one."
Label: NEUTRAL
Message: "The app keeps crashing when I try to pay."
Label:
The model sees the format and the type of reasoning expected and will most likely answer NEGATIVE. The examples taught it the label set, the style and the level of strictness without any long instructions.
When few-shot prompting helps
- Specific formats: you want output in a fixed style, such as JSON fields, a table row or a particular email tone.
- Custom labels: categories that are unique to your business, like "billing issue" or "delivery delay".
- Subtle judgement: tasks where the line between categories is fuzzy and examples show where you draw it.
- Style imitation: rewriting text in a house style by showing before-and-after pairs.
How to choose good examples
The examples are the most important part, so choose them carefully.
- Make them representative. Cover the typical cases you expect.
- Include edge cases. Show how to handle ambiguous or tricky inputs.
- Balance the labels. If every example is POSITIVE, the model may lean that way. Mix the categories.
- Keep format identical. Same separators, same spacing, same label names.
- Keep them correct. A wrong example teaches the wrong rule.
- Vary the wording. Avoid examples that are near copies of one another.
Even the order can matter slightly. If results look biased, shuffle the examples or put a typical one last.
Few-shot prompting in Python
Here is a way to build a few-shot prompt programmatically and send it to a model API. The examples are stored in a list so you can edit them easily.
import anthropic
client = anthropic.Anthropic()
EXAMPLES = [
("The delivery was fast and the packaging was great.", "POSITIVE"),
("I waited two weeks and nobody answered my emails.", "NEGATIVE"),
("I ordered the blue one.", "NEUTRAL"),
]
def classify(message: str) -> str:
shots = "\n\n".join(
f'Message: "{text}"\nLabel: {label}' for text, label in EXAMPLES
)
prompt = (
"Classify each message as POSITIVE, NEGATIVE or NEUTRAL. "
"Answer with the label only.\n\n"
f'{shots}\n\nMessage: "{message}"\nLabel:'
)
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=5,
messages=[{"role": "user", "content": prompt}],
)
return response.content[0].text.strip()
print(classify("The app keeps crashing when I try to pay."))
Setting a small max_tokens keeps the answer to one label, which is convenient when you process many messages automatically.
A mini checklist for your examples
Before you send a few-shot prompt, read your examples as if you were the model. Do they all follow the same layout? Could any example be read in two ways? Are the labels spelled exactly as you want them returned? Do the examples cover both the easy and the awkward cases you expect to see? Five minutes spent tidying examples usually saves hours of fixing inconsistent output later.
Limits and trade-offs
- Longer prompts cost more and take more of the context window.
- Examples can bias the output toward their wording or length.
- It is not guaranteed. The model may still ignore the pattern on unusual inputs, so test on a variety of cases.
- For very large or complex tasks, fine-tuning a model or using retrieval may be more efficient than packing dozens of examples into every request.
A sensible approach is to start zero-shot, add examples when the results are inconsistent, and only consider fine-tuning if you need higher quality at scale.
Frequently Asked Questions
How many examples should I use?
Often three to five is enough. Add more if the task is subtle, but stop when additional examples no longer improve results.
Does few-shot prompting change the model?
No. The model's weights stay the same. The examples only influence the response for that one request.
Can I use few-shot prompts for translation or style?
Yes. Showing a few before-and-after pairs is a very effective way to set tone, terminology and formatting.
What if the output format is still wrong?
Make examples more consistent, state the format explicitly in the instruction, and validate the output in code, retrying when necessary.
Conclusion
Few-shot prompting is one of the simplest and most effective upgrades to a prompt: show the model what good looks like. Choose representative, correct and consistent examples, test on varied inputs, and you will get steadier results without any training.