Few-shot prompting: learning with examples

Published by the LLMNet Developer & Prompt-Engineering Community • Reading time: approx. 5 minutes

When developing applications with Large Language Models (LLMs), you will sooner or later run into the limitations of standard zero-shot prompting. You send an instruction, but the model misses just that piece of context or specific output format that is crucial for your production environment. This is where few-shot prompting comes in: a technique where you explicitly feed the model a number of input-output examples before asking the actual question.

In this guide, we dive deep into how to optimally use few-shot prompting, what to look out for when selecting examples, how many examples you need, and which pitfalls you as a developer should avoid.

What exactly is Few-shot Prompting?

Few-shot prompting is based on the principle of in-context learning. Instead of retraining or fine-tuning the model (which is time-consuming and expensive), you demonstrate directly in your prompt what you expect. You show the model a pattern using concrete cases.

A standard prompt only gives a task description. A few-shot prompt adds representative pairs of input and expected output to that. The model recognizes the underlying format and applies it directly to the final question.

How do you choose high-quality examples?

The quality of your output directly depends on the quality of your examples. Random examples often backfire. Keep the following guidelines in mind:

Bad example (Unclear and inconsistent structure)

Context: You want to classify customer reviews as Positive, Neutral, or Negative.

Classify the sentiments:
- This product is great! -> Positive
- Meh, it's okay.
- Bad junk.

Why this goes wrong: The examples are inconsistent (some completely lack the classification) and the model does not receive clear instructions on the desired format for the edge cases.

Good example (Consistent, structured, and predictive)

Context: The same classification task, but now robustly set up.

Classify the sentiment of the product review below as [Positive, Neutral, Negative]. Return only the label.

Example 1:
Review: "The battery life is very disappointing, empty after just one day."
Label: Negative

Example 2:
Review: "Does what it's supposed to do, no fuss."
Label: Neutral

Example 3:
Review: "Fantastic customer service and super fast delivery!"
Label: Positive

Current task:
Review: "Packaging was damaged, but fortunately the device works fine."
Label:

Why this works: The structure is crystal clear, the labels are unambiguous, and the model immediately sees which expected format it needs to generate after the colon of the current task.

How many examples do you need?

There is no holy grail, but for most LLMs, the sweet spot is 3 to 5 examples.

Common Pitfalls

Even experienced developers sometimes fall into these classic pitfalls with few-shot prompting:

  1. Instruction contamination: Unclear separation of the examples and the actual question. Use clear section headers like ### Examples and ### Question.
  2. Overly complex examples: Examples that contain sub-explanations or exceptions themselves confuse the model. Keep each example short and atomic.
  3. Token waste: Including extremely long blocks of text in your examples when a concise summary would suffice. This slows down inference time and unnecessarily drives up your costs.