Sub topic 13
Limits and defense in depth
This chapter assumes you can write Python, have read the earlier chapters on regex, checksum, and NER detection, and know how redaction fits into a request pipeline. It covers what automated redaction cannot do and how to build layers around it.
Why detection is never complete
Every detector from this tutorial makes errors. Regex and checksums find structured identifiers well, but they miss formats you did not anticipate. NER finds names and places, but it misses unusual names, misspellings, and text in languages the model was not trained on. A detector tuned for fewer false positives will miss more real data, and the reverse is also true.
Measured recall is also an average over your test set. A detector with 98% recall still lets 2 in 100 identifiers through, and in a system that handles a million messages, that is a lot of leaked values.
Common failure modes
| Failure | Example | Why it happens |
|---|---|---|
| Unseen format | `ana [at] example [dot] com` | The regex expects `@` and `.` |
| Context-only identity | "the CEO of our Lisbon branch" | No single token is personal data, but together they point to one person |
| Quasi-identifier combination | ZIP code, birth date, and job title | Each is harmless alone, but the combination can single out a person |
| Obfuscation or encoding | Base64 text, digits spelled as words | Patterns match characters, not meaning |
| Overlap and boundary errors | A span cuts a name in half | Two detectors disagree on start and end |
| Non-text inputs | Text inside an image or PDF scan | The detector only sees extracted text |
| Leaks after redaction | The placeholder `<PERSON_1>` plus surrounding facts | The remaining text still describes the person |
Re-identification is the hardest case. Removing direct identifiers does not make text anonymous if the rest still identifies someone. Placeholders and pseudonyms are pseudonymization, not anonymization, because your mapping can reverse them. Regulations such as the GDPR still treat pseudonymized data as personal data.
Defense in depth
Defense in depth means you assume each layer will fail and make sure no single failure causes a leak. Layers for an LLM app include:
- Data minimization. Do not collect or send fields the task does not need. This is the strongest control because it needs no detection.
- Redaction before the model call. Use the hybrid pipeline from earlier chapters.
- Contractual and provider controls. Use providers with zero-retention options, no training on your data, and a data processing agreement.
- Access control. Limit who and what can read the mapping, logs, and vector store.
- Output checks. Run the detector on model replies as well, since the model may produce personal data from retrieved documents or its own training.
- Retention limits. Delete logs, traces, and cached prompts on a schedule.
- Monitoring and review. Sample redacted and unredacted traffic, and measure recall regularly.
A simple fail-closed guard
A guard decides what happens when detection is uncertain. The version below blocks a request if any high-risk label remains after redaction, and it works with the redact function from earlier.
HIGH_RISK = {"CREDIT_CARD", "SSN"}
def guarded_call(user_text, detect, call_llm):
spans = detect(user_text)
labels = {label for _, _, label in spans}
if labels & HIGH_RISK:
raise ValueError("High-risk data found. Request blocked.")
safe_text, mapping = redact(user_text, spans)
reply = call_llm(safe_text)
leftover = detect(reply)
if leftover:
reply = redact(reply, leftover)[0]
return restore(reply, mapping)Blocking is a policy choice. For card numbers, refusing is often better than guessing, because one miss is costly. For names, blocking too often makes the product unusable, so redaction alone may be acceptable.
Practical rules
- Treat the detector as a risk reducer, not a guarantee.
- Choose thresholds by the cost of each error type. A missed card number costs more than an extra redacted word.
- Test with realistic, messy data and adversarial cases, and add every miss you find to your labelled test set.
- Keep the mapping separate from the logs and encrypt it.
- Document residual risks so that owners and reviewers can decide whether they are acceptable.
Exercise
Your redaction pipeline reaches 99% recall on emails and card numbers. A colleague says the logs no longer need access controls or retention limits. Give two reasons this is wrong.
Show answer
First, 99% recall still lets 1 in 100 identifiers through, so some logs contain real personal data. Second, redaction does not cover quasi-identifiers or context that identifies a person, and the placeholder mapping can reverse the redaction, so the logs and mapping remain personal data. Access control and retention limits are separate layers that reduce harm when detection fails.
Exercise
Why is data minimization listed as the strongest layer?
Show answer
It removes data before any detector runs, so it cannot suffer from missed patterns, unseen formats, or NER errors. Data you never send cannot leak.