All posts

LLM PII redaction: how to keep personal data out of the prompts you send to an LLM API

LLM PII redaction means detecting and masking personal data before a prompt leaves your system. Where to put it, how to restore values, and what automated detection cannot promise.

CodeKrypt 6 min read

A support agent pastes a customer thread into your AI feature. The thread has a name, a phone number, an address and half a card number. Your code builds a prompt and sends it to a third-party API. LLM PII redaction is the step that decides whether those values leave your system at all. Most teams add it after a security review asks where the data goes, which is a more expensive time to add it than before launch.

This post covers where to put redaction, how placeholders let you restore values afterwards, how to choose a detection approach, and the limit you cannot engineer away. It is about the domain, not any particular deployment; sources are linked inline.

Why does personal data end up in prompts?

Because prompts are assembled from whatever is convenient. Common routes:

OWASP's LLM02:2025 entry on sensitive information disclosure names personal information, financial and health records, credentials and confidential business data as the exposure risk, and lists sanitization, least-privilege access, clear retention policies and tokenization or pattern-based redaction among its mitigations. It also notes that system-prompt restrictions on what the model returns can be bypassed, for example by prompt injection, so an instruction like "never repeat phone numbers" is not a control.

Where should redaction sit in the request path?

At a single choke point between your application and the provider client, not scattered across features. If each feature redacts on its own, the next feature will forget.

Redaction flow: the app sends raw text to a redaction gateway, which swaps personal data for placeholders and keeps a private mapping. The LLM API only sees placeholders. The response returns to the gateway, which restores real values before it reaches the user.Your appRedactiongatewayLLM APIraw textplaceholders onlyplaceholders in replyreal values restoredprivate mapping stays on your side

The gateway does four things: detect, replace, remember the mapping for that request, and restore values in the response. The mapping never goes to the provider. If you already route calls through a wrapper for fallback or usage metering, that is the natural home.

Mask, hash or use placeholders?

The replacement choice changes answer quality, not just privacy:

ApproachExampleModel can reason about it?Restorable?Best for
DeleteCall tomorrowNo, context is lostNoData the task never needs
Generic maskCall [REDACTED]Partly; two people look identicalNoSummaries where identity is irrelevant
Typed placeholderCall [PERSON_1]Yes, consistent per requestYes, via mappingDrafting replies, extraction, agents
Format-preserving fakeCall Alex DoeYesYes, via mappingTasks sensitive to text shape

Typed, numbered placeholders are the usual default. They keep references consistent ([PERSON_1] stays [PERSON_1]) and tell the model what kind of thing was removed. Make the mapping per request, held in memory or a short-lived store, rather than a permanent lookup table that becomes its own sensitive dataset.

How do you detect PII reliably?

Detection is the hard part. Common building blocks:

Microsoft Presidio is an open-source example. Its documentation describes recognizers that combine NER, regular expressions, rule-based logic and checksums with context, support for multiple languages, and a separate anonymizer that de-identifies detected entities using configurable operators. If you have Indian ID formats or domain-specific identifiers, expect to write custom recognizers; defaults are tuned to what the library ships with.

Add the cost of this step to your latency budget: detection runs before the model is called, so it sits in the pre-inference portion of every request.

What can redaction not do?

Be honest about the ceiling. Presidio's own documentation states that because detection is automated, it cannot guarantee that all sensitive information is found, and that additional systems and protections should be employed. The same applies to any detector. Expect gaps:

So layer it. Minimise what you put in the prompt in the first place, apply least-privilege on retrieval, set retention on prompts and traces, and check the provider's data-handling terms for your plan. Tune the tradeoff deliberately: for high-risk fields, prefer over-redaction and accept some quality loss.

Does redaction help with Indian data protection law?

It supports the work but does not complete it. Data minimisation, purpose limitation and retention all bear on what you send to a model provider; the obligations are laid out in the DPDP Act and your AI product. Whether a given flow is lawful, and whether a provider counts as a processor for it, is a question for your counsel, not a redaction library.

How do you test it?

Treat redaction like any other behaviour that can regress:

  1. Build a labelled set of realistic text with known PII, including your ugly cases: pasted emails, mixed-language messages, formatted tables.
  2. Measure recall (what fraction of real PII was caught) separately from precision (how much non-PII was wrongly removed). Recall is the one that creates incidents.
  3. Run it in CI alongside your prompt regression tests, and re-run when you change models, since placeholders can alter output quality.
  4. Log counts and types of redactions, never the values.

What should you do next?

  1. Trace one request end to end and list every place personal data can enter the prompt, including retrieval and tool results.
  2. Put a single redaction gateway in front of every provider call.
  3. Start with typed placeholders and a per-request mapping.
  4. Build the labelled test set and set a recall target before launch.
  5. Decide retention for prompts and traces, and write it down.
  6. Review provider terms and have counsel confirm the legal position.

If you are building an AI product where customer data flows through model calls and want this designed in rather than retrofitted, that is the kind of architecture our AI SaaS service covers.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

What is LLM PII redaction?
It is detecting personal data such as names, phone numbers, card numbers and ID numbers in text, and masking or replacing it before the text is sent to a model provider. The goal is that the external API never receives the raw values.
Can automated PII detection catch everything?
No. Microsoft's Presidio documentation says automated detection cannot guarantee it finds all sensitive information, and that additional systems and protections should be used alongside it. Treat redaction as one layer, not a guarantee.
Should I replace PII with blanks or with placeholders?
Placeholders such as [PERSON_1] usually work better, because the model can still reason about who is who and refer to them consistently. You then map placeholders back to real values in your own system after the response returns.
Does redaction replace access controls?
No. OWASP's guidance on sensitive information disclosure lists sanitization alongside least-privilege access and limiting what data sources the model can reach. Redaction reduces what leaves your system; access control decides what enters the prompt in the first place.

Related reading

Chat on WhatsApp