Few-shot prompting: learning with examples
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:
- Cover the edge cases: Make sure you don't just include simple 'happy path' examples, but also show how the model should respond to incomplete data, spelling errors, or unusual input.
- Maintain a consistent format: Use exactly the same delimiters (such as
Input:andOutput:, or Markdown code blocks) in all your examples and in the final question. - Be realistic: Use data that closely resembles the real user data your application will process later on.
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.
- 1 to 2 examples (1-shot / 2-shot): Sufficient for very simple format adjustments (e.g., "Convert this date to ISO format").
- 3 to 5 examples (Few-shot): Optimal for complex classifications, sentiment analysis, data extraction, or specific tone-of-voice styles.
- More than 10 examples: Often not recommended. You run the risk of the recency effect (where the model leans too heavily on the last example) and you waste valuable context window tokens. If you need more than 10 examples, you might want to consider fine-tuning or working with dynamic few-shot retrieval (RAG).
Common Pitfalls
Even experienced developers sometimes fall into these classic pitfalls with few-shot prompting:
- Instruction contamination: Unclear separation of the examples and the actual question. Use clear section headers like
### Examplesand### Question. - Overly complex examples: Examples that contain sub-explanations or exceptions themselves confuse the model. Keep each example short and atomic.
- 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.
Further reading on LLMNet
Want to dive deeper into optimizing your AI architecture and testing model performance? Then check out these sections within our network:
- Deepen your skills via our central Learning & Tutorials portal for advanced prompt architecture.
- Compare the performance of different models on our Benchmark hub to see which model responds most reliably to few-shot prompts.