Prompt Engineering
Intermediate 11 min read Prompt Engineering

Few-Shot Examples

Show, don't tell

A few worked examples in the prompt teach format, tone and edge-case behavior far better than prose. Standouts: quality examples beat quantity, and bad examples are contagious.

few-shotexamplesin-context learningdemonstrationsexemplars
Prerequisites: Prompting Basics
Before we start

What you will learn

  • Select high-quality, representative examples
  • Cover realistic edge cases explicitly
  • Measure example-set changes
Introduction

What is this?

Few-shot prompting puts a handful of (input, ideal output) pairs before the task. The model generalizes style, structure and edge behavior from the examples better than from adjectives. The catch: models copy whatever they see — a poor example is contagious and will reproduce its flaw.

Motivation

Why do you need it?

  • A concrete example communicates intent better than a paragraph (usually).
  • Edge-case examples teach the model what to do when input is unusual.
  • Format/JSON-style examples lock structure reliably.
  • Cost is fixed and small: 2-8 examples typically suffice.
Think about it

Mental model

Imagine

Learning how to write by imitating model letters

Think of

Few-shot examples

Showing a learner three perfect letters teaches shape, stroke order and spacing better than a rulebook. But a single sloppy letter in the examples will be copied faithfully — learners imitate examples, for better and worse.

Mechanics

How it works

  • Choose 2-8 examples covering: common case, tricky boundary, failure to avoid.
  • Vary topics so the model learns the TASK, not the topic (avoid topic overfitting).
  • Match the target format exactly in every example.
  • Put examples between instructions and the real query.
  • Regenerate examples whenever the desired behavior changes.

Prompt anatomy with examples.

Role + rules
Example 1
Example 2
Example 3
Real query
Real world

In production

  • Classification prompts use label-balanced examples to fix skew.
  • Formatting tasks use JSON examples to eliminate parse errors.
  • Support copilots show one 'never make promises' example.
Guidelines

Best practices

  • Audit every example as if it were production output.
  • Keep example set small (2-8) unless measurement says otherwise.
  • Match examples to the REAL data distribution, not idealized inputs.
  • Store examples beside the prompt, versioned.
Watch out

Common mistakes

  • Loading the prompt with dozens of one-off examples.
  • Examples that mismatch the desired output format.
  • Forgetting adversarial examples that teach failure heuristics.
Root cause analysis

Common issues

Error
Outputs mirror the example's topic
Root cause

Examples too topical or few

Fix

Broaden topics; make examples use varied entities.

Error
Examples too long for costing
Root cause

Verbose exemplars per call

Fix

Shorten examples; trim to the essential contrastive pair.

When things break

Debugging tips

  • Model copies an example's style into bad output → that example is teaching wrong behavior; replace.
  • Format degradation on edge cases → add a dedicated edge-case example.
  • Topic-overfit answers → diversify example topics so task-generalization dominates.
Interviews

Interview checkpoint

Practice

Hands-on lab

Few-Shot Classification, No API
25 min · beginner
In progress
Objectives
  • Create a labeled example table for a sentiment task
  • Replicate few-shot behavior with a nearest-example fallback
  • Assemble a k-shot prompt and measure its token size

Few-Shot Classification, No API

25 min · beginner

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Frequent few-shot failure mode to avoid:

2

The model can learn unwanted behavior from a single bad example.

Go deeper

Official documentation