The safest design is to send less data, not to search for sensitive data after it has left your application. Put a policy-enforcing gateway between the user and the AI API. It should identify the tenant and purpose, classify the input locally, reject secrets and disallowed data, redact or tokenize only what the task can safely tolerate, and send the minimum transformed text through an approved provider configuration.
Regular expressions are useful detectors for some stable formats, but they are not a complete privacy or security control. They miss context, obfuscation, documents, images, unusual formats, and new secret types. They also create false positives. Sensitive information can leak through the model output, prompt injection, application logs, traces, error reports, backups, support access, or a cross-tenant bug. Treat every path as a data boundary.
Fail closed for high-risk categories: block credentials, private keys, access tokens, payment data, and health data unless an approved route, contract, and risk assessment explicitly allow that use. Preserve only pseudonymous tokens when the model needs to track an entity across text, keep the token mapping in a separately protected store, and validate output before returning it. Test the gateway against both missed sensitive data and harmless text that it wrongly blocks, then review policy and provider settings whenever the model, endpoint, retention, or region changes.
Start with a data-flow threat model
Before selecting a detector or an AI provider, draw the complete path for one request. Include browser or client, application server, queue, gateway, AI provider endpoint, tool calls or retrieval systems, output renderer, logs, observability platform, support systems, analytics, backups, and people with access. NIST's guidance on protecting PII emphasizes context-based identification and protection against inappropriate access, use, and disclosure. NIST SP 800-122
The model endpoint is one processor in that path. A carefully redacted prompt can still leak if a debug logger stores the original HTTP body, an error tracker captures local variables, a trace exporter includes request attributes, or an administrator can read the token map. The threat model gives each component an owner and a control.
| Asset or event | Example threat | Required design response |
|---|---|---|
| User-provided text | A user pastes a password, API key, national identifier, patient note, or another person's contact details | Classify before egress, then reject, redact, tokenize, or route locally according to policy. |
| AI API credential | A broad, long-lived key is exposed in code, logs, a client app, or a worker | Keep server-side credentials in a secrets manager, use short-lived or scoped identities where available, rotate, and never return or log them. |
| Tenant data | A token, cache key, retrieval result, or conversation identifier crosses tenant boundaries | Bind every request, token map, cache, and retrieval filter to a tenant and authorization context. Test isolation deliberately. |
| Model context and tools | Untrusted text instructs a model to disclose hidden content or invoke a powerful tool | Treat text as untrusted data, restrict tool permissions, validate structured output, and keep sensitive context out of the prompt. |
| Generated output | The model repeats a token, a retrieved record, a hidden instruction, or sensitive content supplied in context | Inspect output, redact or block as policy requires, and rehydrate only authorized tokens for the original tenant and user. |
| Telemetry and support | Logs, traces, error reports, screenshots, and support tickets retain raw prompts or responses | Disable body capture by default, use safe event fields, restrict access, and apply retention and deletion controls. |
| Long-lived copies | Backups, queues, caches, analytics exports, and provider state retain data after the request | Encrypt, minimize, bound retention, test deletion and restore, and document every copy. |
State the intended purpose precisely. “Summarize support text” does not need the same data as “draft a response to a customer” or “answer a question about a patient record.” Under the GDPR, data minimisation, purpose limitation, storage limitation, integrity, and confidentiality are core processing principles. European Data Protection Board overview The legal duties depend on jurisdiction and role, but purpose-specific minimization is a sound engineering default everywhere.
Classify locally and make an explicit decision
Use a small classification policy that a security, privacy, and product owner can read. The classification should be performed before an external request is created. It may combine deterministic format checks, secret scanners, dictionaries, structured form fields, local named-entity recognition, document or image inspection where relevant, tenant policy, and a human-review path. None is complete alone.
| Category | Examples | Default gateway decision | Why |
|---|---|---|---|
| Public or approved low-risk business text | Published documentation, generic product questions, an anonymized support issue | Send only the required excerpt | Minimize even when data is not sensitive. |
| Personal data | Name, email, phone number, address, account number, precise location, persistent identifier | Redact if identity is irrelevant. Tokenize if the model must distinguish the same person or account within the task. | Pseudonymous placeholders preserve limited context without sending the value. |
| Authentication secrets | Password, private key, session cookie, API key, OAuth token, database connection string | Reject, alert the user or security workflow, and rotate a genuine exposed application secret | A secret is not content to summarize or preserve in a mapping. |
| Financial, government, or highly sensitive identifiers | Payment-card data, tax identifier, passport number, biometric template | Reject or use a separately approved, purpose-limited system | A generic external text API is rarely the appropriate default route. |
| Health data | Diagnosis, treatment, lab result, appointment note, claims data, or a combination that makes a person identifiable | Reject by default. Permit only through an approved health-data route after legal, contractual, privacy, and security review. | A health reference can be sensitive even if a simple pattern detector finds no obvious identifier. |
| Internal confidential information | Source code, incident details, unannounced product plans, customer contracts, credentials in pasted logs | Follow the tenant's data policy. Redact, summarize locally, or use only an approved deployment and agreement. | Confidentiality is not limited to statutory PII. |
Keep classification labels with the request only as long as necessary and make them coarse. An audit event such as tenant=t-17, purpose=summarize, action=tokenized_pii, policy=v4 is usually more useful and safer than storing the original sentence that triggered it. NIST's final Privacy Framework includes Inventory and Mapping, Data Processing Management, and Data Security as privacy-risk management categories. NIST Privacy Framework Version 1.0
Redaction, tokenization, and encryption solve different problems
These controls are complementary. Calling all of them “masking” hides important security decisions.
| Control | What leaves the application | When to use it | Important limitation |
|---|---|---|---|
| Data minimization | Only the needed task excerpt or structured facts | Always, before any other control | It requires product design work, not a library call. |
| Redaction | A non-reversible replacement such as [EMAIL REMOVED] |
The model does not need to know whether two references identify the same person | It can reduce answer quality and does not remove sensitive facts implied by surrounding context. |
| Tokenization | A stable placeholder such as [PERSON_7], with the original stored only in a protected token vault |
The model must preserve reference consistency within a permitted task | The token map remains sensitive data and must never be sent to the model provider. |
| Encryption | Ciphertext for a token map, queue, cache, backup, or data in transit | Stored or transmitted data still needs protection | Encryption does not make data appropriate to disclose to a provider or an unauthorized employee. |
| Pseudonymization | An umbrella term that can include tokenization | Useful when identity is separated from task content | Pseudonymous data can remain personal data under applicable law. |
For tokenization, use randomly generated, non-meaningful tokens. Store the mapping in a separate service or database partition with encryption at rest, per-tenant access checks, a short time-to-live, audit records, and a key-management service. Encrypt the mapping and its backups, but also minimize which service accounts can decrypt it. NIST SP 800-53 includes least-privilege and cryptographic-protection controls, and OWASP recommends centralized storage, provisioning, auditing, and rotation for secrets. NIST SP 800-53 Rev. 5.1 OWASP Secrets Management Cheat Sheet
Do not use a token map as a way to evade a data-processing restriction. If policy says that a category must not be processed by an external AI service, block it or use an approved alternative. Tokenization can reduce disclosure of direct identifiers, but context can still be identifying and the re-identification service is still in scope for privacy and security controls.
A practical Python gateway that fails closed
The gateway should be the only component allowed to call the external AI API. Web handlers, background jobs, and internal tools send raw input to the gateway over an authenticated internal channel. The gateway makes a policy decision before constructing the provider request, and it emits only safe audit metadata.
The following is a design sketch. It intentionally leaves local_classify, vault.tokenize, and provider.call as implementations to be selected and reviewed in your environment. A production classifier should combine more than regex rules, and a production vault should not be an in-memory dictionary.
from dataclasses import dataclass
from enum import Enum
class Action(str, Enum):
SEND = "send"
TOKENIZE = "tokenize"
REDACT = "redact"
BLOCK = "block"
class PolicyBlocked(Exception):
pass
@dataclass(frozen=True)
class RequestContext:
tenant_id: str
user_id: str
purpose: str
approved_route: str
@dataclass(frozen=True)
class Assessment:
categories: frozenset[str]
confidence: float
findings: tuple[str, ...]
def prepare_for_ai(raw_text: str, ctx: RequestContext, policy, vault, audit):
if not raw_text or not ctx.tenant_id or not ctx.purpose:
raise PolicyBlocked("missing required request context")
if not policy.route_is_allowed(ctx.tenant_id, ctx.purpose, ctx.approved_route):
raise PolicyBlocked("unapproved AI route")
assessment = local_classify(raw_text, ctx, policy)
action = policy.decide(ctx, assessment)
# High-risk ambiguity blocks instead of silently passing through.
if assessment.confidence < policy.minimum_confidence_for(action):
action = Action.BLOCK
if action is Action.BLOCK:
audit.safe_event(ctx, "ai_egress_blocked", assessment.categories)
raise PolicyBlocked("input requires a different approved workflow")
if action is Action.TOKENIZE:
safe_text, token_handle = vault.tokenize(
raw_text,
tenant_id=ctx.tenant_id,
ttl_seconds=policy.token_ttl(ctx.purpose),
)
elif action is Action.REDACT:
safe_text, token_handle = redact_locally(raw_text, assessment), None
else:
safe_text, token_handle = minimize_for_purpose(raw_text, ctx.purpose), None
# Use an independent or differently configured detector as a last gate.
residual = local_classify(safe_text, ctx, policy)
if policy.disallowed_after_transform(residual.categories):
audit.safe_event(ctx, "ai_egress_residual_data", residual.categories)
raise PolicyBlocked("transformation did not satisfy the policy")
audit.safe_event(ctx, "ai_egress_allowed", residual.categories)
return safe_text, token_handle
def complete_request(raw_text: str, ctx: RequestContext, policy, vault, provider, audit):
safe_text, token_handle = prepare_for_ai(raw_text, ctx, policy, vault, audit)
response = provider.call(
route=ctx.approved_route,
text=safe_text,
store=False, # Request-level preference, not proof of provider retention behavior.
)
checked = inspect_model_output(response, ctx, policy)
return vault.rehydrate_if_authorized(checked, token_handle, ctx)
The important properties are architectural rather than syntactic. The caller cannot choose an arbitrary model or endpoint. Classification and policy are local. A low-confidence decision does not become a permissive decision. The provider request contains transformed text only. The token handle is opaque to the provider. Audit events contain category and decision metadata rather than prompt text. If the gateway, classifier, vault, policy service, or output inspector is unavailable, the sensitive route should fail closed rather than fall back to direct API access.
Use a service identity dedicated to the gateway. Give it access only to the selected provider project, required token-vault operation, and safe audit sink. Give web clients no provider credential. Isolate provider projects, gateway configuration, KMS keys, token namespaces, caches, queues, and retrieval indexes by tenant where the threat model requires it. Explicitly test that a token handle or cache key from tenant A cannot be read in tenant B's request.
Inspect output and resist prompt injection
Input filtering cannot guarantee safe output. The model may repeat user-provided content, expose a placeholder, summarize retrieved confidential material, or produce content that an application renders or sends to another system unsafely. Treat model output as untrusted input to the next component.
Before returning output, the gateway should:
- Detect raw placeholder tokens and allow rehydration only when the authenticated caller, tenant, purpose, and token lifetime match the original request.
- Run output detection for secrets and policy-restricted data. Block, redact, or route to review when required rather than displaying it automatically.
- Validate a strict structured schema before downstream code uses output to look up data, send a message, update a record, or call a tool.
- Escape or sanitize output for its rendering context, such as HTML, Markdown, SQL parameters, or a command argument. Do not let an LLM response become executable instructions.
- Ensure that output review and redaction itself does not log the original response body to a less protected service.
Prompt injection is relevant even if the initial goal is only summarization. Untrusted user text, retrieved documents, web pages, email bodies, and tool output can attempt to steer the model or trigger unauthorized actions. OWASP identifies prompt injection and sensitive information disclosure among the 2025 risks for LLM applications. OWASP LLM Top 10 No prompt wording or detector is foolproof. Limit the blast radius by keeping secrets and unnecessary private context out of the prompt, using tool allowlists and least privilege, separating instructions from data, and requiring deterministic authorization before any action. OWASP prompt-injection guidance
If a model can retrieve data, call plugins, browse, or send messages, treat those capabilities as separate data processors and authorization decisions. A non-sensitive model prompt does not make a later tool request safe. The application, not the model, must enforce tenant filters, record-level permissions, quotas, approval requirements, and egress rules.
Protect logs, traces, backups, and people
Many data leaks happen outside the primary database. Configure application and HTTP libraries not to log request or response bodies by default. Strip sensitive headers before error reporting. Use an allowlist of trace attributes, not a denylist. Do not attach prompt text, raw tool output, tokens, authorization headers, or file contents to telemetry. OWASP specifically warns that logs can contain personal and sensitive information and lists access tokens, passwords, connection strings, keys, and sensitive PII among data that normally should not be recorded directly. OWASP Logging Cheat Sheet
Create separate retention rules for the raw request, transformed prompt, token mapping, provider response, telemetry, cache, queue, and backup. There should be a documented reason, owner, encryption key, access group, deletion method, and maximum retention for each. Test backup restores and deletion workflows. OWASP's secrets guidance recommends encrypted backups, reduced access rights, and tested restore procedures. OWASP Secrets Management Cheat Sheet
Human access needs the same care as API access. Use role-based, tenant-scoped access, just-in-time approval where feasible, audited break-glass procedures, and redacted support views. Administrators who operate the infrastructure do not automatically need the ability to read raw prompts or token maps. Give incident responders a safe evidence path that reveals only what the investigation genuinely needs.
Evaluate the provider, contract, retention, and region
Before enabling an AI route, make a per-route record of the provider, endpoint, model, project or account, data types permitted, storage behavior, retention period, human-review conditions, region of storage and processing, subprocessor implications, security commitments, and incident contact. Have privacy, legal, procurement, and security teams review the relevant data-processing terms and any sector-specific agreement. This is necessary for regulated health data, but it is also good practice for confidential business and personal data.
Do not infer a provider's data handling from marketing language or a model name. Check current endpoint documentation because retention and state can differ by feature. For example, OpenAI's current API data-controls documentation distinguishes abuse-monitoring logs from application state, says some controls require approval, and lists endpoint-specific retention and regional-processing limitations. OpenAI API data controls That is an example of why a gateway must pin approved endpoints and configuration rather than assume one organization-wide setting applies to every API feature.
For data subject to the HIPAA Security Rule, determine whether the organization is a covered entity or business associate, whether the planned use is permitted, and whether the provider arrangement and safeguards meet the applicable requirements. HHS says the Rule requires appropriate administrative, physical, and technical safeguards for electronic protected health information. HHS Security Rule A health-data route should not be enabled merely because the text was de-identified by a basic regex. Obtain qualified legal and compliance advice for your jurisdiction and use case.
Test false positives and false negatives before trusting the gateway
Build a controlled test corpus with synthetic data and carefully authorized examples. Never copy production sensitive content into an unprotected test suite. Version the corpus and policy, then test the gateway at every change to a detector, model, provider endpoint, prompt template, telemetry SDK, or token-vault implementation.
| Test set | Examples | Pass condition |
|---|---|---|
| Secrets | Synthetic API keys, passwords, private-key markers, bearer tokens, credentials pasted in code or logs | All are blocked before provider egress. The test verifies safe audit metadata and no raw-value logging. |
| PII | Synthetic names, emails, phones, addresses, multiple people in one message, alternate punctuation, and multilingual examples | Policy-required values are redacted or tokenized, token map is tenant-bound, and the provider receives no original value. |
| Health | Synthetic symptom, diagnosis, medication, and appointment text with and without direct identifiers | The generic route blocks it. An approved specialized route is tested against its separate authorization and contract controls. |
| Benign look-alikes | Documentation identifiers, fake placeholders, product codes, non-secret hashes, and ordinary numbers | Unnecessary blocks remain within the review budget and have a reason the policy owner can improve. |
| Context and files | Sensitive values split across sentences, tables, PDF text, image OCR, attachments, retrieval passages, and tool output | The intake route classifies every accepted modality or rejects unsupported modalities. |
| Output and injection | Placeholder echoes, attempts to expose data, unsafe tool instructions, and HTML or Markdown edge cases | Output is blocked or made inert, and no action occurs without deterministic authorization. |
| Operational paths | Exceptions, retries, timeouts, queue messages, cache hits, trace exports, backups, and support workflows | No raw sensitive body or credential appears in a lower-protection system. |
Measure detection recall by category, false-block rate on benign traffic, residual-sensitive-data rate after transformation, output-leak rate, cross-tenant authorization failures, and latency. Do not publish a single “sanitization accuracy” number. The acceptable miss rate for a pasted production credential is different from the acceptable false-positive rate for a low-risk email address in a user-visible draft. For high-risk categories, resolve uncertainty by blocking or human review.
Three concrete decisions
Secret in a support request. A user pastes a cloud access key while asking for help diagnosing a deployment. The gateway blocks the external-AI request, records only category=credential and the request correlation ID, tells the user not to share the credential, and routes the incident to the team's secret-rotation process. It does not replace the key with a token and continue summarizing. A secret may be active, and retaining a reversible map adds risk without helping the task.
Email and phone number in a complaint. A user asks for a concise summary of a complaint containing their email address and phone number. The task needs continuity but not the values. The gateway replaces them with [EMAIL_1] and [PHONE_1], sends the transformed text to the approved route, checks the response, and rehydrates only those exact placeholders for the same authenticated user if the chosen product behavior requires it. A support agent viewing the audit trail sees the decision and the request ID, not the original contact data.
Health-related free text. A user asks an app to summarize a note that includes a diagnosis, medication, and appointment date. The generic public-AI route blocks the request. If the organization has a legally reviewed health-data workflow with the necessary agreements, region, retention, authorization, and security safeguards, the gateway routes only the minimum permitted content to that separate configuration. If it does not, the application offers a local summary feature, manual review, or a clear refusal. The safe decision is based on the data category and approved route, not on whether a detector happened to find a phone number.
Implementation path and common mistakes
Start small but make bypasses impossible.
- Inventory every route that can send text, files, images, embeddings, tool output, or logs to an AI provider. Disable direct calls from application code.
- Define a compact data-classification policy and default action for each purpose and tenant. Begin by blocking secrets and high-risk data on general routes.
- Deploy the gateway in monitor mode only with synthetic or explicitly approved test traffic, then compare its safe audit decisions to human review.
- Enable enforcement for high-risk categories first. Add redaction and tokenization only where the product requires them and the vault is ready.
- Configure approved provider projects, credentials, endpoints, retention controls, regions, and egress firewall rules. Store those settings as reviewed configuration.
- Add output checks, telemetry allowlists, encrypted bounded-retention storage, and a documented deletion and incident process.
- Red-team the complete flow, including injection, output rendering, tenant isolation, queues, traces, backups, and support access. Review metrics and policy exceptions continuously.
| Mistake | Why it fails | Better approach |
|---|---|---|
| Relying on one email or phone regex | It misses context and other formats, and it may incorrectly alter harmless text. | Combine policy, multiple local detectors, structured data handling, residual checks, and review. |
| Sending first and redacting later | The provider and network path already received the original data. | Make local classification and transformation a required pre-egress gate. |
| Tokenizing secrets | The vault becomes another place that preserves a live credential. | Block, notify, and rotate the secret if it belongs to the organization. |
| Logging full prompts for debugging | A low-protection telemetry system becomes a sensitive-data store. | Log safe event metadata and grant controlled, audited access to protected evidence only. |
Trusting store=false as a complete retention strategy |
Provider retention and application state vary by endpoint, product, contract, and account configuration. | Verify current provider documentation and controls for the exact route, then minimize data regardless. |
| Relying on prompt instructions to stop disclosure | The model can be manipulated and cannot enforce application authorization. | Enforce data access, tool permissions, and output schema outside the model. |
| Using one global token map or cache | An identifier collision or authorization bug can cross tenant boundaries. | Bind storage, encryption keys, namespaces, and authorization to tenant and purpose. |
Limits and boundaries
No detector, classifier, LLM guardrail, tokenization scheme, or provider setting can guarantee that arbitrary free text is free of sensitive information. Some sensitive facts are implicit, and image, audio, attachment, and retrieval pipelines introduce separate detection problems. The right response to uncertainty is a restricted route, human review, a local model, or refusal, not a silent permissive fallback.
This is security and privacy engineering guidance, not legal advice. Data-protection obligations depend on the organization, contracts, data subjects, jurisdictions, and sector. Health, financial, employment, children’s, government, and cross-border data can have additional requirements. Involve qualified legal, privacy, security, and procurement owners before processing those categories through an external AI service.
Evidence
Sources used for this answer.
Question signals show what people need. Primary documentation supports the answer. Both remain visible.
- 01How can I prevent sensitive data leakage when sending user input to an AI API in Python?Stack Overflow · question signal · checked 1 Sept 2026
- 02NIST SP 800-122csrc.nist.gov · primary evidence · checked 1 Sept 2026
- 03European Data Protection Board overviewedpb.europa.eu · primary evidence · checked 1 Sept 2026
- 04NIST Privacy Framework Version 1.0csrc.nist.gov · primary evidence · checked 1 Sept 2026
- 05NIST SP 800-53 Rev. 5.1csrc.nist.gov · primary evidence · checked 1 Sept 2026
- 06OWASP Secrets Management Cheat Sheetcheatsheetseries.owasp.org · primary evidence · checked 1 Sept 2026
- 07OWASP LLM Top 10genai.owasp.org · primary evidence · checked 1 Sept 2026
- 08OWASP prompt-injection guidancegenai.owasp.org · primary evidence · checked 1 Sept 2026
- 09OWASP Logging Cheat Sheetcheatsheetseries.owasp.org · primary evidence · checked 1 Sept 2026
- 10OpenAI API data controlsplatform.openai.com · primary evidence · checked 1 Sept 2026
- 11HHS Security Rulehhs.gov · primary evidence · checked 1 Sept 2026