Sub topic 13
Measuring false positives and false negatives
This chapter assumes you can write basic Python and have seen rule-based and model-based checks from earlier chapters. A guardrail is a classifier: it labels each input or output as "allow" or "flag". You can't tell whether it works without counting how often it is wrong.
The four outcomes
Pick one risk, such as "text contains an SSN". Compare each decision to the correct label:
| Row | Guardrail flags | Guardrail allows |
|---|---|---|
| Risky in reality | True positive (TP) | False negative (FN) |
| Safe in reality | False positive (FP) | True negative (TN) |
- A false positive blocks something harmless. Users see it as a broken product.
- A false negative lets something risky through. This is the failure that causes incidents.
The rates
- False positive rate: FP / (FP + TN). The share of safe items that get flagged.
- False negative rate: FN / (FN + TP). The share of risky items that get missed.
- Precision: TP / (TP + FP). When the guardrail flags something, how often it is right.
- Recall: TP / (TP + FN). The share of risky items it catches. Recall is 1 minus the false negative rate.
A worked example
This script scores the SSN pattern against a small labeled set. Each case is a pair of text and a label that says whether the text really contains an SSN.
import re
US_SSN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
cases = [
("My SSN is 123-45-6789", True),
("SSN: 987-65-4321", True),
("My social is 123 45 6789", True),
("Order 555-12-3456 shipped", False),
("Nice weather today", False),
]
tp = fp = fn = tn = 0
for text, is_risky in cases:
flagged = bool(US_SSN.search(text))
if flagged and is_risky:
tp += 1
elif flagged and not is_risky:
fp += 1
elif not flagged and is_risky:
fn += 1
else:
tn += 1
print(f"FPR={fp / (fp + tn):.2f} FNR={fn / (fn + tp):.2f}")
print(f"precision={tp / (tp + fp):.2f} recall={tp / (tp + fn):.2f}")
# FPR=0.50 FNR=0.33
# precision=0.67 recall=0.67The order number has the same shape as an SSN, so it is a false positive. The SSN written with spaces slips past the pattern, so it is a false negative. Both errors are visible only because the labels exist.
Building a useful test set
- Collect real examples from logs where you can, with PII removed.
- Include hard negatives, which are safe items that look risky, like the order number above.
- Include evasions, which are risky items written in unusual ways, like spaces instead of dashes.
- Keep the set separate from the examples you used to write the rules. Otherwise the rates look better than they are.
- Use enough items per class. Five cases only illustrate the method. With a handful of risky items, one miss moves the false negative rate by a large amount.
Choosing the trade-off
Making a check stricter usually lowers false negatives and raises false positives. Loosening it does the reverse. Many classifier and judge checks return a score, and the threshold you pick sets the balance.
Decide by cost:
- If a miss is expensive, such as leaked secrets or self-harm content, accept more false positives.
- If blocking is expensive, such as a support bot refusing normal questions, accept more false negatives. Add a second layer to catch what slips through.
Re-measure after every change to a rule, prompt, model, or threshold. A judge prompt edit can shift the rates as much as a new regex.
Exercise
A guardrail ran on 200 safe messages and 50 risky ones. It flagged 10 of the safe messages and 40 of the risky ones. What are the false positive rate and the false negative rate?
Show answer
False positive rate = 10 / 200 = 5%. The guardrail caught 40 of the 50 risky messages, so it missed 10, and the false negative rate = 10 / 50 = 20%.