Room 42, home

Show Claude examples with few-shot prompting

A couple of worked examples in the prompt teach Claude the exact format and quality you want, and they work better than describing it in prose.

Promptingintermediate8 min readUpdated 13 Jun 2026

In short

  1. 01Few-shot prompting puts a few worked input and output examples in the prompt so Claude matches the pattern instead of guessing from prose.
  2. 02Reach for examples when the output has a precise shape, format, or tone that is hard to describe but easy to demonstrate.
  3. 03Use three to five examples that are relevant, diverse, and wrapped in tags, and never let an example contradict your instructions.
  4. 04Promote your best examples into a project memory file so the format steers every session without re-pasting them.
Contents · 12 sections
  1. 1The one-line idea
  2. 2When should I use examples instead of instructions?
  3. 3A first example
  4. 4How many examples should I use?
  5. 5What makes a good few-shot example?
  6. 6Show the reasoning, not just the answer
  7. 7Where should I put my few-shot examples?
  8. 8Ask Claude to build the examples
  9. 9Common mistakes
  10. 10A workflow that compounds
  11. 11When should I not use few-shot prompting?
  12. 12Checklist

Few-shot prompting means putting a small number of worked examples (input and the exact output you want) directly in your prompt. Claude reads the pattern and matches it. Two or three good examples steer format, tone, and structure more reliably than a paragraph of instructions describing the same thing.

The one-line idea

Telling Claude "return the result as a bullet list, present tense, no preamble" works. Showing Claude one filled-in example of that bullet list works better, because the example removes every ambiguity at once. You do not have to name the rules. The example carries them.

This technique has names. "Zero-shot" is a prompt with no examples. "One-shot" has a single example. "Few-shot" (also called multishotAnother name for few-shot prompting: providing several example input/output pairs in the prompt to steer the model) has several. The more an output has a precise shape you care about, the more examples earn their place.

When should I use examples instead of instructions?

Reach for examples when the output has a shape that is hard to describe but easy to show.

SituationWhy examples win
A specific output formatOne sample row defines column order, casing, and separators at once
A consistent tone or voiceStyle is hard to name, easy to demonstrate
Edge-case handlingShow the tricky input and its correct output instead of explaining it
Classification into fixed labelsEach example pins one label to one kind of input
A repeated task across a teamThe examples become the shared spec

Reach for plain instructions instead when the task is open-ended ("explain this code"), when the format is already obvious, or when you only need it once and do not care about exact shape.

A first example

Say you want short release-note lines from commit messages. Describing the format in prose leaves gaps: How long? Past or present tense? Does it keep the ticket number? Show it instead.

Try this prompt
Turn each commit message into a one-line release note. <example> Input: fix: stop double-charge when retrying a failed payment (PAY-204) Output: Fixed a bug that could charge a card twice on payment retry. </example> <example> Input: feat: add CSV export to the reports page (RPT-88) Output: Added CSV export to the reports page. </example> Now do this one: Input: refactor: split the auth middleware into session and token checks (AUTH-12)

The two examples settle every open question: past tense, full sentence, no ticket number, no commit-type prefix. Claude follows the shape on the third input without being told the rules in words.

How many examples should I use?

Anthropic's prompt-engineering guidance recommends three to five examples for the best results. One example fixes the basic shape. A second confirms the pattern is intentional, not an accident of the first. A third through fifth let you cover variety. Past that, returns drop and you are paying tokens for little gain.

Each example costs tokens on every turn it stays in context. That is a fair trade when the examples buy you a reliable format, and it is the reason you stop adding them once the pattern holds. For how that token cost works, see token usage and cost.

What makes a good few-shot example?

Three properties separate examples that steer well from examples that mislead.

Relevant

Mirror the real task. If you classify support tickets, your examples are real-looking support tickets, not toy sentences. Claude generalises from the surface details it sees, so off-topic examples drag the output off-topic.

Diverse

Vary the examples so Claude learns the rule, not an accidental quirk. If all three of your examples happen to be one-word answers, Claude may decide one word is the rule. Cover the range you expect in production, including at least one edge case. Diverse examples also stop Claude from latching onto an unintended pattern, like always starting with the same verb.Common gotchaWatch for content bleed: if every example happens to mention "Acme" or returns three bullets, Claude often copies that literal value into a new answer where it does not belong, because it cannot tell the format from the contents.Vary the concrete details (names, counts, lengths) across your examples so the only thing they share is the shape you want.

Structured

Wrap each example in tags so Claude can tell examples apart from instructions and from the live input. Anthropic's guidance is to wrap each example in <example> tags, and a set of them in <examples> tags.

Try this prompt
Classify each message as billing, technical, or account. <examples> <example> Message: My card was declined but I was still charged twice. Label: billing </example> <example> Message: The export button spins forever and never downloads. Label: technical </example> <example> Message: I need to change the email address on my login. Label: account </example> </examples> Message: I was charged after I cancelled my subscription last month.

The tags do real work. Without them, a long example can blur into the instructions and Claude may treat your sample input as the task to answer.Common gotchaFor classification, balance the labels: a set of four "billing" examples and one "technical" quietly biases Claude towards the label it saw most.Give each label a roughly equal count, and shuffle the order so the last example is not always the same class.

Show the reasoning, not just the answer

For tasks that need a judgement call, show the thinking inside the example, not only the final answer. Put the reasoning in a <thinking> tag and the result in an <answer> tag. Claude generalises the reasoning style, so the pattern you demonstrate shapes how it reasons on new inputs. This pairs well with asking Claude to think before it answers.

Try this prompt
Decide whether each code change needs a database migration. <example> Change: Added an "archived" boolean column to the orders table. <thinking>A new column changes the schema, so existing rows need the column added.</thinking> <answer>Yes, this needs a migration.</answer> </example> <example> Change: Renamed a variable inside the checkout function. <thinking>This is internal code only. The database schema does not change.</thinking> <answer>No migration needed.</answer> </example> Change: Added a unique index on the users.email column.

Showing the reasoning is more useful than showing the verdict alone, because the next input will be different and Claude needs the method, not the memorised answer.

Where should I put my few-shot examples?

The strongest place for examples is not a one-off chat message. It is a file Claude reads on every turn, so you write the examples once and they steer every session.

In the Claude Code CLI, that file is the project memory file at the repo root. Put a short, clearly-labelled set of examples under a heading, and they persist without re-pasting.

## Commit message format
 
Write commit messages in this exact shape.
 
<example>
fix: stop the cart total from going negative on coupon stacking
</example>
 
<example>
feat: add keyboard shortcuts to the search bar
</example>

Claude Code loads this file at the start of every session, so the examples shape its output the same way each time. For the mechanics of memory files and where they load from, see rules and memory files. For why a stable, cached file is cheaper than re-pasting examples each turn, see prompt caching.

You can also keep examples in a separate document and point Claude at it when the task comes up:

Try this prompt
@docs/example-release-notes.md Use these as the format. Turn the last 10 commits on this branch into release notes that match.

Ask Claude to build the examples

You do not have to write every example by hand. Claude can draft a set from a single seed, then you correct the ones that are wrong. The corrected set becomes your few-shot block.

Try this prompt
Here is one input and the output I want for it. Write four more example pairs that cover different cases, including one edge case where the input is empty. Keep the exact output format of my example.

You can also have Claude grade your existing examples for the two properties that matter most.

Try this prompt
Review these five examples. Are they diverse enough that you would learn the rule and not an accidental pattern? Flag any two that are too similar and suggest a replacement that covers a case the set is missing.

This is a fast way to find the gap you did not notice, such as five examples that all happen to be short.

Common mistakes

A few failure modes show up again and again.

MistakeWhat happensFix
Examples too similarClaude learns a quirk, not the ruleVary length, shape, and content
No tags around examplesClaude answers your sample inputWrap each in <example> tags
Inconsistent format across examplesClaude cannot tell which detail mattersMake every example follow the same shape exactly
One example only, for a tricky taskThe shape is set but variety is notAdd a second and a third covering edge cases
Examples contradict the instructionsClaude trusts the examples and ignores the wordsMake the examples demonstrate the rule you stated

The contradiction case is worth naming twice. When your prose says one thing and your example shows another, the example usually wins. So the example is not decoration. It is the spec, and it has to be right.

A workflow that compounds

Few-shot prompting pays off most when you treat the examples as an asset you keep.

Start with one example in a chat. When the format is right, promote the best examples into a file Claude reads every time. Now the format is a property of the project, not something you re-explain on each task.

When should I not use few-shot prompting?

Examples are not free and they are not always the answer.

  • You need a guaranteed machine-readable shape. If the output must be valid JSON that conforms to a schema every time, a few examples help but a stricter mechanism is better. See structured outputs.
  • The task is open-ended. "Review this PR and tell me what worries you" has no fixed shape to demonstrate. Plain instructions fit better.
  • The examples would be huge. If one example is a 500-line file, three of them dominate the context. Trim to the smallest example that still shows the pattern.

Checklist

Before you ship a few-shot prompt, confirm:

  • Each example mirrors the real task, not a toy version of it.
  • You have three to five examples for an important format, and they vary.
  • Every example is wrapped in <example> tags, and the set in <examples> tags.
  • All examples follow the exact same shape, so the pattern is unmistakable.
  • No example contradicts the instructions.
  • For a stable format, the examples live in the project memory file, not a single message.

Get those right and Claude matches your format on the first try, on the tenth try, and across everyone on the team using the same file.