Sub topic 13
Pitfalls: Copying, Bias, Leakage, and Token Cost
This chapter assumes you can read short Python, know the system, user and assistant roles in chat prompts, and have built a few-shot prompt like the sentiment classifier from earlier chapters. Examples steer a model strongly. That same strength causes four common failures.
Example copying
A model may reproduce an example instead of solving the new input. This happens when the query resembles one example closely, or when all examples share a surface feature such as length or a phrase.
Symptoms:
- The output repeats an example's label or wording for an input that differs in meaning.
- Every answer has the same length or structure as the examples, even when the task needs something else.
Fixes:
- Vary the examples in length, vocabulary, and topic.
- Check that no example is a near duplicate of a likely query.
- State the task in an instruction, so the model does not rely only on the pattern.
Label bias
The model is drawn toward labels that appear often, appear last, or look familiar. Suppose three examples are positive and one is negative. The model will lean toward "positive" on ambiguous input.
from collections import Counter
def label_counts(examples):
return Counter(label for _, label in examples)
examples = [
("Great screen.", "positive"),
("Love the speakers.", "positive"),
("Fast shipping, works well.", "positive"),
("It broke in a week.", "negative"),
]
print(label_counts(examples))The output is Counter({'positive': 3, 'negative': 1}). Balance the counts, or match them to the real mix of inputs if you know it. Also shuffle the order and check that results do not change when you reorder the examples.
Leakage
Leakage means the data you use to measure the prompt also appears in the prompt. If a test item is one of your in-prompt examples, the model can answer it by copying, and your accuracy looks better than it is.
def split_pool(labeled, k):
"""Use the first k items as prompt examples and the rest for testing."""
return labeled[:k], labeled[k:]
def has_leak(examples, test_set):
seen = {text for text, _ in examples}
return any(text in seen for text, _ in test_set)Leakage also happens with dynamic selection. If you pick examples from a pool that includes the test set, the most similar example to a test query may be the query itself. Keep the example pool and the evaluation set separate, and run has_leak before every evaluation.
Token cost
Every example is sent with every request. You pay for those tokens, and they also add latency. They also use up the context window, which limits how much room is left for the query and the answer.
A rough estimate:
def prompt_tokens(examples, tokens_per_example=40, base=60):
return base + tokens_per_example * len(examples)
for k in (3, 8, 20):
print(k, prompt_tokens([None] * k))This prints 180, 380, and 860. At 100,000 requests a day, the difference between 3 and 20 examples is 68,000,000 extra input tokens daily (680 tokens × 100,000). Measure whether the extra examples raise held-out accuracy. If they do not, remove them.
Summary of checks
| Pitfall | Warning sign | Check |
|---|---|---|
| Copying | Outputs echo an example | Test inputs unlike any example |
| Label bias | One label dominates errors | Count labels, shuffle order |
| Leakage | Accuracy is suspiciously high | Run `has_leak` |
| Token cost | Bills or latency grow | Compare accuracy at fewer examples |
Exercise
Your prompt has five examples: four labeled "neutral" and one labeled "negative". Accuracy on held-out data is 90%, but nearly every error is a true "negative" predicted as "neutral". Name the pitfall and give two changes you would try.
Show answer
This is label bias. The prompt shows "neutral" far more often than "negative", so the model defaults to it. Rebalance the labels (for example, add negative examples or remove some neutral ones), and shuffle the example order to see whether errors change. Then re-measure accuracy on held-out data that is not in the prompt.
Exercise
A teammate adds 12 more examples and accuracy on held-out data rises from 91% to 91.5%. Input volume is high. Should you keep them?
Show answer
Probably not. The 0.5-point gain is small, and it may be noise on a small test set. The 12 examples add tokens to every request, which raises cost and latency. Keep them only if the gain holds on a larger held-out set and is worth the added cost.