Few-shot learning

Few-shot learning is the technique of including 2-10 worked examples in the prompt to teach an LLM a new task without any retraining.

Few-shot learning (or "in-context learning") is the prompt pattern where you include a handful of example input/output pairs in the prompt itself, then ask the model to follow the same pattern on a new input. The model learns from the examples in real time — no fine-tuning, no training, just careful prompt construction.

This is the single most reliable LLM technique. For almost any structured task, 2-5 well-chosen examples will outperform 500 words of abstract instructions.

A simple example for sentiment classification:

Tweet: "I love this!"
Sentiment: positive

Tweet: "Worst experience ever."
Sentiment: negative

Tweet: "It's okay I guess."
Sentiment: neutral

Tweet: "Absolutely incredible product."
Sentiment:

The model will reliably continue with "positive." It learned the pattern from the demonstrations.

What makes few-shot work well:

  • Format consistency — every example uses the same shape. Models are pattern-matching machines.
  • Edge case coverage — include the ambiguous cases your task actually encounters, not just easy ones.
  • Diversity — vary the inputs so the model doesn't memorize surface features (length, punctuation).
  • Order matters — recent results suggest later examples carry more weight; put the hardest/most-representative ones last.

When few-shot isn't enough: if you need consistent behavior across thousands of edge cases, or strict format compliance the model occasionally violates, you've outgrown few-shot and should evaluate fine-tuning. But that day comes later than most teams expect.

FAQ

How many examples is 'few'?

Practically: 2-10. More than that and you're using context window inefficiently — the marginal gain per example diminishes fast. For very hard tasks, sometimes 20-50 helps; beyond that, fine-tune.

Related terms

Want to actually build with this?

Our Stack Builder picks the best AI tools for your specific project in under 60 seconds.

Build my stack →