TExcelTAD
Home › Generative AI & LLMs › Zero-shot & Few-shot

Zero-shot & Few-shot

Large language models can learn a task from the prompt alone. Give an instruction with no examples and you are prompting zero-shot. Add a few input → output examples and you are prompting few-shot. This ability, called in-context learning, is why one general model can do thousands of different tasks without retraining.

Zero-shot

text
Classify the sentiment of the review as positive, negative or mixed.
Review: "Fast delivery, but the colour is not what the photo showed."
Sentiment:

Modern instruction-tuned models handle tasks like this well zero-shot. Start here: it is the shortest prompt, and you add examples only when results are inconsistent.

Few-shot

Examples are the most direct way to show format, style and edge cases. They often work better than paragraphs of description:

python · try it yourself
examples = [
    ("Arrived quickly and works perfectly.", "positive"),
    ("Broke after two days. Very disappointed.", "negative"),
    ("Great sound, but the battery barely lasts an hour.", "mixed"),
]

def few_shot_prompt(examples, new_input):
    lines = ["Classify the sentiment of each review as positive, negative or mixed.", ""]
    for text, label in examples:
        lines += [f"Review: {text}", f"Sentiment: {label}", ""]
    lines += [f"Review: {new_input}", "Sentiment:"]
    return "\n".join(lines)

print(few_shot_prompt(examples, "Fast delivery, but the colour is not what the photo showed."))
Classify the sentiment of each review as positive, negative or mixed.

Review: Arrived quickly and works perfectly.
Sentiment: positive

Review: Broke after two days. Very disappointed.
Sentiment: negative

Review: Great sound, but the battery barely lasts an hour.
Sentiment: mixed

Review: Fast delivery, but the colour is not what the photo showed.
Sentiment:

The prompt ends with Sentiment: so the most natural continuation is a single label. A typical model replies mixed — matching the third example's pattern.

Tips for choosing examples

Tip Why
Cover every label / case the model can't copy a pattern it never saw
Vary the examples identical-looking examples make it copy surface details (length, words)
Mix the order if all examples end with the same label, that label can be over-predicted
Use realistic inputs examples should look like real traffic, including the hard cases
Keep the format identical consistent formatting is part of what the model learns
Put them in tags e.g. <example>…</example>, so they are not confused with the real input

Dynamic few-shot: pick the most relevant examples

With a large pool of labelled examples you don't have to send them all. Pick the ones most similar to the new input. Here similarity is simple word overlap (Jaccard); real systems use embeddings:

python · try it yourself
pool = [
    ("The battery dies within an hour.", "negative"),
    ("Battery life is excellent, lasts all week.", "positive"),
    ("Screen is bright and sharp.", "positive"),
    ("The screen cracked on day one.", "negative"),
    ("Nice screen, but the battery is weak.", "mixed"),
    ("Delivery was late and the box was damaged.", "negative"),
]

def words(text):
    return {w.strip(".,!").lower() for w in text.split()}

def jaccard(a, b):
    return len(a & b) / len(a | b)

def select_examples(query, pool, k=2):
    q = words(query)
    scored = sorted(pool, key=lambda ex: -jaccard(q, words(ex[0])))
    return scored[:k]

query = "How long does the battery last? Mine dies quickly."
for text, label in select_examples(query, pool):
    print(f"{label:<9} {text}")
negative  The battery dies within an hour.
mixed     Nice screen, but the battery is weak.

Why does in-context learning work?

During pretraining the model saw countless documents that contain patterns: lists of questions and answers, tables, translations side by side. Continuing a pattern is next-token prediction. Few-shot examples set up a pattern; the model continues it.

A useful mental model (a simplification) is a nearest-neighbour classifier: the examples act like reference points. The toy below labels new reviews by their closest example — no training step, just examples supplied at "prompt time":

python · try it yourself
examples = [
    ("love it works great", "positive"),
    ("terrible broke fast", "negative"),
    ("great sound but weak battery", "mixed"),
]

def predict(text):
    w = set(text.split())
    best = max(examples, key=lambda ex: len(w & set(ex[0].split())))
    return best[1]

for review in ["works great love the colour", "broke after a week terrible", "great screen but weak speakers"]:
    print(f"{predict(review):<9} <- {review}")
positive  <- works great love the colour
negative  <- broke after a week terrible
mixed     <- great screen but weak speakers

A real LLM is far more flexible — it understands paraphrases and combines examples with its general knowledge — but the analogy explains why example choice matters so much.

Tip: Few-shot examples cost tokens on every request. If you need hundreds of examples to get a task right, consider fine-tuning or a classic classifier instead.

Test yourself

What is a zero-shot prompt?

  1. A prompt with no instruction
  2. A prompt with an instruction but no worked examples
  3. A prompt with temperature 0
  4. A prompt that has zero tokens

All your few-shot examples are labelled 'positive' except one. What is a likely problem?

  1. The prompt becomes invalid JSON
  2. The model may be biased towards predicting 'positive'
  3. The model ignores the examples
  4. Nothing — label balance never matters

What does 'dynamic few-shot' mean?

  1. Changing the temperature per request
  2. Selecting the examples most relevant to each new input from a larger pool
  3. Generating examples with random labels
  4. Using examples only for the first request