Why redaction belongs in the request path
Redact PII as a runtime control in the request path, not as a library each application adopts on its own. The request path is where the full assembled payload bound for the OpenAI or Anthropic API can be inspected, so it is where the control can see everything that is actually sent.
Three places to enforce redaction
The enforcement point matters as much as the detector. Placing redaction in the request path, rather than leaving each application to adopt its own library, gives one place to apply policy. Egress can then be restricted to that enforcement point, so direct API calls cannot bypass the control.
Redaction in the request path
The control runs as a sequence of five stages on every outbound request.
- DetectScan the full assembled payload, not only the user message.
- SubstituteReplace PII with masks or typed placeholders under policy.
- ForwardSend the redacted request to the OpenAI or Anthropic API.
- Re-hydrateRestore original values only where policy allows.
- LogRecord redaction events without storing raw PII.
Detection and substitution tradeoffs
Detect personal data in the full outbound payload, including retrieved documents and tool outputs, not only the user message. Replace what is found with masks or typed placeholders under policy.
Detection quality varies by entity type, so measure false negatives per entity type using a labeled test set built from real prompts, documents, and tool outputs.
Re-hydration, streaming, and tool calls
Keep any reversal mapping in a scoped and expiring store, and restore original values only where policy allows.
Streaming responses need testing at chunk boundaries to confirm that placeholders restore correctly, or not at all, as policy requires. Tool outputs are part of the outbound payload and should be scanned like any other content.
Failure modes, logging, and governance
Decide explicitly between fail-open and fail-closed behavior for detector errors and timeouts, and confirm that the system behaves as the chosen policy states.
Log redaction events as metadata without raw values. Logs, traces, and mapping stores should retain no raw PII beyond its expiry.
Verification checklist
- Build a labeled test set from real prompts, documents, and tool outputs, and measure false negatives per entity type.
- Attempt to bypass the control with direct API calls, and restrict egress to the enforcement point.
- Test streaming responses at chunk boundaries and confirm placeholders restore correctly or not at all, as policy requires.
- Force detector errors and timeouts and confirm behavior matches the chosen fail-open or fail-closed policy.
- Inspect logs, traces, and mapping stores to confirm no raw PII is retained beyond its expiry.
- Re-run the tests when detectors, policies, models, or prompt templates change.