Few-Shot Prompting
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
Sometimes the easiest way to tell a model what you want is to show it. A few worked examples in the prompt teach format, tone and edge-case decisions faster than a paragraph of rules, and they're one of the most dependable tools you have. They also have a habit of teaching things you didn't mean to teach. This lesson covers both sides.
What you'll learn
- What few-shot prompting is and when examples beat instructions.
- How many examples to use and what makes them good.
- How to wrap examples so the model never mistakes them for the task.
- How to catch common example mistakes with a small script.
Before you start
Show, don't only tell
Zero-shot means you describe the task and give no examples. Few-shot (also called multishot) means you include a handful of input and output pairs. Anthropic's guidance calls examples "one of the most reliable ways" to steer output format, tone and structure, and says a few well-chosen ones improve accuracy and consistency.
Examples are at their best when the thing you want is easier to show than to describe. A label set with a tricky boundary (is a late parcel plus a refund request "shipping" or "billing"?), a house writing style, a very specific output layout. They're less useful when the task is simple and instructions already cover it, because every example costs tokens on every request.
The three qualities that matter
The guide gives a short list of what good examples look like:
- Relevant. They mirror your real inputs. Invented toy examples teach the model a toy task.
- Diverse. They cover the range, including edge cases, so the model doesn't latch onto an accident of your examples. If three of your four examples are billing tickets, the model learns that billing is the likely answer.
- Structured. They sit inside tags, with several in an
<examples> wrapper, so the model can tell them apart from your instructions and from the real input.
The suggested number is 3 to 5 for best results. More isn't automatically better: you pay for every one, and after a few the gains usually flatten. Anthropic also notes you can ask Claude to review your examples for relevance and diversity, or to generate more from your starting set. A model-generated example still needs a human to check it before it goes in.
Building a few-shot prompt
This module builds the prompt from a list of examples, and includes a checker for the mistakes that creep in later when someone edits the list:
from dataclasses import dataclass
@dataclass
class Example:
ticket: str
label: str
# Relevant (real-looking tickets), diverse (every label, plus an awkward one), and kept short.
EXAMPLES = [
Example("I was charged twice for the same order #4412.", "billing"),
Example("Tracking says delivered but nothing is at my door.", "shipping"),
Example("Do you offer gift wrapping?", "other"),
# An edge case teaches more than a fourth easy one: a refund is a billing matter even if the parcel is late.
Example("My parcel is 10 days late, I want my money back.", "billing"),
]
INSTRUCTIONS = (
"Classify the support ticket as exactly one of: billing, shipping, other. "
"Reply with the single label and nothing else."
)
def examples_block(examples: list[Example]) -> str:
items = "\n".join(
f"<example>\n<ticket>{e.ticket}</ticket>\n<label>{e.label}</label>\n</example>" for e in examples
)
return f"<examples>\n{items}\n</examples>"
def build_prompt(ticket: str, examples: list[Example] = EXAMPLES) -> str:
return (
f"<instructions>\n{INSTRUCTIONS}\n</instructions>\n\n"
f"{examples_block(examples)}\n\n"
f"<ticket>{ticket}</ticket>"
)
def check_examples(examples: list[Example]) -> list[str]:
"""Cheap sanity checks that catch the most common example mistakes."""
notes = []
labels = [e.label for e in examples]
for label in set(labels):
share = labels.count(label) / len(labels)
if share > 0.5:
notes.append(f"'{label}' is {share:.0%} of the examples; the model may over-predict it.")
if not 3 <= len(examples) <= 5:
notes.append(f"{len(examples)} examples; Anthropic suggests 3 to 5.")
if len({e.ticket for e in examples}) != len(examples):
notes.append("Duplicate example tickets.")
return notes
Look at the fourth example. It would have been easy to add another obvious billing ticket and call it done. Instead it's an edge case: a late parcel where the customer wants their money back, labelled billing. That one example carries a business rule ("a refund request is billing even if delivery is the cause") that would take a paragraph to explain and still get misread. An edge case teaches more than a fourth easy one.
The prompt it generates puts the instructions first, then the examples, then the real ticket:
<instructions>
Classify the support ticket as exactly one of: billing, shipping, other. Reply with the single label and nothing else.
</instructions>
<examples>
<example><ticket>I was charged twice for the same order #4412.</ticket><label>billing</label></example>
...
</examples>
<ticket>Where is my order?</ticket>
(In the real output each example sits on separate lines.) We tested that the sections come out in that order with four examples and four labels, and that check_examples stays quiet for the shipped list but complains when one label makes up 75 percent of the examples, when two examples are identical, and when there are only two.
How examples go wrong
- Copying the surface. If every example answer is one sentence long, expect one-sentence answers even when the real input needs more. If every example starts with "Sure!", you'll see it in the output.
- Label imbalance. The model drifts toward whichever label appears most. Keep labels roughly even, or use the imbalance on purpose and know you're doing it.
- Examples that contradict the instructions. The model has to choose, and you may not like which one it picks. Read your examples against your rules.
- Stale examples. A policy changes and the examples keep teaching the old one. Review them whenever the rules change.
- Real customer data. Don't paste real names, emails or order numbers into prompts that get logged, shared or committed. Rewrite them.
Examples or instructions?
You don't have to choose. A short instruction says the rule and the examples show it. When they disagree, that's a bug in your prompt, and the evaluation set (lesson 3.8) is how you find it. As a rule of thumb, if you find yourself writing a long paragraph explaining a format, replace the paragraph with two examples and see whether the results hold.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A support team labels tickets as billing, shipping or other, and a few borderline cases keep going to the wrong queue: refund requests caused by late delivery, and questions about gift cards. Rather than lengthening the instructions, they add two examples for exactly those cases, rerun their saved set of fifty tickets, and check that nothing else changed. The edge-case examples earn their place because the evaluation shows the borderline tickets now land correctly, and no clear-cut ticket regressed.
Mistakes to avoid
- Using one example, which the model copies closely.
- Making all examples look alike in length, wording or label.
- Leaving examples outside tags so they blur into the instructions.
- Never testing whether the examples helped.
- Keeping adding examples when a rule would be cheaper.
Security notes
Examples are trusted text that you wrote, so keep them in your own template. Never build examples from raw user input at runtime without review, because that lets any user teach the model new behaviour for everyone. If you do select examples dynamically from a store, make sure only vetted ones are in it.
Cost and speed
Examples are input tokens on every call. Four short examples might add a hundred or two tokens, which is nothing at low volume and real money at high volume. Measure the cost with token counting (lesson 1.4). Because the examples rarely change, they're also a natural part of a stable prompt prefix for caching (lesson 2.9).
Key takeaways
- Show the model what you want when that's easier than describing it.
- Use 3 to 5 examples that are relevant, diverse and wrapped in tags.
- Include at least one edge case, and watch for label imbalance and copied quirks.
- Test with an evaluation set, and keep examples free of real personal data.
Lesson recap: show what you want, with a few varied examples in tags.
Quick quiz
- How many examples does Anthropic's guide suggest, and what three qualities should they have?
- Why wrap examples in tags?
- Why is an edge-case example worth more than another easy one?
- Three of your four examples carry the label "billing". What's the risk?
- Why shouldn't examples be built from live user input?
Answers and explanations
- 3 to 5. They should be relevant, diverse and structured (in tags).
- So the model can tell examples apart from instructions and from the real input.
- It teaches a decision the model would otherwise have to guess, such as a refund request caused by a late parcel counting as billing.
- The model may over-predict "billing" because it looks like the most likely answer.
- Any user could then influence the model's behaviour for everyone. Examples should be vetted text you control.
Official references and further reading
Related tutorials