Skip to content
AgenticAIWiki
  1. Learn
  2. Part 2
  3. 12. Few-shot examples
  4. 12.1

Lesson 12.1 · 1 of 4 · 4 min read · Reviewed 24 Nov 2026

When examples beat instructions

By the end, you can

  • Recognise tasks where examples work better than instructions.
  • Add examples in a consistent format.

Some things are easier to show than to describe: a tone of voice, a tricky output format, how to label a borderline case. Few-shot promptingFew-shot prompting: Including a few worked examples of input and desired output in the prompt so the model copies the pattern. puts a few worked examples in the prompt.

<examples>
<example>
<input>The app crashed when I uploaded a 2 GB file.</input>
<output>{"category": "bug", "area": "upload", "urgent": false}</output>
</example>
<example>
<input>I was charged twice this month!</input>
<output>{"category": "billing", "area": "charges", "urgent": true}</output>
</example>
</examples>

When examples win

  • Format that’s fiddly to specify.
  • Style and tone.
  • Judgement calls, where the line between two labels is fuzzy.

How many

Start with three to five. More helps only if they add variety. Keep the format identical across examples, so the pattern is clear.

Key points

  • Examples show format, tone and judgement that are hard to describe in words.
  • Three to five varied examples are often enough.
  • Wrap examples in tags such as <example> so they're clearly separate from the real input.
Hands-on labRuns in the Playground

Add three examples to a classifier prompt

Build a tagged few-shot block with identical example shapes and send it with a new ticket.

You will have: An examples_block function, a shape check across examples, and a simulated classification.

Steps

  1. Fill in examples_block so each example is wrapped in <example>, <input> and <output> tags.
  2. Add a third example for a how-to ticket with exactly the same keys and types.
  3. Run it and check the shape test passes.
  4. Change one example's urgent value to the string "yes" and watch the shape test fail.
  5. Send a new ticket to the simulated client with the examples in the system prompt.

Done when

  • The script reports 3 examples with identical keys and types.
  • The examples block is wrapped in <examples> tags.
  • Breaking one example's type makes the check print False.

Open in the Playground No API key needed.

Starter code (Python)
import json
import anthropic

client = anthropic.Anthropic()  # simulated

EXAMPLES = [
    ("The app crashed when I uploaded a 2 GB file.", {"category": "bug", "area": "upload", "urgent": False}),
    ("I was charged twice this month!", {"category": "billing", "area": "charges", "urgent": True}),
    # TODO: add a third example for a "how-to" ticket, in exactly the same shape.
]

def examples_block(examples):
    parts = []
    for text, label in examples:
        # TODO: wrap each example as <example><input>...</input><output>JSON</output></example>
        parts.append(text)
    return "<examples>\n" + "\n".join(parts) + "\n</examples>"

def same_shape(examples):
    keys = [tuple(label.keys()) for _, label in examples]
    types = [tuple(type(v).__name__ for v in label.values()) for _, label in examples]
    return len(set(keys)) == 1 and len(set(types)) == 1

system = ("Classify support tickets. Reply with JSON only, in the same shape as the examples.\n"
          + examples_block(EXAMPLES))
print(f"{len(EXAMPLES)} examples, identical keys and types: {same_shape(EXAMPLES)}")
print("examples block lines:", len(examples_block(EXAMPLES).splitlines()))

ticket = "My invoice shows the wrong company name."
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=200, system=system,
                               messages=[{"role": "user", "content": f"<ticket>{ticket}</ticket>"}])
print("reply:", "".join(b.text for b in reply.content if b.type == "text")[:120])
Show a solution
import json
import anthropic

client = anthropic.Anthropic()  # simulated

EXAMPLES = [
    ("The app crashed when I uploaded a 2 GB file.", {"category": "bug", "area": "upload", "urgent": False}),
    ("I was charged twice this month!", {"category": "billing", "area": "charges", "urgent": True}),
    ("How do I export last year's reports?", {"category": "how-to", "area": "reports", "urgent": False}),
]

def examples_block(examples):
    parts = []
    for text, label in examples:
        parts.append(f"<example>\n<input>{text}</input>\n<output>{json.dumps(label)}</output>\n</example>")
    return "<examples>\n" + "\n".join(parts) + "\n</examples>"

def same_shape(examples):
    keys = [tuple(label.keys()) for _, label in examples]
    types = [tuple(type(v).__name__ for v in label.values()) for _, label in examples]
    return len(set(keys)) == 1 and len(set(types)) == 1

system = ("Classify support tickets. Reply with JSON only, in the same shape as the examples.\n"
          + examples_block(EXAMPLES))
print(f"{len(EXAMPLES)} examples, identical keys and types: {same_shape(EXAMPLES)}")
print("examples block lines:", len(examples_block(EXAMPLES).splitlines()))

ticket = "My invoice shows the wrong company name."
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=200, system=system,
                               messages=[{"role": "user", "content": f"<ticket>{ticket}</ticket>"}])
print("reply:", "".join(b.text for b in reply.content if b.type == "text")[:120])
Quick checkYou need replies in a specific house style that's hard to describe. What usually works best?
How sure are you?
Pick an answer and how sure you are