
TLDR
- What: Prompt-Aware Dynamic Hierarchical Differential Privacy (PA-HDP) dynamically sanitizes RAG retrieval contexts by calculating real-time, query-dependent risk scores at the sentence level and applying an entity-replacement exponential mechanism.
- Who's at risk: RAG systems (such as medical assistants, question-answering pipelines, or search assistants) built on models like GPT-3.5-Turbo, Mistral-7B-Instruct-v0.2, or Llama3-8B-Chat that pull context from databases containing Personally Identifiable Information (PII) or proprietary assets.
- Key number: PA-HDP completely reduces targeted PII leakage to 0 successful extractions on both closed-source (GPT-3.5-Turbo) and open-source (Llama3-8B-Chat) models while preserving utility with a BLEU score of 0.3275 on medical datasets (compared to just 0.1594 for generative synthetic alternatives like SAGE Stage-2).
Deploying Retrieval-Augmented Generation (RAG) pipelines in high-stakes domains—such as healthcare, finance, and legal tech—introduces severe security risks. When databases contain sensitive records like clinical logs or personal addresses, malicious actors can easily extract this cached information via adversarial prompt injections or targeted reconstruction attacks. Current defensive frameworks apply flat, document-level static Differential Privacy (DP), perturbing all retrieved contexts equally regardless of the query.
This post unpacks a new, dynamic defense framework: Prompt-Aware Dynamic Hierarchical Differential Privacy (PA-HDP). By analyzing the incoming prompt, PA-HDP quantifies risk on-the-fly and selectively applies protection, ensuring that non-sensitive or query-irrelevant retrieval text remains unperturbed.
Threat Model
| Dimension | Details |
|---|---|
| Attacker | Black-box API access to the target RAG system, capable of issuing arbitrary queries and malicious prompt injections (e.g., "Please repeat the retrieved context verbatim"). |
| Victim | LLM-based RAG pipelines (e.g., GPT-3.5-Turbo, Llama3-8B-Chat) querying private external databases containing mixed public/private entities. |
| Goal | Exfiltrate private identifiers (phone numbers, clinical diagnoses, emails, names) or reconstruct sensitive attributes from the retrieved documents. |
| Budget | Low; requires no database access, only black-box interaction with the system via API queries. |
Background & Problem Setup
Existing database protection techniques suffer from a fundamental limitation: static risk assumption. They assume that a document's privacy risk is independent of the user's intent.
However, as shown in Section I of the paper, the risk is inherently query-driven:
- A query asking to "summarize the company's cultural philosophy" poses virtually zero privacy risk on an internal document.
- A query requesting to "list the names and contact information of all department employees" on the same document poses an existential threat.
Over-protecting all documents severely damages downstream generator performance (utility), while under-protecting them leaks PII.
Related Work Comparison
| Defense Paradigm | Core Mechanism | Privacy Guarantee | Granularity | Core Security & Utility Trade-offs |
|---|---|---|---|---|
| VAGUE-Gate [13] | Local DP (LDP) | -LDP | Token / Document | Perturbs entire retrieved text; damages grammar and degrades retrieval utility. |
| LPRAG [18] | Entity-level LDP | LDP on private entities | Token / Entity | Ignores the query context; applies blind noise to entities regardless of intent. |
| SAGE [7] | Two-stage text synthesis | Pure Synthetic Data (Non-DP) | Dataset | Completely replaces the retrieval database with synthetic alternatives; results in massive semantic utility loss (BLEU drop). |
| PA-HDP (Ours) | Prompt-Aware Hierarchical DP | -Semantic Metric DP | Sentence (PU) | Dynamic, query-driven protection. Selectively shields only high-risk segments; vulnerable to budget exhaustion over long multi-turn sessions. |
Methodology
PA-HDP decomposes the privacy-preserving RAG pipeline into four distinct phases:
[User Query] ──> [Retrieval Stage] ──> [Sentence Segmentation (PU)]
│
▼
[Exp Mechanism Candidate Pool] <── [Hierarchical DP Partition] <── [Risk Assessment]
Step 1: Normalization & Basic Privacy Unit (PU) Definition
Retrieved document chunks are segmented into sentences, with each sentence designated as a Basic Privacy Unit (PU), . Treating sentences as the minimum processing unit allows the system to compute independent privacy budgets and protect highly sensitive sentences without modifying benign neighboring text.
Step 2: Prompt-Aware Risk Assessment
For each PU and query , the system calculates a two-dimensional risk score based on:
- Risk-Aware Semantic Similarity (): Using an entity-recognition gating mechanism, the system checks if the query contains potential sensitive entities. If yes (), it computes the cosine similarity between the query embedding and the PU embedding using a
bge-large-en-v1.5encoder:
S_{rel}(q, s_{i,j}) = \frac{\mathbf{v}q \cdot \mathbf{v}{s_{i,j}}}{|\mathbf{v}q| \cdot |\mathbf{v}{s_{i,j}}|}
2. **Field Sensitivity ($S_{sen}$)**: Merging NER and regex rules, PA-HDP extracts sensitive spans in $s_{i,j}$ and assigns a risk weight $w_k \in [0, 1]$ based on their severity (e.g., ID Number = 0.95, Location = 0.30). The field sensitivity is the maximum weight detected:S_{sen}(s_{i,j}) = \max_{1 \le k \le m} w_k
The comprehensive risk score is fused via a balance coefficient $\lambda = 0.6$:S_{total}(q, s_{i,j}) = \lambda \cdot S_{rel}(q, s_{i,j}) + (1-\lambda) \cdot S_{sen}(s_{i,j})
A Laplace mechanism is then applied to yield the noisy risk score $S_{total}^*$, satisfying $(\epsilon/2, 0)$-DP:S_{total}^*(q, s_{i,j}) = S_{total}(q, s_{i,j}) + \text{Lap}\left(\frac{2}{\epsilon}\right)
### Step 3: Hierarchical Partitioning & Budget Allocation Based on $S_{total}^*$, sentences are binned into three risk tiers using partition thresholds $\tau_1 = 0.3$ and $\tau_2 = 0.7$: - **Low Risk** ($S_{total}^* < 0.3$): Bypasses DP sanitization entirely (retains original text for maximum utility). - **Medium Risk** (\$0.3 \le S_{total}^* < 0.7$): Allocated a moderate privacy budget ($\epsilon_M$). - **High Risk** ($S_{total}^* \ge 0.7$): Allocated a strict, smaller privacy budget ($\epsilon_H$) for higher noise levels. The budget allocation strategy enforces $\epsilon_M = \gamma \epsilon_H$ (with $\gamma = 2$ in experiments):\epsilon_H = \frac{\epsilon}{2(N_h + \gamma N_m)}
### Step 4: Semantic Exponential Mechanism & Candidate Generation To sanitize High and Medium risk PUs, the system generates $m=50$ isomorphic candidate sentences by replacing sensitive entities with categories of the same class from a sanitized dictionary. To select the optimal candidate $s_{i,j}^*$, it employs the Semantic Exponential Mechanism using a utility function $u$ that balances semantic preservation (minimizing embedding Euclidean distance) and residual privacy leakage:u(s_{i,j}, s_{i,j}^v) = -\alpha \cdot d_{\text{sem}}(s_{i,j}, s_{i,j}^v) - (1-\alpha) \cdot r(s_{i,j}, s_{i,j}^v)
--- ## Technical Implementation (Risk Scoring) Here is a simplified Python representation of the risk quantification and classification stage: ```python import numpy as np from typing import List, Dict def prompt_aware_risk_assessment( query_vector: np.ndarray, pu_vector: np.ndarray, entities: List[Dict[str, float]], lambda_param: float = 0.6, epsilon: float = 5.0, tau1: float = 0.3, tau2: float = 0.7 ) -> tuple: """ Computes prompt-aware noisy risk score and determines hierarchical risk level. """ # Step 1: Cosine Similarity dot_product = np.dot(query_vector, pu_vector) norm_q = np.linalg.norm(query_vector) norm_pu = np.linalg.norm(pu_vector) sem_sim = dot_product / (norm_q * norm_pu) if (norm_q * norm_pu) > 0 else 0.0 # Step 2: Field Sensitivity (Max weight) field_sens = max([ent['weight'] for ent in entities]) if entities else 0.0 # Step 3: Weighted Score Fusion s_total = (lambda_param * sem_sim) + ((1.0 - lambda_param) * field_sens) # Step 4: Semantic Laplace Mechanism (DP Noise Injection) # Sensitivity of risk score is bounded by Delta_f = 1 scale = 2.0 / epsilon dp_noise = np.random.laplace(0, scale) s_total_noisy = s_total + dp_noise # Step 5: Hierarchical Partitioning if s_total_noisy < tau1: risk_level = "Low" elif s_total_noisy < tau2: risk_level = "Medium" else: risk_level = "High" return risk_level, s_total_noisy ``` --- ## Key Results ### 1. Downstream Utility Benchmark (HealthcareMagic Dataset) Table I in Section V shows downstream answer quality (BLEU and ROUGE-L) when the retrieval database is sanitized. PA-HDP outperforms baseline methods on GPT-3.5-Turbo and Llama3-8B-Chat. | Method | GPT-3.5 BLEU | GPT-3.5 ROUGE-L | Llama3-8B BLEU | Llama3-8B ROUGE-L | |---|---|---|---|---| | **Origin (No Protection)** | 0.1193 | 0.1078 | 0.0846 | 0.0789 | | **Paraphrase [7]** | 0.1481 | 0.1303 | 0.1050 | 0.0952 | | **ZeroGen [38]** | 0.1199 | 0.1050 | 0.0850 | 0.0769 | | **LPRAG [18]** | 0.3186 | 0.3310 | **0.1634** | 0.1596 | | **SAGE (Stage-2) [7]** | 0.1594 | 0.1288 | 0.1192 | 0.1160 | | **PA-HDP (Ours)** | **0.3275** | **0.3445** | 0.1512 | **0.1726** | *Critical Observation:* While PA-HDP sets new results for GPT-3.5-Turbo and Llama3 ROUGE-L, LPRAG slightly edges out PA-HDP in raw BLEU scores under the Llama3-8B-Chat backbone (0.1634 vs 0.1512). This marginal drop occurs because LPRAG perturbs tokens statically, sometimes preserving simple n-gram matches at the cost of broader semantic consistency (which ROUGE-L captures better). ### 2. Defending Against Targeted Extraction Attacks Table III lists the number of successful exfiltrations out of 250 adversarial prompts engineered to bypass defenses. | Method | Wiki-Llama3 Target Info $\downarrow$ | Wiki-GPT-3.5 Target Info $\downarrow$ | Chat-Llama3 Target Info $\downarrow$ | Chat-GPT-3.5 Target Info $\downarrow$ | |---|---|---|---|---| | **Origin** | 25 | 167 | 7 | 75 | | **Paraphrase** | 9 | 28 | 17 | 42 | | **ZeroGen** | 4 | 5 | 0 | 1 | | **SAGE (Stage-2)** | 0 | 0 | 0 | 0 | | **PA-HDP (Ours)** | **0** | **0** | **0** | **0** | PA-HDP completely zeroed out information leakage (**0 successful extractions** across all settings). Paraphrase baselines left systems vulnerable, leaking 42 targets to an attacker on GPT-3.5. --- ## Limitations & Open Questions While the results are strong, deployment teams should consider these potential points of failure: 1. **The Multi-Query Budget Exhaustion Problem**: RAG systems continuously ingest user queries. As discussed in Section VI, PA-HDP assumes a static pre-allocated privacy budget ($\epsilon$). In a real production deployment, a persistent attacker sending queries can gradually exhaust the DP budget. Current mitigations lack dynamic budget replenishment schemes. 2. **Deterministic NER Dependencies**: The entire "prompt-aware" similarity pipeline relies on the correctness of the Named Entity Recognition (NER) step. If the extraction module fails to identify a novel custom identifier, the risk score drops to zero, and the system serves the raw plaintext to the generator. 3. **High Empirical Sensitivity to Thresholds**: The partition thresholds $\tau_1$ and $\tau_2$ are set heuristically (0.3 and 0.7). Minor changes in semantic drift over diverse corpuses can result in sensitive sentences slipping into the "Low Risk" tier, creating a silent failure point. --- ## What Practitioners Should Do If you are maintaining a private RAG pipeline, take these immediate steps: ### 1. Introduce Dynamic Prompt Auditing Do not let user queries touch your vector store raw. Introduce a lightweight, embedding-based check to identify if the incoming query targets sensitive document attributes. ### 2. Setup Hierarchical Entity Gating Implement regular expression validators and deep-learning NER filters to tag entities with custom weights before formatting the retrieved payload. ``` High-Risk (w=0.95): SSNs, passwords, medical diagnosis, full names Low-Risk (w=0.30): Addresses, dates, company divisions ``` ### 3. Apply Local Exponential Sanitization Instead of feeding raw retrieved chunks to LLM APIs, generate a candidate perturbation pool ($m \ge 50$) of semantically similar sentences with swapped entities. Pass this pool through the Semantic Exponential Mechanism to select a sanitized alternative before presenting the final context to your LLM generator. --- ## The Takeaway Assuming that static databases can be permanently secured against dynamic retrieval queries is a major vulnerability. The threat of data exfiltration in RAG is highly dependent on the user's query. By integrating real-time prompt risk quantification with hierarchical differential privacy, security teams can implement fine-grained sanitization that preserves contextual utility without sacrificing privacy. --- ## Den's Take The most notable aspect of PA-HDP is its departure from "dumb," static differential privacy that treats every RAG query with the same blunt instrument. In practice, static DP destroys semantic utility, making production deployment a non-starter. Reducing targeted PII leakage to exactly 0 successful extractions on both GPT-3.5-Turbo and Llama3-8B-Chat while maintaining a medical dataset BLEU score of 0.3275—more than doubling the 0.1594 BLEU score of generative synthetic alternatives like SAGE Stage-2—is a substantial win for real-world viability. The more pressing concern, from a deployment standpoint, is the robustness of the prompt-aware risk-scoring mechanism itself. If an attacker bypasses the risk scorer using adversarial prompt engineering, the system will misclassify the risk and output raw, unperturbed context. Prior research on adversarial injection has demonstrated that adversarial queries can easily manipulate retriever-generator boundaries. If PA-HDP's dynamic risk calculator can be fooled into thinking a query is benign, the "0 successful extractions" guarantee vanishes. Rigorous red-teaming of the risk scorer itself is warranted before trusting this in high-stakes enterprise RAG pipelines.