Sub topic 7
How Many Examples Are Enough
This chapter assumes you can read short Python, know how system, user, and assistant roles work in chat prompts, and have already built a few-shot prompt with consistent examples. Here you will decide how many examples to include and how to check that decision.
More examples are not always better
Each example costs tokens, which means it costs money and latency on every request. It also takes space from the context window, the maximum amount of text a model can read at once. Examples help most at the start. After a few, each new one adds less.
A rough pattern for a simple classification task looks like this:
| Examples | Typical effect |
|---|---|
| 0 | The model guesses the format and may add extra words. |
| 1 to 3 | Format and label names become reliable. |
| 4 to 8 | Boundary cases improve, such as "neutral" versus "negative". |
| 10 or more | Small gains, or none, and higher cost. |
These ranges are starting points, not rules. Your task, your model, and your data decide the real numbers.
What drives the right number
Three things matter more than a fixed count.
- Label count. Each label should appear at least once, and preferably twice. A task with five labels needs more examples than one with two.
- Task difficulty. Copying a format needs few examples. Judging subtle tone or applying a custom policy needs more.
- Input variety. If real inputs differ widely in length or style, you need examples that cover that range. Ten near-identical examples teach less than four different ones.
A good first prompt uses one example per label plus one or two examples of a hard case. Then you measure.
Measure instead of guessing
The reliable way to choose a count is to test it. Hold out labeled data that is not used as an in-prompt example, then score each prompt variant. This code reuses the accuracy function from the evaluation helper and the build_messages function from the chat-format chapter.
def accuracy(predict, labeled):
correct = 0
for text, label in labeled:
if predict(text).strip().lower() == label:
correct += 1
return correct / len(labeled)
def compare_counts(pool, held_out, make_predict, counts=(0, 1, 3, 6)):
results = {}
for k in counts:
predict = make_predict(pool[:k])
results[k] = accuracy(predict, held_out)
return resultsHere make_predict is a function you write. It takes a list of examples, builds the prompt, calls your model, and returns a function from text to a label. compare_counts returns something like {0: 0.62, 1: 0.71, 3: 0.84, 6: 0.85}.
Read the result as a curve. The curve above rises quickly and then flattens. In that case, three examples are enough, and six only add cost.
Reading the results carefully
Small held-out sets are noisy. With 20 test items, one item changes accuracy by 5 points. A gap of 1 or 2 points between prompts may be chance. Use a held-out set of at least a few dozen items, and treat small differences as ties. When two prompts tie, choose the one with fewer examples.
Also check that the pool order did not drive the result. Taking pool[:k] means the first examples are always the ones used. If your pool is sorted by label, small values of k will miss whole labels. Shuffle the pool, or choose examples so every label is present, before you compare counts.
Practical guidance
- Start with one example per label plus one hard case.
- Add examples only when you can name an error they fix.
- Stop when held-out accuracy stops improving.
- Recheck if you change models, since a stronger model often needs fewer examples.
Exercise
Your five-label classifier scores 0.80 with 5 examples, 0.82 with 10, and 0.82 with 20 on a held-out set of 25 items. Which count should you use, and what should you do before trusting the result?
Show answer
Use 5 examples. The gains from 10 and 20 are 2 points, which is half of one test item out of 25 (one item is 4 points), so the difference is within noise, and the extra examples cost tokens on every request. Before trusting the result, enlarge the held-out set to at least a few dozen items, and confirm that the 5 examples include every label and were not taken from the held-out set.