Skip to main content
Few-shot learning is a powerful prompting technique where you provide examples of the desired input-output pattern before making your actual request. This approach dramatically improves response quality, consistency, and accuracy by showing the AI exactly what you want.

What is few-shot prompting?

Few-shot prompting involves including one or more examples in your prompt to demonstrate the pattern, style, format, or approach you want the AI to follow.
The term “few-shot” refers to the number of examples:
  • Zero-shot: No examples (just instructions)
  • One-shot: One example
  • Few-shot: Multiple examples (typically 2-5)
  • Many-shot: Many examples (6+)

Why few-shot prompting works

Examples are often more precise than descriptions. Consider these approaches:
Response may vary in interpretation.
Few-shot prompting is especially effective for tasks involving specific formats, consistent styling, or domain-specific conventions.

Basic few-shot structure

Single example (one-shot)

Use one example for simple pattern demonstration:

Multiple examples (few-shot)

Use 2-5 examples for complex patterns:

Few-shot prompting by use case

Code generation

Demonstrate coding style, structure, and conventions:

Data transformation

Show the exact transformation pattern:

Content writing

Demonstrate tone, style, and structure:

Test case generation

Show test structure and coverage patterns:

Advanced few-shot techniques

Progressive complexity

Start with simple examples and increase complexity:

Contrastive examples

Show both correct and incorrect patterns:

Domain-specific examples

Provide examples using domain terminology:

Few-shot with structured prompts

Combine few-shot learning with structured techniques:

Optimizing example quality

1

Use diverse examples

Cover different variations and edge cases in your examples to show the full range of expected patterns.
2

Keep examples realistic

Use realistic, production-quality examples rather than overly simplified ones.
3

Annotate when helpful

Add comments or explanations to examples when the pattern might not be obvious:
4

Balance quantity vs. quality

2-3 high-quality examples usually work better than 10 mediocre ones. More examples aren’t always better.

When to use few-shot prompting

  • Generating code in a specific style or framework
  • Data transformation with consistent formatting
  • Content creation matching a particular tone
  • Test case generation with consistent structure
  • API responses following a standard format
  • Documentation with specific templates
  • Naming conventions and style guides
  • Well-known standard formats (JSON, CSV)
  • Common programming tasks with clear conventions
  • Simple, straightforward requests
  • When zero-shot instructions are sufficient

Token efficiency considerations

Examples consume tokens. Balance thoroughness with efficiency:
Token management tips:
  • Use 2-3 examples for most cases (sweet spot for quality vs. tokens)
  • Reference documentation or specifications for very complex patterns rather than including many examples
  • Consider creating an assistant with embedded examples for repeated use
  • See tokens and limits for more optimization strategies

Combining with other techniques

Few-shot prompting works well with other approaches:

Saving and reusing examples

Make few-shot patterns reusable:
  1. Custom instructions: Add frequently used examples to custom instructions
  2. Assistants: Create specialized assistants with embedded examples for specific tasks
  3. Prompt library: Maintain a collection of effective few-shot prompts

Next steps

Custom instructions

Save few-shot patterns for reuse

Structured techniques

Combine with structured prompting

Create assistants

Build assistants with embedded examples

Token optimization

Manage example token usage