Guide · Updated 29 November 2026
How to redact PII before sending it to an LLM
Every prompt, RAG chunk and tool result you send to a hosted model leaves your network. This guide covers three ways to remove personal data first, the formats that slip through once you run real volumes, and a checklist for production.
1. What counts as PII in an LLM prompt
Start with the obvious identifiers: email addresses, phone numbers, postal addresses, people's names, national ID numbers, bank account numbers (IBANs) and card numbers. Then add what is specific to AI workloads: API keys and tokens pasted into prompts by developers, customer names inside support tickets and CRM notes, and your own confidential terms such as product codenames or client lists.
2. Option 1: patterns in your own code
The quickest start is a set of regular expressions that run before you call the model. It works well for structured values like email addresses and is easy to reason about.
Python: a minimal pattern-based redactor
import re
PATTERNS = {
"EMAIL": re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+"),
"PHONE": re.compile(r"\+\d{1,3}[\s\d()-]{7,16}\d"),
"IBAN": re.compile(r"\b[A-Z]{2}\d{2}(?:\s?[A-Z0-9]{4}){3,7}\b"),
}
def redact(text: str) -> tuple[str, dict]:
mapping = {}
for label, pattern in PATTERNS.items():
for i, value in enumerate(dict.fromkeys(pattern.findall(text)), 1):
token = f"[{label}_{i}]"
mapping[token] = value
text = text.replace(value, token)
return text, mappingKeep the mapping if you want to put values back into the answer. The limits show up quickly: this phone pattern only matches numbers written with a country code, the IBAN pattern accepts strings that fail the checksum, and nothing here finds a person's name.
3. Option 2: add name recognition
Names and company names don't follow a pattern, so you need a named-entity recognition (NER) model. Open-source options such as Microsoft Presidio or a token-classification model from Hugging Face detect PERSON and ORG entities in free text. Run the model inside your own infrastructure: sending text to a third-party API to find out whether it contains personal data defeats the purpose.
Expect to tune it. Job titles (“CFO”), acronyms (“EU”) and product names are common false positives, and over-masking makes the model's answers worse.
4. Option 3: reversible redaction through a gateway
The two options above are one-way unless you build the restore step yourself, and agents make that harder: the model writes a placeholder into a tool call, a streamed reply or a follow-up question, and each of those needs the real value back. A redaction gateway sits between your code and the model and handles both directions.
Spendplane: change the base URL, keep your code
from openai import OpenAI
client = OpenAI(base_url="https://api.spendplane.com/v1",
api_key="YOUR_SPENDPLANE_KEY")
# Sent: "Refund DE89 3704 0044 0532 0130 00 for anna@example.com"
# Model: "Refund [IBAN_1] for [EMAIL_1]"
# Returned: the model's answer with the real IBAN and email restoredSpendplane is one such gateway. It also covers coding agents (Cursor, Claude Code, Cline), MCP servers and browser chats, which matters once more than one team uses AI. See how the Spendplane LLM redaction tool works.
5. What breaks at scale: lessons from 60,000 CRM records
We generated 60,000 fictional sales leads from 10 countries (401,930 personal details) and ran them through our own masking in pages of 200, the way an agent reads a CRM. Contact fields were easy. The hard cases were all inside free-text notes:
- Local phone formats without a country code:
07700 069061(UK),635-090-502(Poland),605 41 31 41(Spain),06 12 34 56 78(France). A pattern that only expects+misses all of them. - Accented email addresses such as
chloé.lefèvre@example.org, which many simple patterns cut in half. - Company legal forms: GmbH, AB, S.L., sp. z o.o., S.p.A. Each country has its own.
- Values that must stay readable: dates, postcodes, order numbers and amounts look like phone numbers to a loose pattern, and masking them breaks the agent's reasoning.
Our first run leaked 3,016 of 401,930 values, all of them local phone numbers inside notes. After adding those formats, the same 60,000 records ran with zero leaks, and dates, postcodes and labelled order numbers stayed readable.
The takeaway: test with data that looks like your real data, in every country you sell to, and measure both leaks and over-masking. A redactor that looks perfect on ten examples can leak hundreds of values on a full CRM export.
6. RAG pipelines and AI agents
In a RAG pipeline, redact chunks before they are sent to the model at query time, not only when you build the index; documents change, and retrieved chunks are the part most likely to contain customer data. If you also send chunks to a hosted embedding API, redact them before that call too.
For agents, redact tool results, not just the user's prompt. An agent connected to a CRM or database pulls in far more personal data than anyone types. Spendplane's MCP firewall masks tool results and restores placeholders in the agent's next tool call, so a query like Email = [EMAIL_1] still finds the right record.
7. Production checklist
- List every path where text reaches a model: apps, scripts, IDEs, agents, browser chats.
- Redact prompts, retrieved chunks and tool results, not only the user's message.
- Validate structured values (IBAN checksums, card Luhn checks) to avoid false positives.
- Cover local formats for every country in your data.
- Keep placeholders consistent within a conversation and restore them in replies and tool calls.
- Run name recognition inside your own infrastructure.
- Log what was caught (types and counts), never the values themselves.
- Re-test at volume after every change, and track leaks and over-masking separately.
Skip building it yourself
Spendplane does all of this behind one base URL, for every team and tool in your company.
See the LLM redaction tool