Zero-shot vs few-shot prompting
Zero-shot gives only instructions; few-shot adds a handful of input→output examples, which teach format and edge cases better than prose. The model also copies their quirks, so choose them deliberately.
Analogy: Onboarding a new hire: a style guide helps, but showing three real tickets and how you answered them, including the weird one, teaches more in two minutes.
How it works
- Demos work without weight updates (in-context learning); much of the effect is format and label space.
- Pick 3–5 diverse examples that cover every label and tricky boundary, with varied length and phrasing.
- Balance labels and shuffle order: models over-predict the majority label and the last one shown.
- Dynamic few-shot embeds each input and retrieves the k most similar examples from a reviewed bank.
- Reasoning models are the exception: start them zero-shot, since few-shot consistently degraded DeepSeek-R1.
Check yourself
Answer to mark it learned; your answers aren't scored.
Your ticket classifier uses dynamic few-shot: it retrieves the k = 4 most similar reviewed examples per ticket. It keeps answering billing for tickets that are really bug. Traces show all 4 retrieved demos are labeled billing. Best fix?
Go deeper
Full explanation
Zero-shot prompting gives the model only instructions. Few-shot adds k demonstrations (input → ideal output) and the model imitates them with no weight updates: in-context learning, shown at scale in the GPT-3 paper.
Examples are the most precise spec you can write. They pin down format (exact labels, JSON shape, length, tone) and policy on edge cases ("charged twice after a crash" → billing, not bug) that prose leaves ambiguous. Min et al. 2022 found that even random labels in the demos barely hurt classification accuracy: much of the effect is format and label space.
How to pick them:
- 3–5 diverse, representative examples covering every label and the tricky boundaries, wrapped in
<example>tags or sent as user/assistant pairs. - Vary length and phrasing, or the model copies the surface.
- Balance labels and shuffle order: models over-predict the majority label and the last one shown (Zhao et al. 2021).
Dynamic few-shot embeds each input and retrieves the k most similar examples from a reviewed bank, so every call gets the most relevant demos. Many-shot uses long context to fit hundreds or thousands of examples, and keeps improving many tasks.
Reasoning models are the exception: start zero-shot. DeepSeek reported that few-shot consistently degraded R1.
Code Dynamic few-shot ticket classifier
import numpy as np
from openai import OpenAI
from sentence_transformers import SentenceTransformer
client = OpenAI()
MODEL = "gpt-4.1-mini" # any non-reasoning chat model
enc = SentenceTransformer("all-MiniLM-L6-v2")
LABELS = "billing, bug, how_to, account, other"
bank = load_reviewed_examples() # [{"text": ..., "label": ...}], a few hundred
bank_vecs = enc.encode([b["text"] for b in bank], normalize_embeddings=True)
def classify(ticket: str, k: int = 4) -> str:
q = enc.encode([ticket], normalize_embeddings=True)[0]
# k most similar, ascending: the closest example sits right before the input.
# In production also cap examples per label to avoid majority-label bias.
top = np.argsort(bank_vecs @ q)[-k:]
shots = []
for i in top: # each demo is a user/assistant pair: format is shown, not described
shots += [{"role": "user", "content": bank[i]["text"]},
{"role": "assistant", "content": bank[i]["label"]}]
messages = [
{"role": "system", "content": f"Classify the support ticket. Reply with one label: {LABELS}."},
*shots,
{"role": "user", "content": ticket},
]
out = client.chat.completions.create(model=MODEL, messages=messages, temperature=0)
return out.choices[0].message.content.strip()Real example
A ticket router must output one of 5 labels. Zero-shot, the model writes Billing issue. instead of billing and splits "charged twice after the app crashed" between bug and billing. Four examples, one per tricky boundary and including that exact case labeled billing, fix the format and encode a policy the instructions never stated. The bill: 4 × ~80 tokens = ~320 extra input tokens per call; at 2M tickets a month that is ~640M tokens, so keep a static block cached or keep k small.
Trade-offs +3 · −3
+ You gain
- Teaches exact format and tone faster than describing them.
- Encodes edge-case policy by demonstration instead of rules nobody reads.
- No training: change behavior by editing a list and roll back in seconds.
− You pay
- Every example is paid on every call; dynamic examples also change the prefix and miss the prompt cache.
- The model copies surface quirks: length, phrasing, even specific entities.
- Results swing with example choice, order and label balance.
Use it when
When the format or labeling policy is easier to show than describe, zero-shot fails on edge cases in your eval, and you have a few reviewed examples. Go dynamic when inputs are diverse and you have hundreds of labeled pairs.
Avoid it when
Reasoning models (start zero-shot), tasks the instructions already nail, or high-volume tasks with thousands of labeled examples, where fine-tuning or distillation is cheaper per call.
Numbers to know 5
- Start with 3–5 diverse examples (Anthropic's guidance); add more only if your eval improves.
- Zhao et al. 2021: prompt format, example choice and order alone swung GPT-3 from near chance to near state-of-the-art.
- Min et al. 2022: random labels in the demos barely hurt classification accuracy; format and label space carry much of the effect.
- Many-shot ICL (Agarwal et al. 2024): hundreds to thousands of examples in long context gave significant gains across many tasks.
- DeepSeek-R1 paper: few-shot consistently degraded the reasoning model, so they recommend zero-shot.
Common pitfalls 4
- All examples share one length or opener, so every answer copies it.
- Examples contradict the instructions; the examples usually win.
- Dynamic retrieval returns four examples with the same label and the model just echoes it; cap examples per label.
- Evaluating on the same items used as examples: leakage inflates the score.
Interview questions 2
Answer out loud first, then reveal.
Few-shot or fine-tuning for a ticket classifier?Show answer
I start with few-shot because iteration is nearly free: a zero-shot baseline on a labeled eval set, then 3–5 examples aimed at the boundaries where it fails, then dynamic retrieval if inputs are diverse. I move to fine-tuning when I have thousands of labeled tickets and high volume, so the examples cost more per call than a small tuned model would, or when I need latency a long prompt can't give. The trade-off is iteration speed against per-call cost and consistency.
In production the outputs keep looking like one of your examples. Why, and how do you fix it?Show answer
The model is copying surface features instead of the underlying rule: same length, same phrasing, sometimes the same entities. That happens when examples are too uniform or too few. I diversify them on the dimensions that shouldn't matter, balance labels, shuffle the order, and state that the examples illustrate format, not content. Then I re-run the eval and compare output-length and label distributions with production, because that's where the copying shows up.