Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Techniques: Few-shot, Chain-of-Thought and Roles

Zero-Shot and Few-Shot Prompting

Learn when to give a model no examples (zero-shot) or a few (few-shot), how many to use, and how to build few-shot prompts programmatically in Python.

The most common way to improve a prompt without making it longer in words is to add examples. Prompting techniques are usually named by how many examples they include.

  • Zero-shot: instructions only, no examples.
  • One-shot: exactly one example.
  • Few-shot: a handful of examples (typically two to eight).

"Shot" here just means "example". Nothing is trained: the model does in-context learning, adapting to the pattern in the prompt for this one request.

Zero-shot

Modern instruction-tuned models handle many tasks zero-shot if the instruction is clear.

TEXT
Classify the sentiment of the review as positive, negative or neutral.
Reply with one word.

Review: "The battery lasts all day, but the screen scratches easily."

Use zero-shot when the task is common and unambiguous, the format is simple, and you want the shortest prompt. It is also your baseline: always try zero-shot first so you know whether examples are worth their cost.

Few-shot

Add examples when instructions alone leave room for interpretation: unusual formats, custom labels, a specific tone, or edge cases a description cannot capture.

TEXT
Classify the sentiment of each review as positive, negative or mixed.

Review: "Fast delivery, works perfectly."
Label: positive

Review: "Arrived broken and support never replied."
Label: negative

Review: "Great screen, terrible battery."
Label: mixed

Review: "The battery lasts all day, but the screen scratches easily."
Label:

Three things happen. The model learns the label set, the output format (one word after Label:), and the decision boundary (a review with praise and complaint is "mixed"). The prompt ends mid-pattern, nudging the model to complete it with just the label.

flowchart LR
  A["Instruction"] --> B["Example 1"]
  B --> C["Example 2"]
  C --> D["Example 3"]
  D --> E["New input<br/>(pattern left open)"]
  E --> F["Model completes<br/>the pattern"]

A few-shot prompt: instruction, worked examples, then the new input in the same shape.

Build few-shot prompts in code

Hard-coding examples inside a string gets messy fast. Keep them as data and format them with a function.

Python A few-shot prompt builder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the builder takes the examples as an argument, you can test with zero, one, or five examples and compare.

How many examples?

There is no magic number. A practical approach:

  1. Start with zero-shot. Measure on your test set.
  2. If it fails, add two or three varied examples. Measure again.
  3. Add more only if it keeps helping. Returns diminish, and each example costs tokens on every call.

More examples can also hurt: they may anchor the model too strongly on surface details, or crowd out space for the input. See choosing good examples for how to pick them.

A stand-in for the effect

To show why examples constrain output, here is a toy stand-in that mimics few-shot behavior: it reads the labels used in examples and answers by similarity of words. It is not a real model, but it behaves like one in a key way: with no examples, it has nothing to go on.

Python Why examples help (toy nearest-example stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real mechanism is far richer, but the intuition holds: examples define the label vocabulary and the notion of "closest".

Format consistency matters

The model copies the shape of your examples. Keep them identical in structure: same labels, same punctuation, same spacing. If three examples end with a period and one does not, expect inconsistent output. When examples include the output format (JSON, a table row), that format is much more likely to appear in the answer.

When to prefer zero-shot

  • The task is generic (translation, summarization, simple classification).
  • You are using a strong model and want minimal tokens.
  • Your examples would leak private data.
  • You cannot write representative examples yet. Start zero-shot, collect real failures, and turn those into examples.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does "few-shot" mean?

    Few-shot prompting puts examples in the prompt itself; nothing is trained.
  2. 2. Which should you try first?

    A zero-shot baseline shows whether examples are worth the added tokens.
  3. 3. What can a few-shot prompt teach besides the answer itself?

    Examples show the labels, the format and where the lines fall between categories.
  4. 4. Why keep examples identical in structure?

    Models imitate patterns. Consistent examples produce consistent output.

Summary

  • Zero-shot uses instructions only; few-shot adds a handful of examples; nothing is trained, it is in-context learning.
  • Start zero-shot, add varied examples only if the baseline fails, and measure.
  • Build few-shot prompts from example data with a function so you can vary them.
  • Keep examples consistent in format because the model imitates their shape.

Next: Choosing Good Examples