Sub topic 7
How Many Examples Are Enough
This chapter assumes you can read short Python, know the system, user, and assistant roles in a chat prompt, and have built a few-shot prompt like the ones in earlier chapters. It also assumes you have a pool of labeled examples to choose from.
There is no universal right number of examples. The right number is the smallest one that reaches your accuracy target on data you did not use in the prompt. This chapter explains why returns diminish, what limits the count, and how to measure the answer for your own task.
Why the count matters
Each example you add has a cost and a benefit.
The benefit is information. Examples show the model the output format, the label vocabulary, the tone, and where the boundary between categories lies.
The costs are:
- Tokens. Every example is sent with every request, so the cost multiplies by your request volume.
- Latency. Longer prompts take longer to process.
- Context budget. Examples compete with the input text and the answer for space in the context window.
- Attention dilution. In a long prompt, the model may weigh any single example less, and the examples that matter can get lost among the others.
More examples are not always better. Often you gain most of the benefit from the first few and very little after that.
The usual shape of the curve
When you plot accuracy against the number of examples, k, most tasks follow a similar pattern:
- From 0 to 1 or 2 examples is the biggest jump. The model learns the output format and the label names. Many "wrong" zero-shot answers are really format errors, such as "The sentiment is positive." instead of "positive".
- From about 3 to 8 examples gives smaller gains. The model learns where the category boundaries are and which edge cases belong where.
- Beyond that, the curve flattens and can wobble. Adding examples may do nothing, or it may hurt if the new ones are noisy, redundant, or skew the label balance.
This is a typical shape, not a law. A task with a strict output format may be solved with two examples. A task with many fine-grained labels or subtle rules may keep improving past twenty. Strong recent models often need fewer examples than older ones, so numbers from older articles may be too high.
What the right number depends on
Task difficulty and output format
A task with a rigid output, such as "return a JSON object with these three keys", needs enough examples to fix the format. Usually that is two or three. A task with subtle judgment calls, such as deciding whether a support ticket is about billing or about account access, needs more examples because each one teaches a different part of the boundary.
Number of labels
Aim to show every label at least once, and preferably two or more times for labels that are easy to confuse. With 3 labels, 6 examples is a natural starting point. With 20 labels, showing every label twice means 40 examples, which is often too many. At that point, consider selecting examples per query (a later chapter) instead of showing all labels every time.
Length of each example
Short examples are cheap. Examples that contain full emails or documents are not. A prompt with ten 500-token examples spends 5,000 tokens before the actual input arrives. For long inputs you may only be able to afford two or three examples, so choose them carefully.
Model and context window
Smaller models usually gain more from additional examples, and their gains continue longer. Larger models often get most of the benefit from a few. Check your own model rather than assuming.
Diversity of the examples
Eight near-duplicate examples teach less than four varied ones. If your accuracy stalls, look at whether the examples are redundant before adding more.
Measuring instead of guessing
You find the right k by running an experiment. Hold out a dev set that is separate from the pool you draw examples from. Then vary k and measure accuracy at each value.
Two details matter:
- Draw several random example sets for each k. Which examples you pick changes the result as much as how many you pick. If you test only one random draw, you cannot tell whether a gain came from the count or from a lucky selection. Report the mean and the range.
- Check the dev set size. With 50 dev items, one item is 2 percentage points. The standard error of an accuracy near 80% on 50 items is about sqrt(0.8 × 0.2 / 50), roughly 5.7 points. Differences smaller than that are noise. Use 200 or more items when you can.
The code below runs this experiment. It takes any function complete that receives a chat message list and returns the model's text. The stub model at the bottom lets the script run as shown, but it ignores the examples, so its results are flat. Replace it with a real call to your model to get meaningful numbers.
import random
SYSTEM = (
"Classify the sentiment of each review as positive, negative, or neutral. "
"Answer with one word."
)
def build_messages(examples, query):
messages = [{"role": "system", "content": SYSTEM}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": query})
return messages
def accuracy(predict, labeled):
correct = 0
for text, label in labeled:
if predict(text).strip().lower() == label:
correct += 1
return correct / len(labeled)
def sweep(complete, pool, dev, ks, trials=5, seed=0):
"""Return {k: (mean, low, high)} accuracy over random example draws."""
rng = random.Random(seed)
results = {}
for k in ks:
n_trials = 1 if k == 0 else trials
scores = []
for _ in range(n_trials):
shots = rng.sample(pool, k) if k else []
def predict(text, shots=shots):
return complete(build_messages(shots, text))
scores.append(accuracy(predict, dev))
results[k] = (sum(scores) / len(scores), min(scores), max(scores))
return results
def stub_complete(messages):
# Stand-in for a real model call. It ignores the examples.
text = messages[-1]["content"].lower()
if any(w in text for w in ("stopped", "broken", "weak")):
return "negative"
if any(w in text for w in ("great", "fast", "love")):
return "positive"
return "neutral"
pool = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
("I love the camera.", "positive"),
("The hinge is broken already.", "negative"),
("It comes in a white box.", "neutral"),
]
dev = [
("Great sound for the price.", "positive"),
("The strap snapped on day two.", "negative"),
("It ships with a USB cable.", "neutral"),
]
for k, (mean, low, high) in sweep(stub_complete, pool, dev, ks=[0, 2, 4, 6]).items():
print(f"k={k}: mean={mean:.2f} range=({low:.2f}, {high:.2f})")Notice that the pool and the dev set share no items. If a dev item also appears as a prompt example, the model can copy its answer, and your score will be inflated. A later chapter covers this leakage in detail.
Choosing the stopping point
Pick the smallest k whose accuracy is within noise of the best k you measured. If k=4 scores 0.86 and k=12 scores 0.87 on a 50-item dev set, those two are not meaningfully different, and k=4 is cheaper and faster.
A rough budget check helps. Suppose each example is about 20 tokens and you use 8, which adds 160 tokens per request. At 100,000 requests a day, that is 16 million extra input tokens a day. If 4 examples perform as well, you save half of that.
Edge cases and failure modes
- Accuracy drops when you add examples. The new example may be mislabeled, may clash with the others, or may push the label balance toward one class. Remove it and re-measure before concluding that more examples hurt.
- Results vary a lot between draws. The range in the sweep will be wide. This means your prompt is sensitive to which examples it contains. Curate a fixed set by hand and test it, instead of sampling at random.
- The model starts copying. With very few, very similar examples, the model may repeat an example's wording or label. More varied examples, not more examples of the same kind, reduce this.
- The last examples dominate. Models often weigh examples near the end of the prompt more heavily. Changing k also changes which examples sit next to the query. When comparing counts, keep the order policy fixed.
- Hitting the context limit. If examples plus input plus expected output do not fit, you must cut examples or shorten them. Do not truncate the examples mid-text, because a cut-off example teaches a broken format.
- Plateau below target. If adding examples stops helping and you are still short of your target, the problem is probably not the count. Look at the instructions, the label definitions, or whether the task needs reasoning steps, and consider the alternatives covered at the end of this tutorial.
Summary of rules of thumb
| Situation | Starting point |
|---|---|
| Fixing an output format only | 1 to 3 examples |
| 3 to 5 labels, clear boundaries | 4 to 8 examples, each label at least once |
| Subtle or overlapping categories | 8 to 20, with edge cases, or select per query |
| Long documents as inputs | 2 to 4 short, carefully chosen examples |
| Many labels (20 or more) | Dynamic selection instead of showing all |
These are starting points to test, not answers. Your dev-set curve decides.
Exercise
You measure a classifier on a 40-item dev set. With 3 examples it scores 0.80, with 6 it scores 0.85, and with 12 it scores 0.85. Each score comes from one random draw of examples. Which count would you ship, and what would you do before trusting the comparison?
Show answer
Ship 6 examples at most, since 12 costs twice the tokens for the same score. But the comparison is weak. On 40 items, one item is 2.5 points, so the 5-point gap between 3 and 6 examples is only 2 items, which is within noise. Each number also comes from a single draw. Re-run each k with several random draws (for example 5) and report the mean and range, and enlarge the dev set to 150 or more items if possible. Only then decide whether 3, 6, or another count is enough.