OpenSourceAIHub

How to Prevent PII Leaks in ChatGPT API Calls

Share
April 11, 2026·8 min read·Security

Every time your application calls the ChatGPT API, the entire contents of the prompt travel over the wire to OpenAI's servers. For most use cases that's fine — but the moment your prompts contain real user data, you have a problem worth solving.

Consider a customer-support chatbot that receives a message like “My SSN is 123-45-6789, can you check the status of my claim?” Or an HR assistant processing “Draft an offer letter for Jane Doe at $185,000/year — her address is 742 Elm Street, Chicago.” Or a developer tool that accepts a code snippet still containing a hardcoded AWS secret key.

None of these scenarios require malicious intent. They happen naturally, at scale, every day — and they send personally identifiable information (PII), protected health information (PHI), and developer secrets straight to a third-party AI provider.

The good news: this is a solvable problem. This guide covers the three main approaches to preventing PII leaks in ChatGPT API calls — from manual review all the way to automated gateway-level redaction — and shows you how to implement the most effective one in under five minutes.

What PII Actually Flows Through ChatGPT API Calls?

The ChatGPT API receives whatever your application puts in the messages array. If your system prompt includes user context, if your RAG pipeline retrieves real documents, or if end users type freely into a chat interface — PII will end up in your prompts. Here are real-world examples (anonymized):

Example — Customer support prompt containing PII
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful support agent for Acme Insurance."
    },
    {
      "role": "user",
      "content": "Hi, my name is Maria Johnson. My policy number
       is AC-992481. My SSN is 421-83-7192 and my email is
       maria.johnson@email.com. I need to update my address
       to 1847 Oak Avenue, Austin, TX 78701. Also, please
       charge the new premium to my card 4532-8721-0039-4481."
    }
  ]
}

That single prompt contains seven distinct PII entity types: a person name, an SSN, an email address, a street address, a city/state, a ZIP code, and a credit card number. Multiply this by thousands of API calls per day and the exposure is significant.

Modern PII detection needs to cover far more than names and emails. Here are the 28+ entity types that can appear in ChatGPT API prompts:

  • Government IDs: Social Security numbers, passport numbers, driver license numbers, national insurance numbers (UK NINO), ITINs
  • Financial: Credit card numbers (with Luhn validation), IBAN codes, bank account numbers, crypto wallet addresses
  • Contact info: Email addresses, phone numbers, street addresses, ZIP codes
  • Personal: Full names, dates of birth, nationalities, religious/political affiliations (NRP)
  • Developer secrets: API keys (OpenAI sk-*, GitHub ghp_*), AWS access keys (AKIA*), AWS secret keys, private keys, webhook URLs
  • Medical: Medical license numbers, NHS numbers, patient IDs, diagnostic codes
  • Network: IP addresses, MAC addresses, URLs containing tokens or session IDs

The 3 Approaches to Preventing PII Leaks

There are three main strategies for keeping PII out of your ChatGPT API calls. Each has distinct trade-offs in coverage, scalability, and implementation effort.

Approach 1: Manual Review

The simplest approach: have humans review prompts before they're sent to the API. This works for small-scale, low-volume use cases — perhaps a team of five using an internal tool. But it doesn't scale. At 1,000+ API calls per day, manual review becomes a bottleneck. And humans miss things — especially structured data like API keys and IBAN codes that don't look like “personal data” at a glance.

Approach 2: Regex & Pattern Matching

Write regular expressions to detect structured PII: credit card numbers, SSNs, email addresses, phone numbers, API keys. This approach is fast (sub-millisecond per pattern) and catches data with predictable formats. The problem is coverage — regex cannot detect unstructured PII like person names (“Maria Johnson”), street addresses (“1847 Oak Avenue”), or medical terms. You end up with a false sense of security: the scan “passes” but sensitive data still flows through.

Approach 3: AI-Powered Gateway-Level DLP

The most comprehensive approach: route all API traffic through an AI Firewall that combines pattern matching, context heuristics, and a purpose-built NLP engine to detect both structured and unstructured PII. The gateway sits between your application and the LLM provider, scanning every prompt in real time. Detected entities are either redacted (replaced with placeholders) or the request is blocked entirely — before anything reaches OpenAI.

Approach Comparison

 
Manual Review
Regex / Pattern
Gateway DLP
Structured PII
Partial
Yes
Yes
Unstructured PII
Partial
No
Yes
Scales to 10K+ req/day
No
Yes
Yes
Added latency
Minutes–hours
<5ms
<50ms
Scans images (OCR)
No
No
Yes
Entity types detected
Varies
8–12
28+
Code change required
Process change
Custom middleware
2 lines

How Gateway-Level PII Redaction Works

The architecture is straightforward: your application sends requests to the AI Firewall instead of directly to OpenAI. The firewall scans every prompt through a multi-layer detection pipeline, applies your redaction policy, and forwards the cleaned request to the LLM provider. The provider never sees the raw PII.

Request Flow

Your App

   POST /v1/chat/completions (standard OpenAI format)

AI Firewall receives request

   Layer 1: Pattern matching + checksum validation — <5ms

   Layer 2: Context heuristic analysis — <10ms

   Layer 3: Purpose-built NLP entity recognition — <20ms

   Layer 4: Proprietary OCR pipeline (if images) — <100ms

   Apply policy: REDACT entities or BLOCK request — <1ms

AI Firewall OpenAI / Anthropic / Groq / any provider

Provider AI Firewall Your App

The entire scan happens synchronously in the request path — no async queues, no after-the-fact auditing. Every prompt is scanned before it leaves your infrastructure. Here's what a prompt looks like before and after the firewall:

Before — raw prompt sent to OpenAI

Unprotected request
{
  "messages": [{
    "role": "user",
    "content": "Draft a response to Maria Johnson (SSN: 421-83-7192,
     email: maria.johnson@email.com). She lives at 1847 Oak Avenue,
     Austin, TX 78701. Her card on file is 4532-8721-0039-4481."
  }]
}

After — what OpenAI actually receives

Protected request (after AI Firewall scan)
{
  "messages": [{
    "role": "user",
    "content": "Draft a response to [PERSON_REDACTED] (SSN: [SSN_REDACTED],
     email: [EMAIL_REDACTED]). She lives at [ADDRESS_REDACTED].
     Her card on file is [CREDIT_CARD_REDACTED]."
  }]
}

// Response headers include:
// x-dlp-latency: 32
// x-entities-detected: PERSON,US_SSN,EMAIL_ADDRESS,STREET_ADDRESS,CREDIT_CARD

The best part: the only change in your application code is swapping the base_url. Everything else — your models, your prompts, your response parsing — stays exactly the same.

The 2-line code change (Python)
from openai import OpenAI

# Before: direct to OpenAI
# client = OpenAI(api_key="sk-...")

# After: route through AI Firewall
client = OpenAI(
    base_url="https://api.opensourceaihub.ai/v1",
    api_key="your-hub-api-key"
)

# Everything else stays exactly the same
response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": prompt}]
)

Implementation: Auto-Redact PII in Under 5 Minutes

OpenSourceAIHub implements the gateway DLP architecture described above as a managed service. Here's how to set it up — from zero to protected in four steps.

1

Sign up and get your Hub API key

Create a free account at opensourceaihub.ai. Every account starts with 1M free credits — no credit card required. Your Hub API key is generated automatically when you create your first project.

2

Configure your DLP policy (optional)

By default, every project starts with maximum protection — all 28+ entity types are set to REDACT. You can customize this per project: set high-severity entities (credit cards, SSNs, API keys) to BLOCK, lower-severity ones (emails, phone numbers) to REDACT, and anything non-sensitive to ALLOW.

3

Change your base URL (2 lines of code)

Point your existing OpenAI SDK at the Hub endpoint. Here are examples for Python and Node.js:

Python — OpenAI SDK
from openai import OpenAI

client = OpenAI(
    base_url="https://api.opensourceaihub.ai/v1",
    api_key="your-hub-api-key"   # from Step 1
)

response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": user_message}
    ]
)
print(response.choices[0].message.content)
Node.js — OpenAI SDK
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.opensourceaihub.ai/v1",
  apiKey: "your-hub-api-key",   // from Step 1
});

const response = await client.chat.completions.create({
  model: "gpt-4.1",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: userMessage },
  ],
});
console.log(response.choices[0].message.content);
4

Verify it's working

Send a test prompt containing dummy PII. Check the response headers for x-dlp-latency (scan time in ms) and x-entities-detected (list of entity types found). You can also use the free AI Leak Checker to test any text for PII before writing a single line of code.

What Gets Detected: 28+ Entity Types by Category

The AI Firewall's detection engine covers six categories of sensitive data. Each entity type uses the most appropriate detection method — pattern matching with checksum validation for structured data, and the purpose-built NLP engine for unstructured entities.

Developer Secrets

API_KEYAWS_ACCESS_KEYAWS_SECRET_KEYPRIVATE_KEYGITHUB_TOKENSLACK_WEBHOOK

Financial & Crypto

CREDIT_CARDIBAN_CODEUS_BANK_NUMBERCRYPTO_ADDRESSUS_ITIN

Personal Identifiers

EMAIL_ADDRESSPHONE_NUMBERUS_SSNUS_PASSPORTPERSONSTREET_ADDRESSDATE_TIMENRP

UK / EU Identifiers

UK_NINOUK_NHS_NUMBER

Network & Location

IP_ADDRESSMAC_ADDRESSLOCATIONURL

Medical & Licenses

MEDICAL_LICENSEUS_DRIVER_LICENSE

Credit card validation goes beyond pattern matching. Numbers matching the credit card format are verified with the Luhn algorithm to eliminate false positives. A random 16-digit number won't trigger a detection — only valid card numbers do.

Frequently Asked Questions

How do I prevent PII from being sent to the ChatGPT API?

The most effective method is to route your API calls through an AI Firewall gateway that automatically scans and redacts PII before the request reaches OpenAI. You change two lines of code (the base_url and api_key in the OpenAI SDK), and the gateway handles detection of 28+ entity types — SSNs, credit cards, emails, API keys, medical records, and more — with under 50ms of added latency.

Does PII redaction affect the quality of ChatGPT responses?

Minimally. Redacted entities are replaced with descriptive placeholders like [EMAIL_REDACTED] or [SSN_REDACTED]. The LLM understands that sensitive data was removed and responds accordingly. For most use cases — summarization, classification, drafting replies — the response quality is effectively identical because the LLM didn't need the raw PII to complete the task.

Is ChatGPT API usage GDPR-compliant by default?

Not automatically. When you send personal data to the ChatGPT API, OpenAI processes it under their data processing terms. For GDPR compliance, you need to either obtain explicit consent for this processing or ensure personal data is removed before it reaches the provider. Gateway-level PII redaction implements the data minimization principle required by GDPR Article 5(1)(c) — the provider only receives the minimum data necessary, with all personal identifiers removed.

How much latency does PII redaction add to API calls?

A well-implemented gateway adds approximately 30ms median and 50ms at P95 for text-only requests. When images are present (e.g., GPT-4 Vision), OCR scanning adds approximately 100ms. Compared to typical LLM response times (200ms–1,000ms+), this represents a 3–15% overhead. Every response includes timing headers so you can verify the exact scan duration for each request.

Stop PII Leaks in Your ChatGPT API Calls

1M free credits. No credit card. 28+ entity types auto-redacted with under 50ms latency. Works with OpenAI, Anthropic, Groq, Gemini, Mistral, and 300+ models.

Related Articles