Few-Shot Prompting
PromptingDefinition
A prompting technique that supplies concrete (input, output) example pairs to guide Claude’s behavior. With the Claude API the idiomatic approach is to encode examples as alternating `user`/`assistant` turns in the `messages` array rather than cramming them into the system prompt — this mirrors the conversation format Claude is trained on and keeps cached system-prompt content stable.
Example Usage
Structure 3–5 examples as alternating user/assistant message objects before your real user turn. Place static examples in the system prompt under `<examples>` tags when you want prompt-cache hits across requests.
Why It Matters for the CCA-F Exam
The exam tests whether you know the two placement strategies (system-prompt `<examples>` vs. messages-array turns), when each maximises cache efficiency, and when few-shot outperforms explicit instructions on Claude (style/format nuance, ambiguous classification boundaries). Expect at least one question distinguishing few-shot examples from [prefill](/glossary/prefill), which is a separate — and now deprecated — concept.
In Depth
Two placement strategies for Claude's API
Few-shot examples can live in two places in a Claude API request, and the choice has concrete performance implications.
Option 1 — messages array (alternating turns)
{
"model": "claude-sonnet-4-6",
"system": "Classify customer feedback as POSITIVE, NEUTRAL, or NEGATIVE.",
"messages": [
{"role": "user", "content": "Shipping was three days late and the box was crushed."},
{"role": "assistant", "content": "NEGATIVE"},
{"role": "user", "content": "Works exactly as described. Setup took five minutes."},
{"role": "assistant", "content": "POSITIVE"},
{"role": "user", "content": "It arrived. I guess it does what it says."},
{"role": "assistant", "content": "NEUTRAL"},
{"role": "user", "content": "{{REAL_INPUT}}"}
]
}This mirrors the conversation format Claude is trained on, which tends to produce the cleanest instruction-following for chat-tuned models. The downside: every API call must transmit the full example turns, and those turns sit in the uncached portion of the prompt.
Option 2 — system prompt with <examples> tags
system = """
Classify customer feedback as POSITIVE, NEUTRAL, or NEGATIVE.
<examples>
<example>
<input>Shipping was three days late and the box was crushed.</input>
<output>NEGATIVE</output>
</example>
<example>
<input>Works exactly as described. Setup took five minutes.</input>
<output>POSITIVE</output>
</example>
</examples>
"""Because the system prompt rarely changes across requests, Anthropic's prompt caching can cache everything up to the first dynamic content. With several verbose examples the token savings per request are substantial — often 60–80 % of input tokens served from cache at ~10 % of the normal token cost.
When few-shot beats instructions on Claude
Claude follows explicit instructions well, so few-shot is not always the right tool. Reserve it for:
- Style and tone calibration — prose examples communicate voice far better than adjectives like “concise” or “formal.”
- Ambiguous classification boundaries — three examples pinpointing the sarcasm/bluntness line outperform a paragraph trying to define it.
- Structured output without a tool schema — if you need a specific plain-text format and can’t use structured output, an example is the most reliable anchor.
Few-shot is *less* useful when: the task is clearly described and obvious, you’re already using a strict JSON tool schema, or token budget is tight.
Prompt caching and example stability
Stable examples = cache hits. If you rotate examples per request (e.g., dynamic RAG-retrieved examples), place them in the messages array rather than the system prompt so the cacheable prefix stays clean. Mix: static canonical examples in the system prompt (cached), plus 1–2 dynamic examples as leading messages turns.
Reasoning in examples
Including a scratchpad inside an example output nudges Claude toward chain-of-thought behavior without a separate instruction — useful for multi-step classification. Conversely, terse example outputs suppress verbose reasoning.
Few-shot vs. prefill
Prefill is unrelated — see prefill for the distinction.
Example
Static few-shot examples placed in the system prompt under <examples> tags. The system prompt is identical across calls, so Anthropic’s prompt cache serves it at ~10 % of normal token cost after the first request. Dynamic per-request content stays in the messages array as the sole uncached turn.
import anthropic
client = anthropic.Anthropic()
# Static examples go in the system prompt so they are prompt-cached across calls.
# Dynamic (per-request) examples go as leading messages-array turns.
SYSTEM = """
Classify customer feedback as POSITIVE, NEUTRAL, or NEGATIVE.
Respond with only the label.
<examples>
<example>
<input>Shipping was three days late and the box was crushed.</input>
<output>NEGATIVE</output>
</example>
<example>
<input>Works exactly as described. Setup took five minutes.</input>
<output>POSITIVE</output>
</example>
<example>
<input>It arrived. I guess it does what it says.</input>
<output>NEUTRAL</output>
</example>
</examples>
"""
def classify(feedback: str) -> str:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=16,
system=SYSTEM, # cached after first call
messages=[
{"role": "user", "content": feedback} # only dynamic token
],
)
return response.content[0].text.strip()
# Three static examples live in the system prompt (cache-eligible).
# Swap to messages-array turns if examples change per request.
print(classify("The product stopped working after two days.")) # NEGATIVEFrequently Asked Questions
Should examples go in the system prompt or the messages array?
Use the system prompt with `<examples>` XML tags when your examples are static — they’ll be prompt-cached and served cheaply on every subsequent call. Use the `messages` array (alternating user/assistant turns) when examples vary per request, so the cacheable system-prompt prefix stays stable.
How many examples are usually sufficient?
Three to five well-chosen examples that cover genuinely ambiguous cases is the practical sweet spot. Each example costs tokens, so target the hard edge cases, not the obvious ones. If you need more than eight examples to get consistent behavior, tighten your instructions first — examples should supplement clear instructions, not replace them.
Does few-shot prompting interact with Claude’s prompt cache?
Yes — and placement determines whether you get cache hits. Examples embedded in the system prompt are part of the cacheable prefix and hit the cache on repeat calls. Examples placed as leading messages-array turns are re-transmitted on every call. For high-volume pipelines, static system-prompt examples with `<examples>` tags is the cache-optimal pattern.
Is few-shot the same as prefill?
No — prefill is a distinct (and now deprecated) technique. See [prefill](/glossary/prefill) for details.