Few-shot prompting is a powerful technique for guiding AI models. A core skill in What Is Prompt Engineering? A Practical Introduction, it involves showing a model what to do, rather than just telling it. With few shot prompting, you include examples directly in your prompt to guide the model's output format, style, and logic.

The following sections explain how to apply this technique systematically for more reliable results from large language models (LLMs).

Key takeaways

  • Few-shot prompting uses in-prompt examples, also called demonstrations, to guide a model’s response for a specific task.
  • It's a form of in-context learning, which is pattern matching, not model retraining (fine-tuning).
  • The quality, relevance, and diversity of your examples often matter more than the raw number of examples you provide (What Makes Good In-Context Examples for GPT-3?).
  • The optimal number of examples is a trade-off between task complexity, context length limits, and cost.
  • Systematic selection and consistent formatting of examples are critical for reliable performance, especially for structured outputs like JSON.

What Is Few-Shot Prompting?

Few-shot prompting guides a language model by including several demonstrations (input-output pairs) in the prompt before the final query. This process, also known as in-context learning, shows the model the pattern you want it to follow without changing its internal parameters (AWS prompt engineering guidelines). It helps the model understand the desired output format, style, or reasoning process more effectively than instructions alone.

Think of it like teaching someone a new task. You could give them a list of abstract rules (instructions), or you could show them three completed examples and then hand them a new one to do. The second approach is often faster and clearer. The examples provide context that clarifies ambiguity in the instructions.

This technique is fundamentally different from fine-tuning.

  • Few-shot prompting happens at inference time, providing examples within a single API call. The model doesn't "learn" in a permanent way; it simply uses the examples as a guide for that one generation.
  • Fine-tuning is a separate process where a model's internal weights are updated by training it on a large dataset. Fine-tuning creates a new, custom version of the model.

How Few-Shot Prompting Works: A Step-by-Step Guide

Creating a good few-shot prompt is a systematic process of defining your task and carefully selecting demonstrations. Here is a five-step method for building an effective few-shot prompt.

1. Define the Task and Output Format

Start by being crystal clear about your goal. What is the model supposed to do? What should the final output look like? For a classification task, define your exact labels (e.g., SPAM, NOT_SPAM). For an extraction task, define the JSON schema you expect. This clarity is the foundation of your prompt.

2. Select Diverse and Relevant Examples

Choose a small set of input-output pairs that demonstrate the task. These examples should be structurally similar to the real inputs you expect the model to handle (Research on example selection). They should also cover a diverse range of cases. If you are classifying customer feedback, include positive, negative, and neutral examples.

3. Format Examples Consistently

Consistency is crucial. Use the same structure and delimiters for every example. If you start an input with Text: and the output with Label:, maintain that format for all demonstrations and for the final query. Inconsistent formatting confuses the model and leads to unreliable results.

4. Construct the Full Prompt

Assemble the prompt in a logical order:

  1. Instruction: A brief, clear instruction for the task.
  2. Examples: Your formatted input-output pairs.
  3. New Input: The final query you want the model to process, followed by the label that prompts the answer (e.g., Label:).
Code
Classify the following customer reviews into one of three categories: POSITIVE, NEUTRAL, or NEGATIVE.

Review: "The product works exactly as advertised. I'm very happy with it."
Classification: POSITIVE

Review: "It's fine. Not great, not terrible. It does the job."
Classification: NEUTRAL

Review: "The package arrived damaged and the item was broken."
Classification: NEGATIVE

Review: "I can't believe how fast the shipping was! Amazing."
Classification:

5. Test and Refine

Evaluate your prompt on a separate set of test cases—inputs the model has not seen in its prompt examples. If the output is incorrect or poorly formatted, analyze the failures. Do you need a more diverse set of examples? Is one of your examples ambiguous? Adjust your examples or instructions and test again. Research shows performance is sensitive to example selection, so refinement is key (Study on active example selection).

Zero-Shot vs. One-Shot vs. Few-Shot Prompting

A prompt with no examples is called zero-shot, while a prompt with one or more examples is considered few-shot (Google's prompt design guide). The terminology helps distinguish how much context you provide to the model.

Prompting Type Number of Examples Definition When to Use It
Zero-Shot 0 Provides only an instruction and a query. Relies on the model's pre-existing knowledge. Simple, common tasks that the model already understands well (e.g., summarizing a short text).
One-Shot 1 Provides a single example to demonstrate the task or format (AWS prompt engineering guidelines). When you need to clarify a specific format or disambiguate a simple instruction.
Few-Shot 1+ Provides one or more examples to show a pattern, often used to mean two or more to distinguish from one-shot prompting. Complex classification, strict formatting requirements, or tasks with subtle distinctions.

Start with zero-shot. If it fails, move to one-shot. If it is still inconsistent, use few-shot prompting to provide more comprehensive guidance.

Worked Example: Good vs. Bad Few-Shot Prompts

Simply adding examples is not enough; their quality and consistency determine success. Let's compare a bad few-shot prompt to a good one for a sentiment analysis task.

Zero-Shot Attempt (Often Inconsistent)

A simple instruction might work, but the output format can be unpredictable.

Prompt:

Code
What is the sentiment of this review?
"The camera quality is incredible!"

Possible Outputs:

  • Positive
  • The sentiment is positive.
  • pos

The model understands the task but returns differently formatted answers.

Bad Few-Shot Prompt (Confusing)

This prompt adds examples, but they are inconsistent and irrelevant, which confuses the model.

Prompt:

Code
Analyze the sentiment.

Text: "I love this phone."
Sentiment: positive

Text: "The battery dies so fast."
Sentiment: NEGATIVE

Text: "Paris is the capital of France."
Sentiment: neutral

Text: "The camera quality is incredible!"
Sentiment:

This prompt is bad because:

  • Inconsistent Casing: It uses positive and NEGATIVE.
  • Irrelevant Example: "Paris is the capital of France" is a fact, not an opinion, making neutral an ambiguous label in this context.

The model might copy the inconsistent casing or get confused by the irrelevant example, leading to unreliable output.

Good Few-Shot Prompt (Clear and Consistent)

This prompt uses clear, consistent, and relevant examples to guide the model effectively.

Prompt:

Code
Classify the review's sentiment as POSITIVE, NEUTRAL, or NEGATIVE.

Review: "I absolutely love this new phone, it's perfect!"
Sentiment: POSITIVE

Review: "The phone is okay, but the battery life could be better."
Sentiment: NEUTRAL

Review: "I'm so disappointed. The screen cracked after just one week."
Sentiment: NEGATIVE

Review: "The camera quality is incredible!"
Sentiment:

Expected Output:

  • POSITIVE

This prompt works well because every example reinforces the same strict rules: use one of three all-caps labels, and follow the Review: ... Sentiment: format.

How Do You Select Good Few-Shot Examples?

Choosing the right examples is the most important part of few-shot prompting. Randomly selected examples can lead to unstable results (Study on active example selection). Use the following criteria to select high-quality demonstrations.

  • Relevance: Are the examples structurally similar to the new input? For extracting information from invoices, your examples should be snippets of invoices, not news articles.
  • Diversity: Do the examples cover a range of expected inputs and desired outputs? For a classification task, include at least one example for each label if possible.
  • Clarity: Is each example an unambiguous, correct pairing of input and output? Avoid examples that could be interpreted in multiple ways or that contain errors.
  • Simplicity: Are the examples easy to understand? Overly complex examples with irrelevant details can obscure the core pattern you want the model to learn (Research on example selection).
  • Balance: Are your examples balanced across different categories? Showing four positive examples and only one negative example might bias the model toward predicting "positive."

By deliberately selecting examples based on these criteria, you move from guessing to engineering a reliable prompt. This is especially vital when you need consistent, LLM Structured Output JSON: A Practical Guide.

How Many Examples Are Enough?

There is no single magic number of examples that works for every task. The ideal count depends on task complexity, model capability, and context window limits (Paper on self-adaptive in-context learning). For instance, guidance for one on-device model suggests using between 2 and 15 examples (Apple's developer documentation).

Start with a few (e.g., three) high-quality, diverse examples and test performance. Only add more if they introduce important new variations or cover common failure cases.

Be aware that more is not always better. Adding too many examples can have negative effects:

  • Increased Cost and Latency: More examples consume more tokens, which increases the cost of each API call and the time it takes to get a response.
  • Reduced Accuracy: In some cases, too many examples can cause the model to overfit to the specific demonstrations rather than the general task, hurting its performance on new inputs. One study found instances where using three well-chosen examples produced better results than four (Research on example selection via data compression).
  • Context Window Limits: Every model has a maximum context window, or the total amount of text it can process at once. Too many examples can leave no room for your actual query and the model's response.

Common Mistakes and Limitations

Few-shot prompting is powerful, but it's not a silver bullet. Be aware of these common mistakes and inherent limitations.

Common Mistakes

  • Inconsistent Formatting: Using different delimiters, label names, or capitalization across examples confuses the model.
  • Biased or Unbalanced Examples: Only showing one type of outcome can heavily bias the model's predictions.
  • Accidental Patterns: Creating a false pattern with example ordering (e.g., always having the last example be POSITIVE) can influence the model's final answer.
  • Overly Complex Examples: Including irrelevant information in examples can make it harder for the model to identify the core pattern.

Limitations

  • Context Window: The number and length of examples are physically limited by the model's context window.
  • No New Knowledge: In-context learning only shows the model how to format or apply its existing knowledge. It cannot teach the model new facts or capabilities it doesn't already possess.
  • Task Suitability: Example selection strategies that work for simple classification may not work for complex reasoning or open-ended creative generation (Paper on self-adaptive in-context learning).
  • Sensitivity to Selection: As mentioned, model performance can be highly sensitive to which examples are chosen and in what order, making it feel "brittle" at times (Study on active example selection).

What to learn next

  • Prompt Versioning: A Guide to Testing & Safety - Learn a complete, Git-based workflow for prompt versioning. Track changes to your prompts, models, and settings to build safe and reliable A Once familiar with few-shot prompting, users can explore more advanced methods to handle complex reasoning and ensure greater reliability.
  • Chain-of-Thought Prompting: Combine few-shot examples with step-by-step reasoning to improve performance on tasks that require logic, such as math problems or multi-step questions.
  • Structured Output Control: For tasks requiring perfectly formatted JSON or other structured data, explore how modern APIs can enforce an output schema, often in combination with few-shot examples.
  • Dynamic Example Selection: Instead of hard-coding the same examples in every prompt, advanced systems can dynamically retrieve the most relevant examples from a large library based on the user's current query.

Sources and further reading