Glossary
Few-shot prompting
A prompting technique that includes a handful of worked input and output examples so the model infers the task, format and judgement from them.
Few-shot prompting is a technique in which you include a small number of worked examples, typically two to ten pairs of input and desired output, in a prompt, so the language model infers the task, the output format and the judgement you want from those examples before handling a new input. It needs no training: the model learns the pattern in context for that request only. It is most useful when instructions alone leave room for interpretation, such as an unusual format, a classification with fuzzy boundaries or a house style. Its costs are prompt length and a tendency for the model to copy surface features of the examples, so choosing the right examples matters more than adding many.
How few-shot prompting works
A large language model predicts text that continues the pattern in front of it. Shown three examples of a support email followed by a category label, then a fourth email with no label, the most likely continuation is a label in the same style. This ability to pick up a task from examples inside the prompt is called in-context learning. The model’s weights do not change; remove the examples and the behaviour goes with them.
The terminology follows the number of examples. Zero-shot prompting gives only instructions. One-shot gives a single example. Few-shot gives several, and many-shot prompting, made practical by long context windows, gives dozens or hundreds. With current instruction-tuned models, zero-shot is often enough for common tasks, and few-shot earns its place where the task is unusual or the boundaries are subtle.
When to use few-shot prompting
Situation | Zero-shot | Few-shot |
|---|---|---|
Common task, standard format (summarise, translate) | Usually enough | Little gain |
Unusual or strict output format | Often drifts | Reliable |
Classification with fuzzy categories | Inconsistent at the edges | Examples define the boundaries |
House style or tone | Hard to describe | Easy to show |
Extraction from messy documents | Misses edge cases | Examples show how to handle them |
Tasks needing private facts | Fails | Still fails; supply the facts instead |
The last row is worth stating plainly. Examples teach the model how to answer; they do not give it information it lacks. If a model gets a customer’s plan wrong, more examples will not fix it, and the answer lies in retrieval-augmented generation or agent memory.
Writing good examples
The model generalises from whatever the examples share, intended or not. Most problems with few-shot prompts come from examples that share the wrong things.
Cover the hard cases
Easy examples add little; the model already handles them. Choose examples near the boundaries: the email that looks like a billing question but is a bug report, the invoice with two totals, the request that should be refused. Include at least one case where the right output is “none” or “unknown” if that can happen, otherwise the model learns that an answer is always expected.
Vary the surface features
If every example is three sentences long, outputs drift towards three sentences. If the positive examples all mention a product name, the model may treat the name as the signal. Vary length, wording, order and topic across examples so the only consistent thing is the pattern you want. For classification, balance the labels and shuffle their order; models are known to favour labels that appear more often or more recently in the examples.
Keep examples consistent and correct
Contradictory examples teach inconsistency. Check that every example follows the same rules, uses exactly the same output format and agrees with the written instructions. A single wrong label in a five-example prompt can noticeably shift results, because each example carries a lot of weight.
Format examples clearly
Separate examples with consistent delimiters, label the input and output parts the same way each time, and keep them distinct from the new input. Many chat APIs let you write examples as alternating user and assistant messages, which models handle well. Place examples after the instructions in the system prompt or as early message pairs, and keep a prompt template so the structure stays stable while the examples change.
Few-shot prompting with reasoning and structured output
Examples can show reasoning as well as answers. Few-shot chain-of-thought examples include the intermediate steps, which helps on multi-step problems by showing the model how to break them down. With reasoning models that think internally, written reasoning in examples adds less and can constrain the model’s own approach, so test both ways.
For machine-readable output, examples and constraints work together. Examples show the model how to fill each field in hard cases, such as which of two dates is the due date, while structured output against a JSON schema guarantees the shape. The same applies to function calling: example tool calls show when and how to use a tool more clearly than a description alone.
Static and dynamic examples
The simplest few-shot prompt uses the same fixed examples for every request. That works when inputs are similar. When they vary widely, a fixed set wastes tokens on irrelevant examples and misses the ones that would help.
Dynamic few-shot prompting selects examples per request. You keep a library of good examples, embed each one, and at request time use semantic search to pull the few most similar to the new input; top-k retrieval over embeddings is the usual mechanism. A contract-review agent then sees example clauses like the one in front of it, and an invoice extractor sees the vendor layouts most like the current document. Selection usually outperforms a fixed set at the same prompt length, and it lets the library grow without the prompt growing.
Dynamic selection brings the usual retrieval questions. The examples need to stay correct when policies change, near-duplicates should be removed so the model sees variety, and you need to know which examples a given answer was built from when you debug it.
Few-shot examples as agent memory
For agents, a library of examples is a form of memory. Successful past runs, corrected outputs and user feedback can become examples for future runs. Researchers sometimes call this procedural memory, knowledge of how to do a task, as distinct from episodic memory of what happened and semantic memory of facts. An agent that records “when this customer asks for a summary, they want bullet points under 100 words” and retrieves it next time is doing a personalised version of few-shot prompting; the post on episodic vs semantic memory covers the distinctions.
The risk is the same as with any memory: examples that were right once can become wrong. A corrected output from before a policy change will teach the old policy. Examples drawn from user feedback need review, versioning and a way to retire them, or they become a quiet source of hallucination.
Cost and limits
Every example costs input tokens on every request. Five examples of 300 tokens each add 1,500 tokens per call, which at high volume is a real line on the bill. Fixed examples at the start of the prompt are a good fit for prompt caching; dynamic examples change per request and are not. Long example sets also dilute attention, so returns fall after the first few. If you find yourself needing dozens of examples to get consistent behaviour, fine-tuning on those examples may be cheaper per request, and a fine-tuned model needs no examples at inference time.
Measure rather than guess. Build a golden dataset and compare zero-shot, a fixed few-shot set and dynamic selection on the same inputs as part of your LLM evaluation. Run each configuration a few times at your production temperature, since few-shot results can be sensitive to example order.
A worked example
A finance team extracts the due date from supplier invoices. Zero-shot, the model is right on 84 percent of a 250-invoice test set; its errors are mostly invoices that show an issue date, a delivery date and payment terms such as “30 days net” instead of an explicit due date. Three fixed examples covering those layouts lift accuracy to 92 percent. Switching to dynamic selection, with each invoice matched to the three most similar of 60 labelled examples by vendor layout, reaches 96 percent at the same prompt length. The remaining errors are scanned invoices where the date is unreadable, which no example can fix. The figures are illustrative.
Where Exabase fits
Exabase provides storage and retrieval that dynamic few-shot prompting and example-based memory need; the prompt design stays with you. Store examples as notes or files in Resources, where they are indexed on creation, and use Deep Search to retrieve the most similar ones per request with filters by tag or folder; the searching resources guide lists the options. Per-user patterns, such as a customer’s preferred format, fit Exabase Memory, which updates them when they change and returns the relevant ones for the task; see creating memories.
For extraction, Extract returns structured fields for invoices, resumes and contracts directly, which can remove the need for examples in those common cases; the invoice to JSON tool and the file extraction solution page show the output, and the document extraction use case shows it in a pipeline. The best document extraction API roundup compares alternatives.
FAQs
How many examples should a few-shot prompt have?
Usually two to five. Start with a few that cover the hard cases, measure, and add examples only where errors remain. Gains tend to flatten after the first handful, while the token cost keeps rising with each one.
What is the difference between zero-shot and few-shot prompting?
Zero-shot prompting gives the model instructions and no examples. Few-shot prompting adds worked examples of inputs and outputs. Zero-shot suits common tasks; few-shot helps with unusual formats, fuzzy categories and house style.
Is few-shot prompting the same as fine-tuning?
No. Few-shot prompting puts examples in the prompt and changes behaviour for that request only, with no training. Fine-tuning trains on examples and changes the model’s weights, so the behaviour persists without examples in the prompt.
Why does my model copy details from the examples?
Because the model generalises from everything the examples share. If they share a length, phrase or topic, outputs drift towards it. Vary the surface features across examples so that only the intended pattern is consistent.
Does example order matter?
Yes. Models can favour labels or styles that appear more often or more recently in the examples. Balance labels, shuffle their order and test more than one ordering if results seem unstable.
Can few-shot examples be selected automatically?
Yes. Dynamic few-shot prompting embeds a library of examples and retrieves the most similar ones to each new input with semantic search. It usually beats a fixed set at the same prompt length.
Related pages: Zero-shot prompting, In-context learning, Prompt engineering, Chain of thought, System prompt, Fine-tuning, Procedural memory, File extraction, Episodic vs semantic memory, Best document extraction API, prompt template