Prompting·4 min read

How to few-shot prompt any LLM

Few-shot prompting uses examples to teach LLMs new formats and behaviours without fine-tuning. This guide explains how to construct effective examples and when few-shot beats zero-shot.

What few-shot prompting is

Few-shot prompting includes 2–5 worked examples in the prompt before the actual question. The model learns the desired output format and style from the examples, then applies it to the new input. It is the fastest way to teach an LLM a new task without fine-tuning.

LLMs are excellent at pattern matching from context. A few well-chosen examples can specify complex output structures, tone, reasoning style, or domain-specific formatting that would be difficult to describe verbally.

Structure of a few-shot prompt

Each example consists of an input followed by the desired output. Separate examples clearly — use consistent delimiters like 'Input:' and 'Output:', or XML tags like `<example>` / `</example>`. Consistency in formatting is more important than the specific delimiter choice.

After the examples, present the actual input with the same format but omit the output. The model will complete the pattern. End with 'Output:' (or your chosen label) to prime the generation.

How to choose good examples

Diversity matters more than quantity. Choose examples that cover different cases, different lengths, and different edge cases you expect in production. Two diverse examples usually outperform five similar ones.

Order the examples from simple to complex. The model applies the most recent examples most heavily, so put your most representative example last.

If your task involves specific formatting — tables, JSON, custom markup — show the exact format in the examples. The model will mirror it precisely.

Few-shot vs zero-shot vs fine-tuning

Zero-shot (no examples) works for simple tasks with a large model. Few-shot is better when the output format is unusual, the domain is specialised, or zero-shot gives inconsistent results. Fine-tuning is only justified when you have 1000+ examples and need the lowest possible inference cost for a specific task.

Start with zero-shot. If the output is inconsistent, add 2 examples. If still inconsistent, add 3 more. If you are writing more than 5 examples for every prompt, consider fine-tuning instead.