Answers you can trust, from Codeables

Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.

Explore Codeables
Verified Source
Data Security Platforms

We’re building RAG/agents—how do we prevent PII/PHI from being sent to an LLM in prompts, tool outputs, or agent memory?

Skyflow10 min read

Most enterprises hit the same wall when they move from LLM prototypes to production RAG systems and agents: how do you get value from large language models without ever leaking PII/PHI/PCI into prompts, tool outputs, or long-lived agent memory?

This isn’t just a “best practice” issue. It’s a hard requirement for HIPAA, PCI-DSS, GDPR, SOC 2, and internal data governance. And it’s not solved simply by “using a private LLM.” You need an architecture that systematically prevents sensitive data from entering the model workflow in the first place.

Below is a practical, implementation-focused guide to doing exactly that.


Why RAG and Agents Leak PII/PHI by Default

RAG (retrieval-augmented generation) and multi-agent systems increase privacy risk because they:

  • Ingest rich enterprise data for retrieval (customer records, tickets, EMR notes, CRM, logs).
  • Accept untrusted user prompts that may contain PII/PHI.
  • Use tools and connectors (databases, search, APIs, CRMs) that return raw sensitive data.
  • Maintain agent memory (short- and long-term) where sensitive data can persist.
  • Log everything for debugging and analytics unless explicitly controlled.

Sensitive data can enter and be exposed through:

  1. Training data

    • If you fine-tune or train on internal data without proper redaction, PII/PHI becomes part of the model’s internal weights and may later resurface.
  2. Prompt data

    • Users paste emails, medical notes, or IDs directly into the prompt.
    • Agent or system prompts incorporate retrieved documents containing PII/PHI.
    • Tool outputs are injected straight into the prompt without sanitization.
  3. Agent memory and logs

    • Conversation history, retrieval traces, tool results, and error logs can all store raw PII/PHI for long durations.

A privacy-safe RAG/agent architecture must break this pattern.


Core Principle: Redact Before the Model Sees Anything

The foundational pattern is:

Sensitive data never reaches the LLM, any agent, or any tool in raw form.

Instead, you:

  • Detect and classify PII/PHI/PCI as early as possible.
  • Tokenize, de-identify, or anonymize those fields.
  • Maintain mappings in a secure vault, outside the LLM and outside agent memory.
  • Operate on tokens and masked text throughout prompts, tool outputs, and memories.
  • Re-identify only when absolutely necessary, and only for authorized users or downstream systems.

Think of it as a “PII firewall” in front of the LLM.


Step 1: Build a PII-Aware Data Pipeline for RAG

Before you ever index or retrieve anything, enforce a privacy-aware ingestion pipeline.

1. Define what counts as sensitive

Create a data classification policy that covers:

  • Direct identifiers: names, SSNs, MRNs, email addresses, phone numbers, credit card numbers, account IDs.
  • Quasi-identifiers: dates of birth, ZIP codes, IP addresses, device IDs, unique usernames.
  • Domain-specific fields: internal project names, clinician IDs, claim numbers, member IDs, or other proprietary identifiers.
  • Clinical or financial attributes: diagnoses, medications, lab results, bank account numbers.

This classification should be consistent across all systems feeding your RAG corpus.

2. Transform data at ingestion

As you index documents or records for retrieval:

  1. Run detection on every record/document

    • Use PII/PHI detection capable of:
      • Pattern-based detection (regex for SSNs, card numbers, etc.).
      • ML-based entity detection (names, locations, organizations).
      • Domain-specific lexicons and rules.
  2. Replace sensitive fields with tokens

    • Example:
      • Jane DoePATIENT_93827
      • SSN: 123-45-6789SSN_TOKEN_4a7c
      • Project DragonflyPROJECT_TOKEN_7f3b
    • Keep referential integrity:
      • The same person or value always maps to the same token within defined scopes (e.g., per tenant, per app, per dataset).
  3. Store original values in a secure vault

    • A data privacy vault holds:
      • Token → original value mappings.
      • Metadata (type, tenant, policy tags).
    • The LLM index only contains the tokenized / de-identified text, never the raw PII/PHI.
  4. Enforce data minimization

    • Don’t index fields you don’t need for your use case.
    • If only aggregate or statistical info is required, use irreversible anonymization rather than reversible tokenization.

Result: Your vector store / RAG corpus is privacy-filtered by design.


Step 2: Guardrail the Prompt Path (User Inputs)

Even if your corpus is clean, user prompts can still introduce sensitive data.

1. Intercept raw user input

All user requests should go through a pre-processing layer before touching any agent or model. In this layer:

  • Run PII/PHI detection on the input.
  • Replace detected entities with tokens, as you did for ingested data.
  • Log only the transformed prompts, not the originals.

Example:

User input:

“What does this lab result mean for John Doe, DOB 01/01/1975, MRN 84729?”

Model-facing prompt:

“What does this lab result mean for PATIENT_TOKEN_84729, DOB_TOKEN_01_01_1975?”

The agent and LLM never see raw names, MRNs, or dates of birth.

2. Implement prompt-time policy checks

Before the prompt goes to the LLM:

  • Validate that no raw PII/PHI remains.
  • If sensitive data is detected that cannot be tokenized safely (e.g., free-text clinical notes not configured for your policies), either:
    • Block the request.
    • Or route it to a specialized, more restrictive pipeline.

Step 3: Sanitize Tool Outputs and Connectors

Tools are one of the biggest leaks in agent-based systems. Databases, search, CRMs, EHRs, or custom APIs often return raw sensitive data.

1. Treat every tool as untrusted from a privacy standpoint

For each tool:

  • Wrap the tool with a security shim that:
    • Executes the original call.
    • Runs detection on the output.
    • Replaces sensitive fields with tokens or masked versions.
    • Applies data minimization (include only necessary fields).

2. Prevent raw tool outputs from entering prompts

LLM/agent frameworks often do this by default:

Tool response:
"Customer Jane Doe (SSN 123-45-6789) has account balance $4,200."

Agent prompt to LLM:
"Tool X returned: 'Customer Jane Doe (SSN 123-45-6789)...'"

You must interpose a transformation step:

Raw tool output → redaction → LLM-facing tool output

Example:

Raw tool output:
"Customer Jane Doe (SSN 123-45-6789) has account balance $4,200."

Sanitized output:
"Customer CUSTOMER_TOKEN_28d3 (SSN_TOKEN_12ab) has account balance $4,200."

Only the sanitized version should be added to the agent’s scratchpad or passed back into the LLM.

3. Enforce “minimum necessary” at tool level

Align tools with access policies:

  • Return only the fields required for the task.
  • For example, a summarization agent might only need:
    • Problem description and resolution notes.
    • Not full identity or payment details.

Configure tools to:

  • Drop or token-encode identity fields.
  • Return aggregates instead of per-individual data when possible.

Step 4: Protect Agent Memory and Long-Term State

Agent memory is where sensitive data can quietly accumulate and persist.

1. Never store raw sensitive data in memory

Apply the same data firewall to any memory:

  • Conversation history buffers.
  • Long-term memory stores (vector DBs, key-value stores).
  • Agent planning traces or workflow logs.

Before anything is added to memory:

  • Run detection and tokenization.
  • Confirm no raw PII/PHI remains.

2. Externalize identity and context to a vault

Instead of putting PII/PHI in memory:

  • Use tokens for identities and entities:
    • PATIENT_TOKEN_84729 instead of “Jane Doe”.
    • ACCOUNT_TOKEN_3498 instead of “Acct #987654321”.
  • Store all identity-resolving information in the vault, never in LLM-accessible memory.

This way:

  • The LLM can recognize that it’s working with the same patient or account over multiple turns.
  • But it never sees actual names, IDs, or clinical details that can directly identify someone.

3. Configure retention and deletion policies

For any memory storage:

  • Auto-expire entries after a short time window if not required.
  • Respect data subject requests (e.g., right to be forgotten) via the vault, which manages tokens and their mappings centrally.

Step 5: Privacy-Aware Logging, Monitoring, and Debugging

Many systems are careful in prompts but leak everything in logs.

1. Log only transformed data

  • All logs should capture only the tokenized/anonymized prompts, tool outputs, and responses.
  • Raw PII/PHI must never be written to:
    • Application logs.
    • Vector stores used for analytics.
    • Monitoring dashboards.

2. Use secure, controlled re-identification

For debugging or customer support:

  • Allow controlled “re-identification” of specific tokens only for authorized staff, via the vault.
  • This avoids dumping sensitive data into shared logs, screenshots, or collaboration tools.

Step 6: Why “Private LLMs” Aren’t Enough on Their Own

Many organizations assume moving from public APIs to private or self-hosted LLMs solves the problem. It improves control, but it doesn’t remove the core risks:

  • A private LLM can still:
    • Ingest prompts containing PII/PHI.
    • Generate responses that echo or infer sensitive data.
    • Log data internally.
  • If you fine-tune it with internal corpora, those weights can still memorize and leak sensitive phrases.

You still need:

  • Pre-LLM redaction and tokenization.
  • Strict policies around training and fine-tuning.
  • Vault-based control over any data that could identify individuals.

Private LLMs are part of the solution, not the whole solution.


Step 7: Architectural Blueprint for Privacy-Safe RAG/Agents

Here’s a high-level reference architecture you can adapt:

  1. Data ingestion pipeline

    • Source systems → PII/PHI detection → tokenization/anonymization → store in vault → write sanitized content into vector store / search index.
  2. User request path

    • Raw user prompt → detection + tokenization → policy check → LLM/agent.
  3. Tool/connector path

    • Agent invokes tool → tool executes → detection/tokenization on tool output → sanitized tool result → LLM/agent.
  4. Agent memory

    • Before storing any memory snippet → detection + tokenization → store only sanitized text and tokens.
    • Vault holds identity mappings and sensitive fields.
  5. Response path

    • LLM generates response with tokens → optional re-identification (if user is authorized and policy allows) → final response to user.
    • Watch out for accidental PII hallucinations; apply post-response scanning if needed.
  6. Logging and analytics

    • Store only sanitized prompts, tool outputs, and responses.
    • Use tokens for analytics and metrics.

Step 8: Data Minimization as a Default Policy

Throughout your RAG/agent stack, aim for:

  • Minimum necessary exposure:
    • Only share what the LLM absolutely needs to complete the task.
  • Layered enforcement:
    • Ingestion: strip or tokenize sensitive data.
    • Tools: return minimal fields.
    • Prompts: inject only relevant snippets.
    • Memory: retain only what’s necessary, and only in sanitized form.

This aligns directly with regulatory principles (HIPAA’s “minimum necessary,” GDPR’s data minimization) and dramatically reduces blast radius.


Step 9: Turning This into a Repeatable Governance Pattern

To keep PII/PHI out of LLM prompts, tool outputs, and agent memory at scale:

  • Standardize patterns and libraries
    • Provide internal SDKs or middleware so teams don’t reinvent redaction logic.
  • Automate policy enforcement
    • Centralize data classification and access policies and enforce them via code, not process alone.
  • Continuously test for leaks
    • Run synthetic tests where known PII/PHI is injected and verify it never reaches logs, prompts, or model outputs.

Summary: Practical Checklist

When you’re building RAG/agents and want to prevent PII/PHI from ever reaching the LLM:

  • Ingest via a privacy pipeline that detects and tokenizes PII/PHI before indexing.
  • Intercept and sanitize user prompts before they reach any agent or model.
  • Wrap tools and connectors with a redaction layer that scrubs their outputs.
  • Store only tokenized data in agent memory and vector stores.
  • Use a secure data privacy vault to hold original sensitive values and token mappings.
  • Apply data minimization everywhere: prompts, tools, memory, logs.
  • Avoid raw PII/PHI in logs, and support controlled re-identification only when necessary.
  • Do not rely solely on private LLMs—combine them with systematic redaction, tokenization, and policy enforcement.

Implementing this pattern lets you ship powerful RAG systems and autonomous agents while keeping PII/PHI out of the model’s reach—and out of your compliance risk register.

We’re building RAG/agents—how do we prevent PII/PHI from being sent to an LLM in prompts, tool outputs, or agent memory? | Data Security Platforms | Codeables | Codeables