
Privacy and Compression: Secret Savings
Learn how to redact PII and sensitive data while reducing token counts. Master the 'Encryption-Lite' and 'Token Hashing' patterns.
Privacy and Compression: Secret Savings
In highly regulated industries (Finance, Health, Government), you cannot send raw user data to a cloud LLM. You must redact or mask Personally Identifiable Information (PII).
Most developers treat PII Redaction as a "Security burden." But if done correctly, it is a Token Efficiency Opportunity.
By replacing long, sensitive strings (e.g., "The patient Johnathan Montgomery Smith born on Jan 14, 1978...") with short placeholders (e.g., [P_01]), you achieve two goals:
- Security: No sensitive data leaves your infrastructure.
- Efficiency: You have compressed 15 tokens into 1.
In this final lesson of Module 18, we learn the Place-Holder Mapping pattern and the Minified Redaction strategy.
1. The PII Mapping Pattern
- Step 1 (Local): Scan text for PII (Names, Emails, SSNs) using a library like Presidio or Regex.
- Step 2 (Local): Store the mapping in a local memory-map.
Johnathan Smith->[P1]jsmith@gmail.com->[E1]
- Step 3 (Remote): Send the Minified Block to the LLM.
- Step 4 (Local): When the LLM responds, "De-minify" by subbing the names back in.
2. Token Diversity ROI
PII is often repetitive. A name might appear 20 times in a document.
- Verbatim: 3 tokens/name × 20 = 60 tokens.
- Mapped: 1 token/placeholder × 20 = 20 tokens.
Savings: 66%. In a 50-page legal document with hundreds of references to "Party A" and "Party B," this mapping can save thousands of tokens while ensuring 100% compliance.
3. Implementation: The Privacy Compressor (Python)
Python Code: Mapping and Re-Injective
import re
def compress_privacy(text):
# Mapping table
mapping = {}
# 1. Identity Email
emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)
for i, email in enumerate(emails):
placeholder = f"[E{i}]"
mapping[placeholder] = email
text = text.replace(email, placeholder)
return text, mapping
def decompress_privacy(llm_output, mapping):
for placeholder, original in mapping.items():
llm_output = llm_output.replace(placeholder, original)
return llm_output
# Cycle:
# "Contact jdoe@me.com" -> "Contact [E0]" -> "Reply to [E0]" -> "Reply to jdoe@me.com"
4. The 'Token Hashing' Pattern (Advanced)
For extremely sensitive data that needs to be "Consistent" across requests but never revealed (e.g., a Database ID), use a Short Hash.
db_id_9876543210->h_123
This keeps the model's focus on the Logic of the ID without move the actual ID through the cloud context.
5. Summary and Key Takeaways
- Redaction is Compression: Short placeholders use fewer tokens than long names.
- Deterministic Mapping: Keep the "Secret Sauce" on your server; send only the "Recipe" (Placeholders) to the AI.
- Consistency: Use the same placeholder for the same entity throughout the conversation to maintain the model's reasoning.
- Compliance ROI: You save money on tokens while also satisfying your legal department.
Exercise: The Privacy Switch
- Take a business email with multiple project names, employee names, and dates.
- Manually replace every Name with
[N]and every Project with[P]. - Count the tokens of the new text.
- Ask a model: "Based on this text, what is the status of project [P1]?"
- Verify: Does the model still understand the relationships?
- (Result: Yes, LLMs are excellent at reasoning about variables/placeholders).