Few-shot prompting uses a small set of input-and-output examples to show an AI model exactly how to perform a task. This guide explains how to choose the right number of examples, estimate prompt size and cost, handle edge cases, and test whether your examples actually improve consistency across ChatGPT, Claude, Gemini, and open-source LLM workflows.
Overview
Few-shot prompting is one of the most practical prompt engineering techniques for tasks with a repeatable format. Instead of describing the desired behavior only with instructions, you include demonstrations such as:
- Input: A customer message about a late delivery
Output:{"intent":"delivery_issue","priority":"normal"} - Input: A message reporting an account takeover
Output:{"intent":"security_issue","priority":"high"}
The model can then apply the demonstrated pattern to a new input. The examples do not train the model permanently. They guide the current request, so they are often called in-context examples or few-shot examples.
There is no universal best number of examples. A useful starting point is two to five carefully selected demonstrations. Simple tasks may need only one or two examples, while tasks with several labels, formatting rules, or important edge cases may need more. Adding examples indefinitely is usually less effective than improving their coverage and clarity.
Few-shot prompting is most useful when a task has an implicit standard that is difficult to express in instructions alone. Examples can clarify tone, label boundaries, date formats, field names, acceptable detail, and the difference between a valid answer and a plausible-looking mistake.
How to estimate
Treat the number of examples as a testing decision rather than a fixed rule. The goal is to find the smallest example set that produces acceptable results across representative inputs.
1. Establish a zero-shot baseline
First, run the task with instructions but no demonstrations. Record the results on a small test set. Measure the errors that matter for your workflow: incorrect labels, missing fields, invalid JSON, unsupported claims, inconsistent tone, or unnecessary explanations.
This baseline prevents you from assuming that few-shot prompting is automatically better. If the task is already reliable, examples may add token use and maintenance without enough benefit.
2. Add one representative example
Choose an ordinary, unambiguous case and add it to the prompt. Test again using the same inputs. If the output changes in the intended direction, add a second example that demonstrates a different valid case.
3. Cover the decision boundaries
Examples should explain where similar categories separate. For a support classifier, showing two unrelated categories is less valuable than showing two messages that could reasonably be confused. For an extraction task, include an example where a field is present and another where it is absent.
A practical selection sequence is:
- One typical example for the main behavior.
- One example for a commonly confused case.
- One edge case or missing-value case.
- One formatting example if the output structure is strict.
- Additional examples only when testing shows a specific failure remains.
4. Compare consistency, not just one answer
Run multiple inputs from each important category. A prompt that performs well on one demonstration may still fail on paraphrases, longer text, unusual punctuation, or mixed requests. Compare the zero-shot and few-shot versions using the same evaluation set.
For production work, estimate prompt overhead separately from output usage. A simple token-cost model is:
Total prompt tokens = instruction tokens + example input tokens + example output tokens + live input tokens.
If your provider charges separately for input and output, apply the provider's current rates to each portion. Do not rely on an old price estimate: model pricing, context limits, and provider counting methods can change. For a repeatable comparison, record the model, prompt version, average input length, average output length, request volume, and observed failure rate.
Inputs and assumptions
Good few-shot prompts depend more on example quality than example quantity. Before writing demonstrations, define the following inputs.
Task and output contract
State what the model must do and what it must return. Separate instructions from the data being processed. For structured tasks, specify field names, allowed values, missing-value behavior, and whether extra commentary is prohibited. See the guide to structured output prompts for a deeper treatment of valid JSON workflows.
Representative examples
Select examples from the real distribution of inputs rather than inventing only neat demonstrations. Include variations in length, vocabulary, spelling, and formatting when those variations occur in production. An example should be correct, concise, and easy to compare with the new input.
Edge cases and abstention behavior
Show the model what to do when information is incomplete or does not fit a known category. For example, use an explicit unknown label or a null field instead of encouraging the model to guess. If user-provided text can contain instructions, treat it as data and consider the input-handling practices in the prompt injection defense checklist.
Ordering and formatting
Keep each demonstration in the same structure: label the input, then show the expected output. Consistent separators make examples easier to inspect and reduce ambiguity. Put the most important or representative examples where they are easy for the model and a human reviewer to identify. Test ordering rather than assuming that the first or last example is always best.
Keep examples close to the task they explain, but do not bury the live input inside the demonstrations. A reliable pattern is:
Instruction:
Classify the message using exactly one allowed label: billing, technical, account, or unknown.
Return JSON only.
Example 1
Input: I was charged twice for the same order.
Output: {"label":"billing"}
Example 2
Input: The app closes whenever I upload a file.
Output: {"label":"technical"}
Example 3
Input: Can you help me with this?
Output: {"label":"unknown"}
New input:
{{message}}
Output:
Use version control for prompts and examples. When a label definition changes, update the examples and evaluation set together. The prompt debugging checklist can help isolate whether an error comes from instructions, examples, input quality, or model behavior.
Worked examples
Classification
For classification, include examples that distinguish neighboring labels. If the labels are “positive,” “neutral,” and “negative,” examples should clarify how to handle mixed sentiment, sarcasm, and factual complaints without emotional language. Two or three well-separated examples may be enough for a simple taxonomy; a larger label set usually requires coverage of each important boundary.
Extraction
For extraction, demonstrate both populated and missing fields:
Input: Meeting with Dana Lee on 14 March at 3 PM.
Output: {"person":"Dana Lee","date":"14 March","time":"3 PM"}
Input: Please send the contract to Dana Lee.
Output: {"person":"Dana Lee","date":null,"time":null}
This pattern tells the model not to invent values. It is especially useful for document processing, where source text may be incomplete or inconsistently formatted. For a broader workflow, see the AI text extraction workflow.
Rewriting
For rewriting, examples should show the transformation rather than merely describe the desired voice. Include the original text, revised text, and any constraints such as maximum length, reading level, or preserved facts. One example can establish tone; a second can show how the model handles technical language or a sentence that should not be changed.
Coding
For coding prompts, use examples that expose the expected interface: input type, output type, error handling, and naming conventions. A small example that compiles or passes a stated test is more useful than a long code sample with unclear requirements. Ask the model to return only the requested code or a specified explanation format.
Across all four tasks, evaluate more than apparent fluency. Check correctness, schema validity, preservation of source facts, and behavior on cases not shown in the examples. For publisher workflows, combine few-shot prompting with a review standard such as the AI-generated content quality rubric.
When to recalculate
Revisit a few-shot prompt whenever its inputs or operating conditions change. Recalculate example count, token usage, and observed quality after:
- Adding, removing, or redefining labels.
- Introducing a new document type, audience, language, or content format.
- Seeing a new edge case in production.
- Changing the model, provider, context limit, temperature setting, or structured-output method.
- Changing request volume or average input length.
- Observing higher error rates, more invalid outputs, or increased review time.
- Updating provider pricing or token-counting guidance.
Keep a small regression set containing typical cases, boundary cases, and known failures. When you change the prompt, compare the new version with the previous version using the same set. If extra examples improve one category but harm another, revise the examples or instructions instead of simply adding more.
As a practical next step, create three versions of your prompt: zero-shot, one or two examples, and a broader example set. Run each against the same inputs, record quality and token usage, then keep the smallest version that meets your requirements. Save the examples, model name, prompt version, and test results together. This turns few-shot prompting from trial and error into a maintainable part of your AI development workflow.