Few-shot prompting with Claude XML
How to use
Few-shot prompting means showing the model worked examples before the real input. With Claude, the convention is to wrap each demo in <example> tags grouped inside <examples>.
How to apply the pattern
- Define the task in
<instructions>. Be explicit about the output format — even one sentence. - Pick 2–5 representative examples. Cover the real distribution: edge cases included, not all the happy path.
- Wrap each example with
<input>and<output>. Or task-specific equivalents like<q>/<a>. - Put the live input after the examples. Order matters — Claude treats what comes last as the active query.
Worked example
<instructions>
Extract the company name from each press release headline.
Return only the company name, no other text.
</instructions>
<examples>
<example>
<input>Acme Corp announces Q3 earnings beat</input>
<output>Acme Corp</output>
</example>
<example>
<input>Globex shutters European division</input>
<output>Globex</output>
</example>
<example>
<input>Sources: Initech in talks to acquire Pied Piper</input>
<output>Initech</output>
</example>
</examples>
<headline>{{ live_headline }}</headline>
Tips
- If accuracy doesn't improve with examples, you probably don't need them — Claude is strong zero-shot.
- Don't include examples from your real production data unless you've handled PII.
- More than ~5 examples rarely helps and bloats your context budget.
Cite this page
Few-shot prompting with Claude XML. claudexml.com. https://claudexml.com/patterns/few-shot/