PII & data privacy

In one sentence

Detect and redact or pseudonymize personal data before it reaches the model, logs or indexes; pick providers with no training and short or zero retention; and make deletion reach every copy, vectors and caches included.

Analogy: A coat check: you hand over your coat and get ticket 42. Staff move ticket 42 around all night without knowing whose coat it is; only you can swap it back.

Raw values stay in your vault; the provider and the logs only see placeholders; an erasure request must reach every store.

How it works

  • Detect PII at the boundary: regex plus checksums for structured ids, NER for names; Presidio does both.
  • Pseudonymize reversibly when the answer needs the value: the model sees [PERSON_0], a vault keeps the mapping.
  • Redact one-way in logs, traces and eval sets, because they outlive the request.
  • Treat embeddings as personal data: vec2text recovered 92% of 32-token inputs exactly from them.
  • GDPR needs a lawful basis per purpose; transfers outside the EU need a legal mechanism, hence data residency.

Check yourself

Answer to mark it learned; your answers aren't scored.

Your app pseudonymizes every prompt with Presidio before calling the LLM, and re-identifies only at the edge. A privacy audit still finds raw customer emails in your observability vendor's dashboards. Most likely cause?

Go deeper

Full explanation

PII (personally identifiable information) is any data that identifies a person: names, emails, phone numbers, government ids, card numbers, health data. The rule for AI systems is minimize what reaches the model, the logs and the indexes, because every copy is another place to leak or to have to delete.

  • Detect at the boundary: regex plus checksums for structured ids (cards, IBANs), NER for names and addresses. Microsoft Presidio combines both.
  • Pseudonymize reversibly when the answer needs the value: replace Maria Lopez with [PERSON_0], keep the map in a vault, re-insert at the edge. The model reasons over placeholders; the provider never sees the name.
  • Redact one-way in logs, traces and eval sets; they outlive the request.

Then the contract side: the provider's retention (e.g. OpenAI keeps API abuse-monitoring logs up to 30 days), zero-data-retention agreements for sensitive workloads, and whether your data trains their models (usually off by default on paid API tiers; free tiers and consumer apps differ).

Under GDPR, each purpose needs a lawful basis (consent, contract, legitimate interest…); the right to erasure must reach the vector index, semantic cache, logs and any fine-tuning set, not just the database; and transfers outside the EU need a legal mechanism, which is why customers ask for data residency (in-region endpoints).

Code Reversible pseudonymization with Presidio
pythonReversible pseudonymization with Presidio
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine

analyzer = AnalyzerEngine()      # regex + checksums + spaCy NER, boosted by context words
anonymizer = AnonymizerEngine()
ENTITIES = ["PERSON", "EMAIL_ADDRESS", "PHONE_NUMBER", "CREDIT_CARD", "IBAN_CODE"]

def pseudonymize(text: str) -> tuple[str, dict[str, str]]:
    found = analyzer.analyze(text=text, language="en", entities=ENTITIES)
    vault, out = {}, text
    for r in sorted(found, key=lambda r: r.start, reverse=True):  # right-to-left keeps offsets valid
        value = text[r.start:r.end]
        token = next((t for t, v in vault.items() if v == value),   # same value -> same token
                     f"[{r.entity_type}_{len(vault)}]")
        vault[token] = value
        out = out[:r.start] + token + out[r.end:]
    return out, vault

def restore(text: str, vault: dict[str, str]) -> str:
    for token, value in vault.items():
        text = text.replace(token, value)
    return text

msg = "Refund John Smith, john@acme.com, card 4111 1111 1111 1111"
safe, vault = pseudonymize(msg)  # e.g. "Refund [PERSON_2], [EMAIL_ADDRESS_1], card [CREDIT_CARD_0]"
reply = call_llm(safe)           # the provider never sees the raw values
print(restore(reply, vault))     # re-identify only at the edge, for this user

# One-way redaction for logs: default operator replaces each span with <ENTITY_TYPE>
log_line = anonymizer.anonymize(text=msg, analyzer_results=analyzer.analyze(text=msg, language="en")).text
Real example

An EU clinic's scheduling assistant handles 50k chats a day. Before each call, Presidio swaps names, phone numbers and insurance ids for tokens; the LLM drafts "Confirm [PERSON_0]'s appointment and text [PHONE_NUMBER_1]" and the app re-inserts the values only inside that patient's session. Logs keep the redacted version with 30-day retention. When a patient requests erasure, a job deletes their rows, their chunks from the vector index by patient_id metadata, their semantic-cache entries and their traces. The audit finds one more copy: an eval set built from raw production chats, which is regenerated from the redacted logs.

Trade-offs +3 · −3

+ You gain

  • Shrinks breach and compliance scope: data the provider never saw can't leak from it.
  • Reversible tokens keep answers useful while the model only sees placeholders.
  • Redacted logs and traces can be shared with engineers and vendors far more freely.

− You pay

  • Detection is probabilistic: NER misses names in odd formats, other languages and free text.
  • Placeholders can hurt quality when the value matters (spelling a name, gender agreement in Spanish).
  • Erasure across indexes, caches, backups and fine-tuned weights is real engineering work.

Use it when

Whenever messages or documents may contain personal data and flow to a third-party model, logs, embeddings or training sets, which is nearly every production assistant.

Avoid it when

Don't mask values the task needs verbatim inside the model (extracting the name from an ID scan); use a zero-retention, in-region deployment and strict access controls instead.

Numbers to know 4
  • GDPR fines: up to €20M or 4% of global annual turnover, whichever is higher.
  • GDPR: answer an erasure or access request within 1 month; notify the authority of a breach within 72 h.
  • OpenAI API data isn't used for training by default (since March 2023); abuse-monitoring logs are kept up to 30 days unless you have ZDR.
  • vec2text recovered 92% of 32-token inputs exactly from their embeddings: treat embeddings as personal data.
Common pitfalls 4
  • Masking the prompt but logging the raw request in the gateway, APM traces or error reports.
  • Deleting from the database but not from the vector index, semantic cache or eval datasets.
  • Fine-tuning on raw customer chats: you can't remove one person from the weights without retraining.
  • Relying on regex alone for names and addresses, or on English-only NER for Spanish text.
Interview questions 2

Answer out loud first, then reveal.

A user invokes the right to erasure. What has to happen in your RAG app?Show answer

Erasure is a data-lineage problem, so I need every place their data landed. That's the primary database; the vector index, which is why every chunk carries user_id and doc_id metadata so I can delete by filter; the semantic cache; agent memory; logs and traces; eval sets; and backups on their expiry cycle. If personal data went into a fine-tuning set, the honest answer is retraining, so I design not to train on raw PII at all. I run it as an auditable job and answer within GDPR's one-month deadline.

How do you use a third-party LLM on sensitive customer data?Show answer

Three layers. Minimize: detect and pseudonymize PII before the call, Presidio-style, with a token vault and re-identification only at the edge. Contract: a paid API tier with no training on our data, a DPA, zero data retention where available, and an in-region endpoint if residency is required. Hygiene: redacted logs and traces by default, short retention, access controls on the vault. The trade-off is quality on tasks that need the literal value, so I run the eval suite with and without masking before deciding per field.