Adding PII redaction to Azure API Management AI Gateway policies
Implement PII redaction in the APIM policy pipeline by inspecting and redacting the prompt in the inbound section before the model call, inspecting the completion in the outbound section, deciding in on-error whether a failure blocks or passes traffic, and emitting a structured record of every decision. Detection can run as in-policy logic, as a call to an external detection service, or in a dedicated enforcement layer in the path. Redaction reduces exposure but is not a formal privacy guarantee, so test it and document its limits.
Map redaction to the four policy stages
- 1
1. Inbound
Parse the prompt, detect PII, apply the configured action (redact or block), and attach a decision record to the request context.
- 2
2. Backend
Send only the sanitized body to the model endpoint. Never fall back to the original payload on a retry.
- 3
3. Outbound
Inspect the completion, apply redaction or block, and complete the decision record.
- 4
4. On-error
Handle detector failure, timeout or malformed payloads with a defined fail-open or fail-closed outcome, and emit the record either way.
Test coverage and false negatives
- Build a labeled test set that includes structured identifiers, free-text names and addresses, and PII embedded in code, JSON and multi-turn context.
- Measure false negatives separately from false positives, and report both per entity class.
- Test completions as well as prompts, including cases where the model repeats or invents identifiers.
- Test streaming boundaries by splitting entities across fragments.
- Inject detector timeouts and malformed bodies to confirm on-error behavior.
- Re-run the set whenever a policy, detector or model changes.
Why redaction belongs in the gateway path
Teams that route LLM traffic through an API gateway usually start with application-level controls: each client team strips identifiers in its own way, or a reviewer samples logs after the fact. Coverage is inconsistent, and a log review only tells you that PII reached a model endpoint after it did.
NIST SP 800-228 describes the API gateway as a policy enforcement point and recommends applying data-removal rules as a cross-cutting policy rather than in each service's business logic. That is the argument for putting PII redaction in the APIM pipeline. The policy runs once, in the execution path, for every caller.
Be precise about what redaction is. NIST defines it as removal of information, also called suppression, and treats it as one de-identification technique among several. NIST also states that redaction alone does not provide formal privacy guarantees, and that selective removal can reduce the usefulness of what remains. For prompts, that means an over-aggressive policy can degrade answers. Describe the control as risk reduction with a measured residual risk, never as anonymization.
Choose where detection runs
Detection placement drives accuracy, latency and what evidence you can produce. There are three common options, and they can be combined, for example pattern rules first and a service call for ambiguous text.
- In-policy logic: Pattern matching for structured identifiers such as account or ID formats. No extra network hop and simple to deploy. Weak on names, addresses and other free-text PII, and rules become hard to maintain as they grow.
- External detection service call: A policy such as send-request calls a service, for example Azure AI Language PII detection, and applies the returned entity spans. Better coverage of free text. Adds a network hop per payload, a dependency whose failure needs handling, and your own code to apply spans and record decisions.
- Dedicated enforcement layer: A component in the path that detects, applies allow, redact or block per policy, and writes decision evidence. Keeps policy definitions and evidence out of XML policy code. Adds a component to run or adopt and a policy change process to own.
Streaming, latency and fail-open versus fail-closed
Streaming is the hardest part of outbound redaction. A streamed completion arrives in fragments, and an identifier can be split across fragments. You have two honest options. Buffer the response until it can be inspected as a whole, which protects the caller but removes the incremental experience and increases time to first token. Or inspect fragments or sliding windows as they pass, which preserves streaming but can miss entities that span boundaries. Decide per workload, and state the choice in the design. If a route cannot tolerate buffering, consider whether it should carry sensitive data at all.
Latency compounds in the same way. Every detection call sits in the request path, and outbound inspection adds a second. Set explicit timeouts, measure added latency at realistic payload sizes, and decide what happens to oversized payloads: inspect in chunks, reject, or route to a stricter path.
The on-error section forces the fail-open or fail-closed decision. Fail-closed blocks traffic when detection cannot complete, which protects data and costs availability. Fail-open passes traffic through, which preserves availability and leaves a gap. For prompts bound for external model endpoints that may carry regulated data, fail-closed is the defensible default. Where you choose fail-open for a low-risk route, record each uninspected request as such so the gap is visible rather than silent.
Evidence, gateway limits and when a dedicated layer fits
Audit questions are about decisions: what was detected, what action was taken, under which policy version, and when. Gateway diagnostics typically show that a request occurred. A decision record has to be produced deliberately by your policy or enforcement layer, with fields such as entity class, action, policy version, correlation identifier and timestamp. Do not write the raw PII into that record. Standards-based claims need care too: NIST material describes de-identification criteria, but a record of your own decisions does not by itself demonstrate compliance with HIPAA, GDPR or NIST AI RMF. Connect the control to your existing governance and compliance processes.
Policy code has limits. Free-text detection is imprecise, streaming handling is awkward in policy expressions, and maintaining detection rules, versioning and evidence output in gateway XML becomes its own engineering program.
Trussed is one option when those limits matter. It enforces policy in the execution path, with allow, redact and block outcomes enforced at runtime, not reviewed after the fact, and generates audit-ready evidence from that enforcement. It supports self-managed deployment in the customer cloud with configurable retention and data residency. If your requirements are a few structured identifiers on a single route, custom policy may be enough. If you need consistent outcomes across many models and routes plus durable evidence, a dedicated enforcement layer is worth evaluating.
Where PII redaction sits in the APIM policy pipeline
Inbound
Detect and redact PII in the prompt before it leaves the gateway.
Backend
Forward only the sanitized payload to the model endpoint.
Outbound
Inspect the completion and redact or block before the caller receives it.
On-error
Apply an explicit fail-open or fail-closed decision and record it.
Evaluate in-path PII enforcement for your AI traffic
Review your detection placement, failure behavior and evidence design with an expert.
Talk to an ExpertHow does your AI governance program compare?
See where your program has gaps in less than 2 minutes.