Zero-Leakage AI: Enforcing PII Protection with Claude Code and Client-Side Masking
How to prevent personal data leaks to third-party LLMs like Groq or Anthropic. Includes an architectural flow diagram, an enforcement proxy script, and step-by-step instructions for integrating with Claude Code.
As enterprise engineering organizations rapidly adopt autonomous CLI tools like Claude Code, OpenAI, and high-speed inference engines like Groq, data privacy has emerged as the most critical hurdle to enterprise adoption. Regulated companies cannot risk sending Personally Identifiable Information (PII)—such as customer emails, telephone numbers, credit cards, or internal API secrets—over the wire into third-party LLM cloud contexts.
Rather than blocking AI usage or waiting for costly private on-prem models, teams can enforce Zero-Leakage Privacy by Design: a deterministic client-side privacy proxy that automatically detects, masks, vaults, and unmasks PII before external LLM calls are dispatched.
The Zero-Leakage Architecture
Architecture Sequence Flow
sequenceDiagram
autonumber
actor User as User / Developer
participant Proxy as Local Masking Proxy
participant Vault as Ephemeral In-Memory Vault
participant LLM as External LLM (Claude Code / Groq)
User->>Proxy: Prompt containing PII ("john@gmail.com", "555-123-4567")
Proxy->>Proxy: Regex Scan & Extract Sensitive Primitives
Proxy->>Vault: Store Encrypted Token Map (, )
Proxy->>LLM: Send Masked Prompt ("", "")
Note over LLM: LLM reasons on synthetic tokens without seeing real data
LLM-->>Proxy: Response with Masked Tokens
Proxy->>Vault: Retrieve Original Values
Proxy->>Proxy: Replace Tokens with Real Values
Proxy-->>User: Seamless Unmasked Final Response
1. The 6-Stage Masking Pipeline
- PII Detection: High-speed regex scanners inspect prompts before transmission, matching emails, phone numbers, SSNs, credit cards, and credential patterns.
- Token Masking: Sensitive tokens are replaced with synthetic placeholders (e.g.
john@gmail.com$\rightarrow$<EMAIL_1>,555-123-4567$\rightarrow$<PHONE_1>). - Encrypted In-Memory Vaulting: The token dictionary is stored in an ephemeral, local memory map on your workstation or proxy. It is never transmitted to the cloud LLM.
- Cloud LLM Inference: The LLM processes the syntactic structure with zero exposure to user identity.
- LLM Response: The model produces code, instructions, or summaries referencing the synthetic tokens.
- Local Unmasking: The local proxy swaps the tokens back to real values before outputting results to the developer.
2. Enforcing This with a Python Privacy Proxy Script
Here is an automated Python script (pii_guard.py) that acts as a local CLI wrapper or HTTP proxy to intercept prompts, apply deterministic masking, query the LLM, and unmask responses:
#!/usr/bin/env python3
"""
pii_guard.py • Zero-Leakage PII Protection Proxy
Author: @samuelj90
"""
import os
import re
import sys
import json
import urllib.request
# Regex primitives for PII detection
PATTERNS = {
"EMAIL": re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'),
"PHONE": re.compile(r'\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b'),
"KEY": re.compile(r'\b(?:sk-[a-zA-Z0-9]{32,}|ghp_[a-zA-Z0-9]{36})\b')
}
class PIIGuard:
def __init__(self):
self.vault = {}
self.counters = {"EMAIL": 0, "PHONE": 0, "KEY": 0}
def mask(self, text: str) -> str:
masked = text
for pii_type, pattern in PATTERNS.items():
def replacer(match):
val = match.group(0)
# Avoid re-tokenizing identical instances
for token, original in self.vault.items():
if original == val:
return token
self.counters[pii_type] += 1
token = f"<{pii_type}_{self.counters[pii_type]}>"
self.vault[token] = val
return token
masked = pattern.sub(replacer, masked)
return masked
def unmask(self, text: str) -> str:
unmasked = text
for token, original in self.vault.items():
unmasked = unmasked.replace(token, original)
return unmasked
def call_llm(sanitized_prompt: str) -> str:
"""Dispatches sanitized prompt to LLM endpoint (Groq / Anthropic / OpenAI)"""
api_key = os.getenv("GROQ_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
if not api_key:
# Fallback simulation if no key is set
return f"LLM Acknowledged: processed task for prompt: {sanitized_prompt}"
# Example: Groq high-speed completion
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = json.dumps({
"model": "llama-3.3-70b-versatile",
"messages": [{"role": "user", "content": sanitized_prompt}]
}).encode("utf-8")
req = urllib.request.Request(url, data=payload, headers=headers)
with urllib.request.urlopen(req) as resp:
data = json.loads(resp.read().decode("utf-8"))
return data["choices"][0]["message"]["content"]
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 pii_guard.py 'Your prompt with PII'")
sys.exit(1)
raw_input = " ".join(sys.argv[1:])
guard = PIIGuard()
# 1. Mask
sanitized = guard.mask(raw_input)
print(f"🔒 [Masked Sent to LLM]: {sanitized}\n")
# 2. Transmit
response = call_llm(sanitized)
# 3. Unmask
final_output = guard.unmask(response)
print(f"✨ [Final Output]:\n{final_output}")
3. How to Use It in Claude Code
When working with autonomous agent systems like Claude Code, you can enforce this guardrail at multiple integration points:
Method A: Claude Code Pre-Tool Shell Hook
Claude Code allows running shell commands and custom tools. Wrap your external calls through pii_guard.py:
# Instead of passing raw customer records to an API query:
python3 pii_guard.py "Troubleshoot transaction failure for user john@gmail.com with account phone 555-123-4567"
Method B: Claude Code MCP (Model Context Protocol) Server
You can expose the PII Guard as a local Model Context Protocol tool. When Claude Code analyzes codebase files or database dumps:
- Claude Code invokes
sanitize_promptvia MCP. - The MCP tool performs regex substitution and keeps the lookup map in memory.
- Claude Code evaluates logic on synthetic tokens (
<EMAIL_1>). - Claude Code delivers the result through
restore_tokens, restoring customer data locally on the user’s terminal.
Method C: Local OpenAI/Anthropic Compatible Proxy Base URL
You can run pii_guard.py as a lightweight HTTP proxy on http://localhost:8080/v1 and configure your CLI environment:
export OPENAI_BASE_URL="http://localhost:8080/v1"
export ANTHROPIC_BASE_URL="http://localhost:8080/v1"
All outbound requests from CLI agents and development workflows automatically have sensitive attributes stripped before leaving your machine.
Conclusion
Privacy by Design does not require forfeiting the reasoning power of modern cloud LLMs. By combining deterministic regex tokenization with ephemeral local memory storage, enterprise developers can safely use Claude Code, Groq, and OpenAI with mathematically verifiable zero-leakage security.
