Scrub PII Before You Prompt: A Developer's Guide to Sanitizing Data for Cloud AI
The Data You're Sending Without Realizing It
Here's the real cost of AI productivity: developers are routinely pasting customer data into cloud AI tools and calling it debugging.
A support ticket with a user's full name and email. A log file with IP addresses and session tokens. A CSV export that includes dates of birth and phone numbers. A SQL query result that happens to include partial credit card numbers. Not maliciously — just quickly, because the AI assistant is right there and you need an answer in 30 seconds.
Every one of those pastes is a potential GDPR violation, a HIPAA incident, or an NDA breach depending on your jurisdiction and employer. More practically: it's your users' data living in OpenAI's or Anthropic's training pipeline unless you've specifically opted out, verified the opt-out worked, and trust that it'll hold.
The fix isn't to stop using AI. The fix is a pre-flight sanitization habit that takes 30-60 seconds and eliminates the risk. This guide gives you the tools, the checklist, and the tiered decision framework to make it automatic.
What Actually Counts as PII in an AI Context
The GDPR and HIPAA definitions are a starting point, but they're not complete for your purposes. When you're prompting an AI tool, you should treat all of the following as PII or sensitive data:
Obvious PII (most people already know):
- Full names, email addresses, phone numbers
- Physical addresses, postal codes
- Social Security Numbers, passport numbers, driver's license numbers
- Dates of birth when combined with other identifiers
Less obvious but equally risky:
- IP addresses — uniquely identifies users in most jurisdictions under GDPR
- Session tokens, user IDs, internal customer IDs — these don't look like PII but map directly to real people in your database
- Device fingerprints and User-Agent strings tied to logged activity
- Geolocation data (even city-level can be identifying for small populations)
- Usernames and handles — even pseudonymous ones, because they can be cross-referenced
Business-sensitive data that isn't technically PII but still shouldn't leave:
- API keys, access tokens, passwords — even partial or expired ones
- Internal project names and codenames
- Revenue figures, contract values, pricing from internal tools
- Client company names in certain contexts (if under NDA)
- System architecture details, database schemas, infrastructure hostnames
The mental model: if the data has a unique identifier attached — even implicitly — treat it as PII and scrub it before prompting.
Step One: Store the Raw Data Safely Before You Work
Before you start scrubbing, you need somewhere to keep the unsanitized original. This matters more than it sounds: the scrubbing process itself involves handling sensitive data, and you need confidence that the raw file isn't sitting in plaintext on a shared drive, a Slack message, or a Google Doc.
Tresorit is the cleanest solution for this. It's end-to-end encrypted with zero-knowledge architecture, meaning Tresorit servers never see the plaintext of your files. You upload the raw log file, customer export, or whatever it is, and only your device (and whoever you explicitly share with) can decrypt it.
Recommended
Store sensitive files before and after scrubbing. E2E encrypted, GDPR-compliant, zero-knowledge by design.
Tresorit
Affiliate Disclosure: This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. We only recommend products we genuinely believe in. This helps support our work and allows us to continue providing free content.
The workflow: raw data goes into a Tresorit vault first. You scrub locally. The sanitized version goes to wherever you need it. The raw original stays in the vault — available for audit or compliance review, but never exposed to cloud AI or unsecured storage.
For teams, Tresorit's shared workspaces maintain encryption while allowing collaboration. The alternative (a shared Google Drive folder) means Google can read the contents, which is exactly the data exposure you're trying to avoid.
Step Two: The Pre-Sanitization Checklist
Before writing a single line of scrubbing code, run through this checklist mentally or post it somewhere visible:
1. Do I actually need this data in the prompt?
Most of the time: no. If you're debugging a parsing error, you need the structure of the data, not the actual values. Replace real names with USER_1, USER_2. Replace real emails with user@example.com. The AI doesn't care about the actual values — it cares about the shape.
2. Can I use synthetic data instead?
If you're demonstrating a bug pattern or asking for code review, generate fake data that has the same structure as real data. There are good tools for this (faker.js, Python's Faker library, @faker-js/faker) that produce realistic-looking but entirely fictional records.
3. What's the minimum context the AI needs?
Trim aggressively. Don't paste 500 lines of logs when 20 lines demonstrate the error. Each line you don't send is a line that can't leak.
4. What identifiers might be hiding in what looks like structural data?
Database schemas often embed business logic that reveals client relationships. Log file prefixes often include internal hostnames. JSON keys sometimes reference project codenames. Scan structure, not just values.
If you can answer all four confidently, proceed. If any answer is "not sure," treat the data as sensitive and scrub before sending.
Step Three: Regex-Based PII Scrubbing
For most developers, a quick regex pass catches the obvious identifiers. Here's a TypeScript utility you can drop into any project:
```typescript
const PII_PATTERNS: Array<{ name: string; pattern: RegExp; replacement: string }> = [
{
name: "email",
pattern: /[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/g,
replacement: "[EMAIL_REDACTED]",
},
{
name: "phone_us",
pattern: /(\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/g,
replacement: "[PHONE_REDACTED]",
},
{
name: "ipv4",
pattern: /\b(?:\d{1,3}\.){3}\d{1,3}\b/g,
replacement: "[IP_REDACTED]",
},
{
name: "ssn",
pattern: /\b\d{3}[-\s]?\d{2}[-\s]?\d{4}\b/g,
replacement: "[SSN_REDACTED]",
},
{
name: "api_key_generic",
pattern: /\b[A-Za-z0-9_\-]{20,}\b/g,
replacement: "[TOKEN_REDACTED]",
},
{
name: "credit_card",
pattern: /\b(?:\d[ -]?){13,19}\b/g,
replacement: "[CC_REDACTED]",
},
];
export function scrubPII(input: string): string {
return PII_PATTERNS.reduce((text, { pattern, replacement }) => {
return text.replace(pattern, replacement);
}, input);
}
```
A few caveats on the regex approach:
The api_key_generic pattern is intentionally aggressive — it catches alphanumeric strings over 20 characters, which will false-positive on things like base64 data or long hex values. That's acceptable: you want to over-redact, not under-redact.
Phone number patterns vary by country. The example covers US/Canada format. If you're handling international data, add patterns for common formats in your user base.
Regex won't catch everything. A first name embedded in a paragraph of prose won't match a name pattern. A user ID of 12847 looks like any other number. This is why the checklist question "do I actually need this data?" matters so much — structural scrubbing is not a substitute for judgment about whether the data should be sent at all.
Step Four: Automated PII Detection Tools
For higher-volume use cases — analyzing lots of log files, reviewing large datasets, building a pipeline that processes user data before sending to AI — manual regex isn't enough. These tools give you programmatic PII detection:
Microsoft Presidio (open source, self-hostable): The most complete open-source option. Identifies 50+ entity types including names, locations, and domain-specific identifiers. Runs locally. Output is JSON-structured with entity positions and confidence scores, so you can programmatically replace only high-confidence detections.
```bash
pip install presidio-analyzer presidio-anonymizer
```
```python
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()
text = "Customer Jane Smith (jane@example.com, 555-867-5309) reported an issue."
results = analyzer.analyze(text=text, language="en")
anonymized = anonymizer.anonymize(text=text, analyzer_results=results)
print(anonymized.text)
→ "Customer (, ) reported an issue."
```
spaCy with NER (open source): Named Entity Recognition gives you person names, organizations, and locations that regex can't catch. Less purpose-built for PII than Presidio, but more flexible for custom entity types.
AWS Comprehend / Google DLP: Cloud-hosted options with high accuracy for complex detection tasks. Useful when you're building a server-side preprocessing pipeline — but note the irony: you're sending PII to a cloud service to detect PII before sending it to another cloud service. Only appropriate when the detection service itself is under a Data Processing Agreement.
For most developers doing one-off scrubbing before an AI prompt: Presidio locally is the right call. It runs offline, it's accurate, and it produces structured output that's easy to audit.
Step Five: Tier Your Queries
Not every AI interaction needs the same level of caution. A decision framework:
Tier 1 — Safe for any cloud AI, logged-in:
General coding questions, documentation lookups, language syntax, algorithm explanations. No user data, no proprietary information, nothing sensitive. Use whatever tool is fastest.
Tier 2 — Safe for cloud AI after scrubbing:
Log files and stack traces after PII has been removed. Schemas and database structures with table/column names but no real data. Code that processes user data but doesn't contain actual user data. Scrub first with the tools above, then prompt.
Tier 3 — Cloud AI only in anonymous mode after scrubbing:
Competitive intelligence, market research, anything that reveals business strategy. Use Perplexity Pro in an anonymous private session — no account tied to your work identity, queries not linked to your profile. Scrub any business context from the query itself.
Tier 4 — Local LLM only:
Actual customer records. Raw production exports. Full codebase context including business logic. Medical, legal, or financial data of any kind. Internal credentials even if expired. No cloud AI, no exceptions, regardless of scrubbing.
The tier system turns a judgment call into a habit. When in doubt, tier up.
Using Perplexity Safely for Research (Tier 3)
One practical application: competitive and technical research where you're working with public information but don't want to build a query history under your work account.
Perplexity Pro is the tool for this. It's the fastest cited research tool available, with a Deep Research mode that synthesizes 15-30 sources in minutes. For technical due diligence, CVE lookups, or market intelligence — all of which involve only public information — it's significantly more efficient than manual searching.
The setup for sensitive research:
- Create a dedicated browser profile (Chrome or Brave — three-dot menu → Add Profile)
- Don't log into a Perplexity account in this profile
- Use Perplexity anonymously — queries are IP-linked, not account-linked, with shorter retention
- Route through a VPN with a static IP if the query reveals business strategy
This is Tier 3 in practice: public data, anonymized session, no proprietary context in the prompt. The research gets you what you need without building a queryable history under your work email.
Recommended
Cited research from the live web. Deep Research synthesizes 30 sources in minutes. $20/mo after trial.
Perplexity Pro
Affiliate Disclosure: This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. We only recommend products we genuinely believe in. This helps support our work and allows us to continue providing free content.
The Output Side: Encrypted Storage for Sanitized Results
Scrubbing the input is step one. The output — the AI-generated analysis, the insights, the code — also needs a home that doesn't introduce new exposure.
The Proton ecosystem handles this well. Proton Drive gives you end-to-end encrypted cloud storage for AI output documents. Proton Mail handles encrypted delivery if you're sharing results with colleagues or clients. The suite is independently audited, Swiss-headquartered under strong privacy law, and doesn't require trusting a company headquartered in a Five Eyes jurisdiction.
For a complete private AI workflow: raw sensitive data stored in Tresorit → scrubbed locally → Tier 2/3 queries to cloud AI → AI outputs stored in Proton Drive → shared via Proton Mail if needed. Every step in that chain is encrypted by a party that can't read the content.
Recommended
End-to-end encrypted email, cloud storage, and VPN. Swiss privacy law. Free plan available.
Proton Privacy Suite
Affiliate Disclosure: This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. We only recommend products we genuinely believe in. This helps support our work and allows us to continue providing free content.
Building This Into Your Workflow (Not Just a One-Off)
The developers who have privacy incidents aren't usually careless — they're in a hurry. The fix isn't to be more careful in the moment; it's to make the safe path the fast path.
Practical integrations to make this automatic:
Pre-commit hook for API keys: Tools like gitleaks or detect-secrets run before every commit and block secrets from going into version control. Same principle applies to AI prompts — make the check happen before the paste, not after.
Clipboard preprocessor: A small script that runs your regex scrubber on clipboard contents before pasting. Triggers on a hotkey. Takes 200ms. You never paste raw data by accident.
IDE snippet for fake data: Create a snippet library of realistic-but-fake records (names, emails, IPs, UUIDs) that you can insert instead of real data when you need to demonstrate a data structure. Faster than fetching real data, zero risk.
Shared team scrubbing script: If your team is regularly prompting AI with log data or support tickets, a shared CLI tool that everyone uses before copying to AI means the scrubbing step doesn't depend on individual discipline.
The goal is removing the friction from the safe path. Once sanitizing data before prompting is as fast as not sanitizing it, you'll do it automatically.
Get the PII Scrubbing Toolkit
We've packaged the TypeScript scrubber, a Python Presidio setup script, and the tiered decision framework above into a single toolkit. Drop your email below and we'll send it over — including updates when we add new patterns or tools.
Stay Updated
Join our newsletter for the latest updates.
_Last updated: 2026-06-28_