Implementation Guide

Redacting PII from RAG Context Before It Enters the Prompt

Treat the boundary between retrieval and prompt assembly as an enforcement point. Add a runtime stage that inspects everything headed for the prompt, applies redaction based on who is asking and why, and logs what it did without storing the raw values. Ingestion-time redaction remains useful as a first layer, but it cannot account for missed entities, later content changes, or users with different entitlements.

Where PII can enter the prompt

The prompt is assembled from more than the retrieved text. Candidate chunks and their metadata leave the data store, and conversation history and tool output can also be part of the final prompt. Each of these is a path for PII to reach the model.

For that reason, the boundary between retrieval and prompt assembly is the right place for enforcement: it is the last point where everything headed for the prompt can be inspected together.

Designing the runtime redaction stage

The stage sits between retrieval and prompt assembly. It inspects everything headed for the prompt, applies redaction based on who is asking and why, and records the event without raw PII.

Runtime redaction between retrieval and prompt
  1. Retrieve and rerankCandidate chunks and metadata leave the data store.
  2. Detect and decideEntities are found and checked against identity-aware policy.
  3. TransformValues are masked, tokenized, pseudonymized, or dropped.
  4. Assemble and logThe sanitized prompt is built and the event is recorded without raw PII.

Ingestion-time, retrieval-time, and prompt-assembly-time redaction

Redaction can be applied at ingestion, at retrieval, or at prompt assembly. Ingestion-time redaction remains useful as a first layer, but on its own it cannot account for:

  • Entities the detector missed.
  • Content that changes after ingestion.
  • Users with different entitlements.

Choosing detection and transformation methods

The stage works in two parts: detection finds entities, and transformation decides what happens to each value. Depending on policy, a detected value is handled in one of these ways:

  • Masked
  • Tokenized
  • Pseudonymized
  • Dropped

Missed PII is the leakage risk, so evaluate detection by recall per entity type, separately from precision. Over-redaction has its own cost in answer quality, which is why both are tested (see the verification checklist below).

Access-aware policy and audit evidence

Redaction decisions should depend on who is asking and why, not only on what the text contains. Detected entities are checked against identity-aware policy before any transformation is applied.

Each event is logged without storing the raw values. These audit events also serve as evidence: sampling them from production supports periodic review of how the stage is behaving.

Verifying the stage works in production

  • Build a labeled test set from representative enterprise text, with seeded PII in varied formats, languages, tables, and scanned or OCR-derived content.
  • Measure false negatives per entity type. Missed PII is the leakage risk, so track recall separately from precision.
  • Test the assembled prompt, not just detector output, so metadata, history, and tool output are covered.
  • Add adversarial cases such as split or obfuscated values, PII deep in long chunks, and documents that instruct the model to reveal data.
  • Compare answer quality on a fixed question set before and after redaction to catch over-redaction.
  • Re-run the suite when the detector, policy, or corpus changes, and sample production audit events for periodic review.

Put policy enforcement at the retrieval-to-prompt boundary

If you are designing runtime controls for AI agents and RAG pipelines, we can discuss how identity, permissions, policy enforcement, and audit logging fit your architecture.

Talk to an Expert