Few-shot examples are a technique for improving the performance of machine learning models—particularly language models—by providing a small set of input-output pairs (typically 3 to 10) alongside a new input. They are most effective when the model has not seen the task before, or when the task is complex and subtle. This approach is used during inference, especially in zero-shot or few-shot prompting, and is essential when deploying models that must generalize across diverse, unstructured, or evolving data.

When to Use Few-Shot Examples

Few-shot examples are appropriate when you have a model that can accept natural language or structured input but lacks explicit training on a specific task. The model is not fine-tuned, or fine-tuning is too costly or infrequent. Instead, the task is described implicitly through examples embedded in the input.

Use few-shot examples when:

  • The model is expected to perform a task it has not been trained on (e.g., summarizing technical documentation from a domain it has seen only in examples).
  • The task has nuanced requirements that are hard to encode in a prompt alone (e.g., formatting a medical report with specific section headers and metadata).
  • The input data varies significantly across clients or domains, and a single prompt does not suffice.
  • You are building a service that must handle a wide variety of input types, and you want to minimize the need for retraining.

Constructing a Few-Shot Example Set

A few-shot example set consists of a sequence of input-output pairs, each clearly separated and structured. The input is the raw data, and the output is the desired transformation or prediction.

Each example must be complete: it includes all necessary context for the model to learn the task.

Format of an Example

Each example is a pair:

  • Input: The raw data the model must process.
  • Output: The expected result.

The input and output are typically separated by a delimiter such as --- or ###. The full set is prepended to the new input.

Input: The following is a user query from a customer support system.

Query: "I need to reset my password but I can't access the login page. The error says 'Connection timed out.' What should I do?"

Output: 
- Step 1: Verify network connectivity (ping the server).
- Step 2: Clear browser cache and cookies.
- Step 3: Try accessing the login page from a different device.
- Step 4: Check the server logs for the timeout duration.
- Step 5: Contact support with the error details.

---

Input: A product description for a new tablet.

Description: "The NovaTab X3 is a lightweight 10-inch tablet with 128GB storage, 8-hour battery, and a 12MP camera. It runs on the latest Android 14 and includes a stylus with pressure sensitivity."

Output:
- Product: NovaTab X3
- Size: 10 inches
- Storage: 128 GB
- Battery Life: 8 hours
- Camera: 12 MP
- OS: Android 14
- Accessory: Stylus with pressure sensitivity

---

Input: A clinical note from a patient visit.

Note: "Patient reports fatigue and dizziness. Blood pressure 130/85 mmHg, heart rate 92 bpm. Ordered CBC and lipid panel. Suspected anemia."

Output:
- Diagnosis: Anemia (suspected)
- Symptoms: Fatigue, Dizziness
- Vital Signs: BP 130/85 mmHg, HR 92 bpm
- Tests Ordered: CBC, Lipid Panel
- Next Steps: Evaluate iron levels, consider ferritin test

---

Now process this new input:

This format ensures that the model learns both the structure and the intent of the task from the examples.

Embedding Examples in Model Input

The few-shot examples are embedded directly into the model’s input sequence. The model processes the entire string as a single sequence, where the examples are part of the context.

Common Input Structures

The most common way to structure the input is to interleave examples with delimiters and the new query.

{
  "prompt": "Translate the following text into French.\n\n---\n\nInput: Hello, how are you?\nOutput: Bonjour, comment allez-vous?\n\n---\n\nInput: I love programming with Python.\nOutput: J'adore programmer avec Python.\n\n---\n\nInput: The weather is sunny today.\nOutput: Il fait ensoleillé aujourd'hui.\n\n---\n\nInput: {{new_text}}\nOutput:"
}

The {{new_text}} placeholder is replaced with the actual input at inference time.

When using a model via an HTTP API, this prompt is sent in the prompt field.

POST /v1/completions
{
  "model": "gpt-3.5-turbo",
  "prompt": "Translate the following text into French.\n\n---\n\nInput: Hello, how are you?\nOutput: Bonjour, comment allez-vous?\n\n---\n\nInput: I love programming with Python.\nOutput: J'adore programmer avec Python.\n\n---\n\nInput: The weather is sunny today.\nOutput: Il fait ensoleillé aujourd'hui.\n\n---\n\nInput: The sun is shining brightly.\nOutput:",
  "max_tokens": 100,
  "temperature": 0.7
}

The model returns the translation of the new input.

Selecting Effective Examples

Choosing the right examples is critical. Poor examples lead to models that generalize poorly or make inconsistent predictions.

Example Selection Criteria

  • Representative: Each example should capture a different aspect of the task.
  • Diverse: Examples should vary in length, domain, and complexity.
  • Clear and Complete: Examples must be self-contained; no hidden assumptions.
  • Consistent Format: All examples follow the same structure (e.g., same key names, same output format).
  • Contrastive: Examples should contrast common cases (e.g., one with a missing field, one with an edge case).

Common Pitfalls

  • Overfitting to Examples: The model replicates the example outputs verbatim instead of generalizing.
  • *Fix*: Use varied input formats across examples.
  • Redundant Examples: Multiple examples that teach the same pattern.
  • *Fix*: Prune examples to remove overlapping knowledge.
  • Inconsistent Output Formats: Some outputs use bullet points, others use paragraphs.
  • *Fix*: Standardize output format across all examples.
  • Too Many Examples: The model becomes overwhelmed by context, especially in models with limited context windows.
  • *Fix*: Limit examples to 5–8, and prioritize the most critical ones.

Handling Input Variability

Few-shot examples must account for variability in input structure. The model may receive data in different formats: JSON, plain text, XML, CSV.

Example: Processing Semi-Structured Text

Input: 
Name: Alice Johnson
Email: [email protected]
Phone: +1-555-123-4567
Address: 123 Main Street, Anytown, CA 90210
Order ID: 88291
Items: 
  - Laptop, $999
  - Mouse, $45
  - Keyboard, $75
Total: $1,119
Status: Shipped

Output:
- Customer: Alice Johnson
- Contact: [email protected], +1-555-123-4567
- Address: 123 Main Street, Anytown, CA 90210
- Order ID: 88291
- Items:
  - Laptop ($999)
  - Mouse ($45)
  - Keyboard ($75)
- Total: $1,119
- Status: Shipped

This example teaches the model to extract structured data from a freeform block of text, even when the input is inconsistent.

When the model receives a new input with slightly different formatting—e.g., the address is on a single line, or the items are comma-separated—the few-shot examples guide it to produce a consistent output.

Failure Modes and Common Errors

Few-shot examples can fail silently. The model produces plausible outputs, but the results are off in subtle ways.

Silent Failures

  • Model Reads Only the First Example: The model focuses on the first few examples and ignores later ones.
  • *Symptom*: Outputs are accurate for the first task but miss nuances from later examples.
  • *Diagnosis*: Add a # Example N header to each example to improve attention.
  • Example Overlap: The model confuses inputs across examples because the structure is too similar.
  • *Symptom*: A new input is processed using an example that is semantically close but not identical.
  • *Fix*: Use unique keywords in each example input (e.g., “Example 1: Customer Query”, “Example 2: Product Description”).
  • Context Window Overflow: The examples are too long or too many, pushing the new input into the model’s context limit.
  • *Symptom*: The model forgets details from the beginning of the input.
  • *Fix*: Use max_tokens and truncate the input to fit within the model’s context window.
  • Missing Output Delimiters: The model fails to parse the expected output format because the delimiter between input and output is inconsistent.
  • *Symptom*: The output is a single paragraph instead of a structured list.
  • *Fix*: Use a consistent delimiter like --- or ### and preface the output with a clear label.

Optimizing the Few-Shot Pipeline

Few-shot examples are not static. They evolve with use.

Dynamic Example Selection

Instead of using a fixed set of examples, use a retrieval system to select the most relevant examples at inference time.

  • Use a vector database (e.g., FAISS, Weaviate) to store embeddings of past examples.
  • For a new input, compute its embedding and retrieve the top-k most similar examples.
  • Concatenate the retrieved examples into the prompt.

This approach scales better and adapts to new domains.

Example: Retrieval-Augmented Few-Shot Prompting

{
  "prompt": "Based on the following examples, extract structured data from the input.\n\n---\n\n{{retrieved_examples}}\n\n---\n\nInput: {{new_input}}\nOutput:"
}

Each retrieved_examples is formatted as a block of input-output pairs, selected dynamically.

Configuring the Model for Few-Shot Use

The model’s hyperparameters must be tuned for few-shot scenarios.

ParameterValueReason
temperature0.7Encourages diversity in output without overfitting to examples.
max_tokens100–200Sufficient for structured outputs with moderate length.
top_p0.9Allows more exploration of output options.
frequency_penalty0.5Favors more diverse word usage across examples.
presence_penalty0.3Encourages use of new terms not seen in examples.

These settings balance the model’s ability to generalize from the examples while remaining faithful to the task.

Debugging Few-Shot Performance

When few-shot results are suboptimal, use these steps:

  1. Inspect the Full Input: Log the entire prompt sent to the model.
  2. Check Example Order: Ensure the model sees the examples in the correct sequence.
  3. Compare Predictions to Examples: Use a metric like BLEU or exact match to evaluate how well the model follows the example patterns.
  4. Visualize Attention: Use attention visualization tools to identify which examples the model focuses on for each part of the output.

If the model consistently ignores the last few examples, consider adding explicit labels (e.g., “Example 1:”, “Example 2:”) or using a more prominent delimiter (e.g., ===).

Summary

Few-shot examples are a lightweight yet powerful technique for guiding models through new tasks. They rely on a few well-chosen input-output pairs embedded directly into the model input. The success of the approach depends on:

  • Clear, diverse, and representative examples.
  • Consistent formatting across all examples.
  • Proper configuration of model hyperparameters.
  • Attention to failure modes like context overflow and silent misalignment.

By treating the few-shot example set as a first-class component of the pipeline—configurable, retrievable, and debuggable—teams can achieve high performance with minimal training effort.

This page was rewritten on 11 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.