Skip to main content
This example shows how to build a Retrieval-Augmented Generation (RAG) system that redacts PII from both the user’s query and the retrieved documents before they reach the LLM. Prefer to watch? This page as a silent, step-by-step video:

Overview

RAG systems often process sensitive documents (HR records, customer data, legal contracts). This example demonstrates:
  • Redacting PII from the user’s query and the retrieved documents in the LLM call
  • Scanning the LLM’s answer before it reaches the user
  • Maintaining answer quality while protecting data

Architecture

promptguard.init() patches the LLM call, so PromptGuard scans the query and the retrieved documents together when they are sent to the model. The vector search itself runs in your process and is not scanned: the raw query is embedded and compared locally, and never reaches the LLM unredacted.

Implementation

Setup

Basic RAG with Protection

With LangChain

RetrievalQA is gone from LangChain 1.x, so build the chain with LCEL. The inline PromptGuardRunnable sits between the retriever and the prompt: on a redact decision it passes the redacted context on, and on a block it raises PromptGuardBlockedError.

PII Types Detected

These are a few of the types PromptGuard redacts. The full list, including country-specific identifiers, is in PII and sensitive data protection.

Next Steps

Data Privacy Example

More data protection patterns

Threat Detection

Understand detection capabilities
Last modified on November 8, 2026