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.
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:
Free-text user input. People type personal details into any text box,
whether or not you asked for them.
Retrieved context. A RAG pipeline pulls documents, tickets or emails
that contain personal data the user never typed. See
multi-tenant RAG isolation for
the access side of this.
Tool and database results. An agent calls a CRM lookup and the full
record goes into the context.
Logs and traces. Even if the provider call is clean, prompts stored for
debugging become a second copy of the data.
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.
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:
Approach
Example
Model can reason about it?
Restorable?
Best for
Delete
Call tomorrow
No, context is lost
No
Data the task never needs
Generic mask
Call [REDACTED]
Partly; two people look identical
No
Summaries where identity is irrelevant
Typed placeholder
Call [PERSON_1]
Yes, consistent per request
Yes, via mapping
Drafting replies, extraction, agents
Format-preserving fake
Call Alex Doe
Yes
Yes, via mapping
Tasks 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:
Patterns and checksums for structured values like card numbers, tax or
national ID formats and emails. Fast and precise when the format is fixed.
Named entity recognition (NER) for names, places and organisations,
which have no fixed format.
Context rules, for example treating a number near the word "account" as
more likely sensitive.
An LLM or classifier pass, which costs latency and money and, if it
runs on the external API, defeats the purpose unless it runs locally.
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:
Misses on unusual formats, typos, mixed languages or names that look
like ordinary words.
False positives that strip useful context, such as a product name that
resembles a person's.
Indirect identifiers. A job title, a rare condition and a city can
identify someone without any single field being obvious PII.
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:
Build a labelled set of realistic text with known PII, including your
ugly cases: pasted emails, mixed-language messages, formatted tables.
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.
Run it in CI alongside your
prompt regression tests,
and re-run when you change models, since placeholders can alter output
quality.
Log counts and types of redactions, never the values.
What should you do next?
Trace one request end to end and list every place personal data can enter
the prompt, including retrieval and tool results.
Put a single redaction gateway in front of every provider call.
Start with typed placeholders and a per-request mapping.
Build the labelled test set and set a recall target before launch.
Decide retention for prompts and traces, and write it down.
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
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.
Enterprise buyers ask to use their own LLM provider account or encryption keys. What BYOK means in an AI SaaS product, how to store customer keys, and what it does to cost, support and quality.
Enterprise buyers ask where prompts go and how long they live. How provider retention, zero data retention and your own logs fit together, and what to settle before the questionnaire arrives.
LLM output guardrails are a layered set of checks between the model and your user. Which checks are worth their latency, which are theatre, and how to place them.