PII handling and data residency
LLM features are data-hungry by design: they work better with more context, and the context is often customer emails, tickets, documents and chat history full of personal data. That collides with privacy law, customer contracts and plain common sense. This doc covers how I keep personal data under control in LLM pipelines: minimise what goes in, detect and redact what you cannot avoid, control retention and logging, and make sure processing happens where it is supposed to.
Why it matters
Sensitive Information Disclosure is LLM02 in the OWASP Top 10 for LLM Applications 2025. The ways it happens are mundane: a support transcript pasted into a prompt and kept in vendor logs, an embedding index that returns another customer's document, a debugging trace in your observability tool full of names and account numbers, or a fine-tuning dataset built from production logs that nobody redacted.
Data residency is the other half. Enterprise and public sector customers often require that data is processed and stored in a specific region. Providers responded: OpenAI offers data residency for eligible API customers by creating a project in a chosen region (Europe was first, and more regions followed), and Anthropic documents separate controls for where inference runs (an inference_geo setting per request or workspace) and where data is stored at rest (workspace geo). Options and supported models differ by provider and change over time, so check the current docs before you promise anything in a contract.
How it works
The privacy-by-design flow for an LLM request has four checkpoints.
Rendering diagram…
Minimise. The most effective control is not sending the data at all. If the task is "categorise this ticket", the model rarely needs the customer's phone number. Select fields deliberately in your context builder.
Detect. Use a mix of pattern recognisers (emails, card numbers with checksum validation, national IDs) and NER models for names and addresses. Microsoft Presidio is the common open-source choice: an analyzer that combines regex, NER and checksums with confidence scores, and an anonymizer with replace, redact, mask, hash and encrypt operators.
Redact or pseudonymise. Replace values with stable placeholders such as <PERSON_1> so the model can still reason about who did what, then map them back in the response if the user is allowed to see them.
Control retention. Know what each provider retains and for how long, use zero data retention arrangements where you need them, and apply the same discipline to your own logs, traces, eval datasets and caches.
Reference architecture
Residency is an architecture decision, not a setting. Every component that touches the data (gateway, vector store, logs, model endpoint, backups) has to live in the right region.
Rendering diagram…
Watch the side channels: an observability SaaS in another region, an evaluation pipeline that copies prompts to a central bucket, or a fallback that silently routes to a global endpoint when the regional one is down. Fallbacks should fail closed for residency-bound tenants.
Code
Pseudonymisation with Presidio, keeping a per-request mapping so the response can be re-identified for authorised users. The mapping never leaves your service.
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()
ENTITIES = ["PERSON", "EMAIL_ADDRESS", "PHONE_NUMBER", "CREDIT_CARD", "IBAN_CODE"]
def pseudonymise(text: str):
results = analyzer.analyze(text=text, entities=ENTITIES, language="en")
mapping, counters, ops = {}, {}, {}
for r in sorted(results, key=lambda r: r.start):
original = text[r.start:r.end]
if original not in mapping:
counters[r.entity_type] = counters.get(r.entity_type, 0) + 1
mapping[original] = f"<{r.entity_type}_{counters[r.entity_type]}>"
redacted = text
for original, token in sorted(mapping.items(), key=lambda kv: -len(kv[0])):
redacted = redacted.replace(original, token)
return redacted, {v: k for k, v in mapping.items()}
def reidentify(text: str, reverse: dict[str, str]) -> str:
for token, original in reverse.items():
text = text.replace(token, original)
return text
safe_prompt, reverse_map = pseudonymise("Refund Jane Doe, jane@example.com, card 4111 1111 1111 1111")
# send safe_prompt to the model; store reverse_map only in request memory
Presidio's built-in operators (replace, mask, hash, encrypt) cover most cases; the manual mapping above is for when you need reversible, consistent placeholders.
Trade-offs and pitfalls
- Detection is imperfect. NER misses unusual names and catches false positives. Tune recognisers for your domain and measure recall on labelled samples.
- Redaction can break the task. If the model needs to compare two addresses, masking both with
<ADDRESS>loses the signal. Use distinct placeholders. - Logs are the biggest leak. Prompt and response logging in tracing tools is often the largest store of personal data in the system. Redact before logging and set retention.
- Embeddings are personal data too. Vectors derived from personal text can be sensitive, and deletion requests must reach the vector store.
- Fine-tuning on production data. Training data is long-lived and hard to delete from weights. Redact first and document lawful basis.
- Residency claims drift. New features (tools, batch APIs, caches) may not share the same regional guarantees. Re-verify per feature.
Checklist
- Select only the fields each prompt actually needs.
- Detect and pseudonymise PII before model calls and before logging.
- Document provider retention terms and use zero-retention options where required.
- Pin regional endpoints for residency-bound tenants and fail closed.
- Keep vector stores, logs, evals and backups in the same region as the tenant.
- Make deletion requests reach prompts, logs, vector stores and datasets.
- Exclude unredacted personal data from fine-tuning sets.