Few-Shot Examples & XML Tags
Show Claude examples of good output, and use XML-style tags to separate parts of your prompt.
Two reliable techniques sharpen Claude's output: showing examples, and structuring your prompt with tags.
Few-shot examples
Instead of describing the format, show it. Include one or two input-output examples and Claude matches the pattern. This is especially effective for consistent tone, structure, or edge-case handling.
XML-style tags
Wrapping parts of your prompt in tags like <context> and <question> makes the structure unambiguous. Claude handles tagged sections cleanly, so it knows what is instructions, what is data, and what is the actual request.
Example
const prompt = `Classify the sentiment as positive, negative, or neutral.
<examples>
<example><text>Love this, works great!</text><label>positive</label></example>
<example><text>It broke after a day.</text><label>negative</label></example>
</examples>
<text>${userReview}</text>
Return only the label.`;
const res = await client.messages.create({
model: "claude-haiku-4-5-20251001", max_tokens: 10,
messages: [{ role: "user", content: prompt }],
});When to use it
- Use two or three labelled examples to show Claude the exact headline capitalisation style your brand uses.
- Wrap user-submitted content in <user_input> tags to separate it from your instructions and prevent injection.
- Provide XML-tagged examples of good and bad error messages to calibrate Claude's tone for your app.
More examples
Few-shot examples in the prompt
Three labelled examples teach Claude the exact output format (single lowercase word) without any written rules.
const prompt = `Classify customer sentiment.
Examples:
Input: "Works great, fast delivery!"
Output: positive
Input: "Arrived damaged, very unhappy."
Output: negative
Input: "Package was late but product is fine."
Output: mixed
Now classify:
Input: "Quality is decent but shipping took 3 weeks."
Output:`;
const msg = await client.messages.create({
model: "claude-haiku-4-5",
max_tokens: 8,
messages: [{ role: "user", content: prompt }],
});XML tags to isolate user input
Wrapping user content in XML tags makes it impossible for that content to override the system instructions.
const prompt = `You are a content moderator.
Classify the text inside <user_input> as safe or unsafe.
Respond with exactly one word: safe or unsafe.
<user_input>
${userSubmittedText}
</user_input>`;
const msg = await client.messages.create({
model: "claude-haiku-4-5",
max_tokens: 4,
messages: [{ role: "user", content: prompt }],
});Combined XML tags and few-shot
Uses XML-fenced examples to show the expected input/output shape while isolating live content from instructions.
const prompt = `Extract action items from meeting notes.
<examples>
<example>
<notes>Alice will update the roadmap by Friday. Bob to send invoice.</notes>
<actions>[{"owner":"Alice","task":"Update roadmap","due":"Friday"},{"owner":"Bob","task":"Send invoice","due":null}]</actions>
</example>
</examples>
<notes>
${meetingNotes}
</notes>
Return a JSON array only.`;
const msg = await client.messages.create({
model: "claude-sonnet-4-5",
max_tokens: 512,
messages: [{ role: "user", content: prompt }],
});
Discussion