Area 01 · LLM · Concept 08 of 20

Few-shot Prompting

Put 2–5 worked examples in the prompt so the model copies the pattern.

01–10 · The basePhase 1 · Weeks 3–6≈ 45 min

The idea

Describing a format in words is often less reliable than showing it. Few-shot prompting includes a handful of input → output examples before the real input, and the model follows the pattern: the labels you use, the length, the tone and the edge-case handling.

Choose examples that cover different cases, including at least one tricky one. Keep them consistent with each other; if two examples disagree on format, the model will too. Wrap them in clear tags so the model can tell examples from the real task.

Examples cost tokens on every call. When the pattern is simple, a clear description plus one example is often enough; when it needs dozens of examples, consider fine-tuning or structured outputs.

Why it matters

It is the fastest way to get consistent labels, extraction formats and house style without training anything.

Words to know

  • Zero-shotInstructions with no examples.
  • ShotOne worked example.

Try it without code

Classify tickets with examples

  1. Ask a chat app to label five support tickets with no examples.
  2. Then send the prompt below with three examples.
  3. Compare the label names and consistency.
Label each ticket as BILLING, BUG or HOWTO. <example>Ticket: "I was charged twice" -> BILLING</example> <example>Ticket: "Export button does nothing" -> BUG</example> <example>Ticket: "How do I add a teammate?" -> HOWTO</example> Ticket: "The invoice PDF is blank" ->

Use it in code

Build few-shot messages from a list of examples.

python
import anthropic
client = anthropic.Anthropic()

EXAMPLES = [("I was charged twice", "BILLING"),
            ("Export button does nothing", "BUG"),
            ("How do I add a teammate?", "HOWTO")]

def classify(ticket):
    shots = "\n".join(f"<example>{t} -> {l}</example>" for t, l in EXAMPLES)
    prompt = f"Label the ticket as BILLING, BUG or HOWTO. Reply with the label only.\n{shots}\nTicket: {ticket} ->"
    msg = client.messages.create(model="claude-haiku-5-5", max_tokens=5, temperature=0,
                                 messages=[{"role": "user", "content": prompt}])
    return msg.content[0].text.strip()

print(classify("The invoice PDF is blank"))
PRACTICE · TICK IT AFTER THIS

Label 20 real messages (emails, issues) zero-shot and few-shot. Count disagreements with your own labels.

Common mistakes

  • All examples from the same easy case.
  • Examples that contradict the instructions.
  • Leaking the expected answer for your test set into the examples.

Go deeper