Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 8/ 160
01. LLM Fundamentals

Few-shot Prompting

Teaching format and edge cases with examples in-context instead of fine-tuning.

BeginnerIn-context Learning · Exemplars3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

You show the model a few solved examples first, so it copies the pattern for the new one.

Think of it like…

Showing a new employee three correctly filled forms instead of reading them the manual: they spot the pattern quickly, including how to handle the awkward cases the manual forgot, but if all three examples happen to be "approved", they may start approving everything.

Where you’ve seen it

A support-ticket router has instructions for 12 categories and still mislabels refund versus billing questions. Adding 8 examples (including 3 deliberately tricky ones with a one-line reason) lifts accuracy on a 500-ticket test set from 81 to 92 percent, with no training. Moving to dynamic selection, where the 8 most similar past tickets are retrieved per request, gains a few more points.

Words you’ll hear

In-context learning
The model adapts to a task from examples in the prompt, without weight updates.
Exemplar
One example input-output pair shown in the prompt.
Format lock
Using examples to force an exact output structure.
Dynamic few-shot
Retrieving the most relevant examples per request instead of using a fixed set.

Visual diagram

Loading interactive diagram

Theory & deep dive

Choosing and formatting examples

Pick examples that cover each output class or format variation, include hard and borderline cases (not only easy ones), and keep them consistent in format, because the model will imitate that format closely. Three to eight well-chosen examples usually capture most of the gain; returns diminish after that and tokens add up. Mark examples clearly, for instance with input and output tags, so the real input at the end is unambiguous.

Watch for biases the examples introduce: if most examples are one label the model leans toward it, and the last example has outsized influence (recency). Balance labels, shuffle order when testing, and avoid examples that leak the answer through superficial patterns. For reasoning tasks, include the reasoning steps in the examples (few-shot chain-of-thought) so the model reproduces the process, not just the answer.

A classification prompt with a hard case

"Ticket: 'I was charged twice and want my money back.' → Label: refund (reason: asks for money back, even though billing is mentioned). Ticket: 'Why is my invoice higher this month?' → Label: billing. Ticket: {new ticket} → Label:"

Key takeaway: Few, diverse, balanced, consistent examples, with edge cases included.

Dynamic examples and when to fine-tune instead

Dynamic few-shot stores a library of labelled examples, embeds them, and at request time retrieves the k most similar to the new input. This puts the most relevant guidance in front of the model and scales to hundreds of examples, and it's a natural place to add new corrections from production: each mistake you fix becomes a new example.

Few-shot has limits. Long example blocks raise cost and latency on every call (mitigated by prompt caching when the examples are static), and context space is finite. If you need thousands of examples' worth of behaviour, a very stable style, or the lowest latency at high volume, fine-tuning moves that knowledge into weights and shortens prompts. A common path is: zero-shot, then few-shot, then retrieval-based examples, then fine-tune once you have enough data.

Corrections become examples

When reviewers flag a wrong label in production, add the ticket and its correct label to the example library. Dynamic retrieval then shows it to the model the next time a similar ticket appears, with no deployment.

Key takeaway: Retrieve examples per request; fine-tune when examples outgrow the prompt.

How it works, step by step

Step 1 of 4
Baseline

1. Start with a clear instruction

Measure zero-shot accuracy on a small labelled set so you know what examples must improve.

baseline: 81% on 500 tickets
Key numbers at this step
Eval set
fixed

When to use it & trade-offs

What it is

Few-shot prompting puts a handful of worked input-output examples in the prompt so the model infers the task, format and edge-case rules from them, which is in-context learning: no weights change. Zero-shot gives only instructions; one-shot gives one example. Examples are especially good at locking an exact output format and showing how to handle ambiguous cases that are hard to describe in words.

Why it matters: Examples are often the quickest way to fix a model that understands the task but produces the wrong shape, tone or label set. They are cheaper and faster to iterate than fine-tuning, and a few good examples can beat a page of instructions. They also cost tokens on every call and can bias the model, so choosing, ordering and testing them matters.

When to reach for it

  • Classification and routing with specific label sets
  • Forcing an exact JSON or text format
  • Showing how to handle ambiguous or borderline cases

When to avoid or be careful

  • Examples that are all easy or all the same label
  • Inconsistent example formats
  • Dozens of static examples sent on every call without caching

Zero-shot vs few-shot vs fine-tuning

Zero-shot
Best for: Tasks the model already does well from instructions

Strengths: Shortest prompt; cheapest.

Watch out: Format and edge-case handling can drift.

Few-shot (static or dynamic)
Best for: Locking formats and teaching edge cases quickly

Strengths: Fast iteration, no training, easy to add corrections.

Watch out: Tokens on every call; order and label bias.

Fine-tuning
Best for: High-volume, stable behaviour with lots of labelled data

Strengths: Short prompts; consistent style.

Watch out: Data, training and maintenance effort.

Interview signals & numbers to know

When this topic comes up

  • “The model understands but uses the wrong format”
  • “How many examples should I include?”
  • “It labels everything as the first category”
  • Dynamic example selection
  • Few-shot vs fine-tuning

Benchmarks & rules of thumb

3 – 8 -
examples that usually capture most of the benefit
last example -
has outsized influence (recency)
+11 points -
the ticket-router example's gain (81 → 92%)

Common mistakes & follow-ups

Common interview mistakes

  • Providing five "billing" examples and one "refund" example, then wondering why the model over-predicts billing.
  • Letting example formatting vary, so the model's outputs vary the same way.
  • Not measuring: judging a prompt from three hand-picked inputs instead of a labelled set.

Likely interviewer follow-ups

  • How would you pick examples for a classifier prompt?
  • What biases can few-shot examples introduce?
  • When would you move from few-shot to fine-tuning?
Interview tip: Mention measuring on a labelled set and shuffling example order. Few-shot answers without an evaluation loop sound like folklore.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Few-shot Prompting with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
System Prompts
Next topic
Chain of Thought