Syllabus Lesson 121 of 239 · Prompt Engineering for AI Engineers
Prompt Engineering for AI Engineers

Zero-Shot vs Few-Shot: When to Show Examples

Zero-shot means you just describe the task. Few-shot means you also paste in a handful of worked examples before the real input. Few-shot usually lifts quality, but it costs tokens and latency on every single call, so it is a judgment, not a reflex.

The rule that holds up in production:

  • If you have no examples to show, you are zero-shot by definition. Nothing to add.
  • If the task needs a specific output format (a precise JSON shape, a fixed label set), examples are the cheapest way to pin it down. Go few-shot.
  • If the task is a fuzzy pattern a description struggles to capture (tone, edge-case classification, style), examples teach it better than prose. Go few-shot.
  • Otherwise, a clear instruction is enough. Stay zero-shot and save the tokens.

A few-shot block, for intuition, just stacks examples ahead of the query:

Classify the sentiment.
Text: I love it -> positive
Text: total waste -> negative
Text: it is fine -> ?

You will write the decision layer that picks the strategy. Build choose_strategy(task) where task is a dict with n_examples_available (an int), needs_specific_format (a bool), and is_fuzzy_pattern (a bool). Return the string "zero-shot" or "few-shot".

Order matters: no examples always means zero-shot, even if the other flags are true, because you literally have nothing to show. Only when examples exist do the format and fuzziness flags push you to few-shot. Press Run to grade.

Your turn

Write choose_strategy(task) returning "zero-shot" or "few-shot" from a dict with keys n_examples_available (int), needs_specific_format (bool), is_fuzzy_pattern (bool). If no examples are available, always return "zero-shot". Otherwise return "few-shot" when the task needs a specific format OR is a fuzzy pattern, and "zero-shot" when it is neither.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output