PII Redaction at the LLM Gateway Layer vs Application Layer
Gateway redaction enforces policy consistently; application-layer redaction leaves dangerous gaps.

PII redaction in an LLM pipeline is not a filtering feature. It's a policy decision about where sensitive data is allowed to go, and that decision only holds up if it's enforced the same way for every model, every provider, and every application that touches it. That kind of consistency is possible at the gateway layer. It is not possible at the application layer, no matter how careful any one team is.
PII enters an LLM request at four separate points: what the user types, what a retrieval system pulls in as context, what a tool call returns, and what the model outputs. Most teams build filters for exactly one of those, the user's typed input, and stop there. A documented example describes a support agent's RAG system pulling up a ticket containing a real government-issued identification number that no user ever typed into the chat window. The model reads it in its retrieved context and quotes it back, word for word, in its answer. Nobody copy-pasted anything. The leak happened entirely inside the pipeline.
That's a different animal than a normal API data leak. A traditional request has one clear payload, sent by one client, to one endpoint. An LLM request gets assembled at runtime from a pile of different sources: system instructions, conversation history, documents pulled from a vector store, results from tool calls. Any one of those segments can carry PII the application never touched directly. Worse, the model can recombine pieces of that context into something new, a paraphrase, an inference, a "Sarah at the downtown office" instead of a name and address, and that's not a copy-paste problem regex was ever built to catch. And provider-side safety filters, however good, only run after the bytes have already crossed the wire, as philterd.ai puts it. By the time a hosted model's own filter looks at the input, the data already reached the provider. The disclosure already happened.
That distinction is what an auditor cares about. Under HIPAA, a hosted model handling PHI is doing so on the deployer's behalf, filter or no filter. Under GDPR, personal data handed to an independent third-party controller has left the original controller's processing boundary; contracted processors stay bound by GDPR, but the controller is still accountable for how those processors behave. Under PCI DSS, cardholder data reaching any outside service pulls that service into audit scope. None of these frameworks ask whether the provider has a good filter. They ask whether sensitive data reached the provider at all.
This scenario is grounded in the finding that Cyberhaven's 2026 AI Adoption and Risk Report found that 39.7% of all enterprise AI interactions involve sensitive data, and a large share of that runs through personal, unmanaged accounts that never... Cyberhaven's AI Adoption and Risk Report found that 39.7% of all enterprise AI interactions involve sensitive data, and a large share of that runs through personal, unmanaged accounts that never touch corporate controls at all, 58.2% of Claude usage and 32.3% of ChatGPT usage in the report. The 2025 OWASP Top 10 for LLM Applications ranks Sensitive Information Disclosure second on the list (LLM02:2025), covering PII, credentials, and health records moving through both inputs and outputs. The threat model spans four insertion points. The next question is why fixing just one of them, inside each application, doesn't actually solve the problem.
Why application-layer redaction fails as a compliance control at organizational scale
Application-layer redaction means each team builds its own detection logic, wires up its own filters, and produces its own evidence that the filters ran. It sounds reasonable until you count how many times "each team" has to get it right.
Every new model integration is a new surface. Every new internal service is a new surface. Every team that starts experimenting with an AI feature adds another codebase that needs its own PII logic, built and maintained separately from every other one. When policy changes, someone has to go update all of them by hand. Coverage ends up uneven because the design guarantees unevenness. The Northwind scenario is the natural result: a team remembers to sanitize user input and forgets that RAG-retrieved context needs the same treatment. There's no systemic backstop catching that gap, because there's no system, just a collection of separate implementations each doing its own thing.
That fragmentation is most visible in the audit trail. SOC 2 Type II wants continuous, immutable logs proving that controls ran on every request, not most of them. Per-application, ad-hoc logging doesn't produce that kind of record; it produces a folder of inconsistent evidence from a dozen different teams, none of which can vouch for the others. As the getmaxim.ai analysis puts it, for regulated industries, that is not a defensible compliance posture. An auditor doesn't want proof that some calls were governed. They want proof that all of them were, and fragmented, per-service logging can't give them that.
Agentic AI makes the math worse. Gartner projects that 40% of enterprise applications will embed task-specific AI agents by the end of 2026, up from under 5% in 2025. Each agent can fire off multiple outbound model calls per user turn, not one. Multiplying that against a dozen internal services each maintaining their own redaction code causes the surface area application-layer enforcement has to cover to grow a lot faster than any team's ability to keep up with it by hand.
There's also the risk of forgotten clients. Application-layer redaction assumes every service calling a model has implemented the control correctly. One overlooked internal tool, one legacy script nobody updated, one contractor's quick integration, and that client reaches the provider with zero redaction and no backstop catching it at the network level.
None of this means application-layer filtering is worthless. It has a real role, covered further down. It just can't be the organization's primary compliance control, because nothing about its structure lets it scale past a handful of teams without gaps opening up. So what does work at that scale?
What gateway-layer redaction enforces and how it works
Routing every outbound call to a model provider through a single reverse proxy gives you something application-layer enforcement structurally can't: a forgotten or rogue client physically cannot reach a provider without going through the redaction check first. The architecture makes the gateway the hardest layer to bypass, because the network path itself enforces it. It's not that gateway enforcement is stricter in spirit. It's that the network path itself doesn't allow a way around it.
One enforcement point means one place to change policy. Update a rule at the gateway, and every connected application inherits it automatically, no code changes, no rollout across a dozen repos. The gateway also becomes the single place that logs which detector fired and what action it took, instead of scattering that logic (and that risk) across every service that happens to call a model.
Timing matters here. Input redaction runs before the request reaches the provider, so the provider only ever sees tokens or placeholders, never the original value. Output redaction runs after the model generates a response but before that response goes back to the caller, catching PII the model invented or reconstructed from context it was given. Api7.ai notes that output redaction can't undo a disclosure that already happened upstream. If the actual risk is unauthorized data reaching the provider in the first place, an output-only policy is already too late to matter. Input redaction is the control that actually prevents the disclosure; output redaction is the control that catches what the model does with what it already has.
A well-designed gateway signals its decision explicitly rather than silently altering content. One design pattern returns three distinct outcomes: a pass response when content clears validation, a block response (HTTP 446, in one implementation's scheme) with violation details when the request gets rejected outright, and a modify response (HTTP 246) when PII gets redacted and the request proceeds, with redaction counts included. That distinction lets the calling application actually react to what happened instead of just receiving quietly altered text and never knowing.
Masking and blocking aren't interchangeable, and picking the wrong one per entity type creates its own risk. Mask when the task doesn't need the real value, a ticket summarizer doesn't need a customer's actual email address to do its job. Block when a substituted placeholder could produce a wrong or dangerous outcome, a payment flow that needs an exact account number shouldn't proceed on a value like [BANK_CARD_REDACTED]; route that case to a non-LLM path instead. Monitor mode, which just logs matches without acting on them, has a real use in tuning detectors before going live, but api7.ai's warning is blunt: don't leave a policy sitting in monitor mode while describing the data path to anyone as protected.
Permanent redaction also breaks things it shouldn't. Swap "Maria Gonzalez" for a fixed string like [REDACTED], and any downstream logic that needs to reconnect the model's answer to a specific customer record loses that thread entirely. A session-scoped vault approach handles this better: swap the name for a token like [PERSON_1], keep the real mapping stored locally and temporarily, then de-mask the response for the authorized user on the way out. The model never sees the real name. The application still knows exactly who it's talking about. This exact pattern appears in zero-data-retention agent designs, keeping the mapping local so de-masking happens without the LLM ever touching the original value.
All of this produces a byproduct worth its own mention: a gateway sitting in the middle of every call naturally logs identity, provider, model, token counts, cost, latency, result status, and redaction counts, for every request, without any application needing to add its own instrumentation. That's the audit trail issue from the last section, mostly solved as a side effect of where the enforcement sits.
None of it works, though, without a detection engine that actually finds the PII in the first place. That's where the tradeoffs get real.
Detection methods and their accuracy-latency tradeoffs at the gateway
PII splits into three detectability tiers, and each one needs a different tool. Structured PII, government-issued identification numbers, credit card numbers, IBANs, email addresses, follows fixed formats with checksum math behind them (Luhn for card numbers, mod-97 for IBANs). Regex plus a checksum check catches these with close to zero false positives; there's not much ambiguity in a 16-digit number that passes a Luhn check. Semi-structured secrets, API keys, JWTs, PEM keys, need entropy analysis layered with pattern matching, since these don't follow a single universal format. Unstructured PII, names, addresses, relationships between people, has no regex that can catch it, because there's no fixed pattern for "this string is a person's name." That tier needs named-entity recognition or an actual language model doing the reading.
Speed and accuracy trade off hard here. Benchmarks cited by pctechmag.com show that plain regex adds under 2 milliseconds to a request. An NER model adds around 35 milliseconds. An external PII detection API adds roughly 180 milliseconds to time-to-first-token, and that cost lands on every single request, inline, every time.
Accuracy numbers look great in vendor pitches and fall apart out of distribution. A DeBERTa model fine-tuned on a fixed entity set can hit an F1 score of 0.976 on data that resembles its training set. Test that same class of model on data it wasn't tuned for, and the picture changes fast. PIIBench ran eight different detection systems across ten datasets covering 48 entity types and found that even OpenAI's Privacy Filter dropped from a 0.96 score down to as low as 0.18 once the data moved out of distribution, pctechmag.com reports. LLM-based detectors, by contrast, hit the highest recall in that same testing, 95.8%, because a language model actually reads for context. It can flag PII in a sentence like "please forward this to Sarah at the downtown office," where there's no structured pattern at all, just implied identity, ertas.ai reports. No single method wins on both precision and recall across every entity type. Whatever gets deployed needs to be tested against actual production traffic, not a vendor's benchmark set, before anyone sets a confidence threshold and walks away.
Guidance published by mlflow.org recommends layering detectors rather than picking one. Regex and checksum validation run first, on every request, under 2 milliseconds, catching structured PII cleanly. NER, the kind Microsoft Presidio runs on a spaCy backend, runs second, adding tens of milliseconds, which most production workloads can absorb without users noticing. A local LLM handles contextual edge cases that the first two passes can't resolve deterministically, reserved for workloads where the compliance stakes justify the added latency. An external PII API, with its roughly 180-millisecond cost, makes sense for high-compliance workloads that can tolerate the delay, but it doesn't belong inline on every request by default.
A 2026 arxiv.org paper on the architecture describes Microsoft Presidio as the current reference point for how this layering gets built in practice: regex recognizers combined with a spaCy NER backend, entity types configurable through a plugin system, and an anonymizer module that supports redaction, hashing, and masking as separate output modes. Small models running locally on the gateway itself, rather than called out to a remote service, can reduce latency significantly compared to the roughly 180-millisecond cost of an external PII API, and they keep the data inside the deployment perimeter instead of shipping it to yet another external endpoint for inspection.
The hardest failure mode stays the same regardless of which detector stack gets used: paraphrased or inferred PII, identity reconstructed from context rather than stated outright, which regex and NER both miss by design. Catching that requires contextual detection and active output monitoring, and tracking false negative rates by entity type against live traffic, not just a one-time benchmark run before launch. And not every entity type deserves the same response. An email address, a government-issued identification number, and a JWT carry different risk levels and different recovery paths if they leak; policy at the gateway should treat them differently, not apply one blanket rule to all three.
Good detection closes the enforcement gap. It still isn't the whole compliance story on its own.
Completing the compliance architecture: ZDR, audit logs, and access controls around gateway redaction
Redacting data before it reaches a provider doesn't automatically mean the data's gone. As practitioners note, avoiding prompt logs at one layer means nothing if a cache, a gateway, or the provider itself still retains the same data somewhere else in the chain. Zero Data Retention agreements at the provider level and audit logging on the deployer's side aren't two ways of solving the same problem. They're two different problems, and the EU AI Act treats them that way: logging obligations for high-risk AI systems have to sit under the deployer's own control, not depend on whatever retention policy the provider happens to run.
ZDR means the provider doesn't store requests or responses once inference finishes, so there's no log entry sitting around to get breached, no prompt to respond to in a subpoena, no chance it ends up folded into some future training run. Abubakarsiddik.site reports that OpenAI's default API retention runs 30 days before auto-deletion, reduced from the prior 30-day default in September 2025. Anthropic's ZDR option works differently: it requires a contract addendum through enterprise sales plus Anthropic's approval, and it covers eligible Anthropic APIs tied to a Commercial organization API key, including Claude Code accessed via that API, along with Claude Code for Enterprise plans. The same reporting states that it does not cover Claude Free, Pro, or Max consumer accounts. That last detail matters enormously in practice: an employee running personal Claude Pro to get work done is operating entirely outside whatever ZDR agreement their company negotiated.
That's why ZDR has to be enforced as a routing rule at the gateway rather than as a contract sitting in a legal filing cabinet somewhere. Requests should only route to providers under a ZDR agreement, and enforcing that at the gateway level means no application, however it gets updated down the line, can accidentally route around it.
Audit logs are the evidence layer for all of this. SOC 2 Type II wants continuous, immutable logs, and a gateway that emits structured records from day one, identity, provider, model, token counts, cost, latency, result status, redaction counts, is a far easier thing to certify than a pile of applications each logging in its own ad-hoc way, bolted on after the fact. The EU AI Act's Annex III obligations for high-risk systems become enforceable starting December 2, 2027, after Regulation (EU) 2026/1744 pushed the original August 2, 2026 deadline back. Deployers in the EU will need to show logging that sits under their own control, not something they're borrowing from a provider's retention settings. HIPAA wants demonstrable controls over PHI. GDPR Article 5 wants data minimization. Both frameworks want evidence of actual controls.
Access control closes the loop. Role-based access at the gateway decides which teams, which services, and which API keys can reach which providers and which models in the first place, and redaction policy can scope itself by caller identity, applying stricter rules to higher-risk applications without touching the rules governing everyone else. Detection catches the data. Routing decides where it's allowed to go. Logging proves both of those things actually happened, on every request, not just the ones someone remembered to check.
Sources
- PII Redaction: LLM Gateway Layer vs Application Layer
- PII Redaction in an AI Gateway: Protect Sensitive Data Before It Reaches an LLM - API7.ai
- How to Redact PII Before Sending to an LLM: Chat, RAG, and AI Agents
- PII Redaction for LLMs in 2026: How to Strip Sensitive Data Before It Leaves Your Perimeter
- Developers: Stop 80% of PII Before Any LLM Call | MLflow
- arxiv.org


