Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท Safety, Injection and Guardrails

Privacy and PII Handling in Prompts

Keep personal data out of prompts and logs: detect and redact PII, use reversible placeholders, minimize context, and set retention rules for LLM applications.

Every prompt you send is data you are handing to another system, and every output you log is data you are storing. If prompts contain names, emails, phone numbers, health details or card numbers, then your provider, your logs, your analytics and your test sets all become places where that data can leak. PII (personally identifiable information) is any information that can identify a person, alone or combined.

This lesson gives you practical habits: send less, redact what you cannot avoid sending, restore it afterwards, and keep records for as short a time as you can. It is engineering guidance, not legal advice; privacy law (such as GDPR or HIPAA) and your contracts decide the exact duties, so involve your privacy or legal team for anything regulated.

Data minimization

The best protection is not sending the data. Before building a prompt, ask what the model really needs.

  • A summarizer of support tickets rarely needs the customer's address.
  • A classifier of product reviews does not need reviewer names.
  • A translation of a letter may need the text, but not the sender's account number.

Select fields explicitly when you build the prompt (an allow-list of fields) instead of dumping a whole database row. New columns added later then do not silently start flowing to the model.

Python Allow-list the fields that enter a prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Detecting PII

When free text arrives (a ticket, a chat message), personal data may be anywhere in it. Pattern-based detection finds the structured kinds: emails, phone numbers, card numbers, ids. Names and addresses are harder; production systems use named-entity recognition models or dedicated tools. No detector is perfect, so treat redaction as risk reduction, not a guarantee.

Python Regex PII detector

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what it misses: the name "Ana". Regexes cannot see every kind of PII.

Redaction with reversible placeholders

Replace each sensitive value with a typed placeholder such as [EMAIL_1], keep the mapping in your own memory, send the redacted text to the model, then swap the placeholders back in the answer. The model can still reason about "the email" without ever seeing it. Using numbered placeholders keeps different values distinct.

flowchart LR
    T["Original text"] --> R["Redact: values to placeholders"]
    R --> M["Model sees placeholders only"]
    M --> O["Answer with placeholders"]
    O --> X["Restore from local mapping"]
    X --> F["Final answer"]
    R -.-> K[("Mapping kept locally")]
    K -.-> X

The mapping never leaves your system.

Python Redact, call the model, restore

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The user gets a normal reply with their real data back in it, but the provider never received it.

A caution: a card number should often not be sent to a model or stored at all. For secrets (passwords, API keys, full card numbers) prefer dropping the value entirely instead of redacting and restoring it.

Logs, caches and test sets

Prompts and outputs end up in more places than you think:

  • Application logs and tracing tools. Redact before logging, or log hashes and ids instead of text.
  • Prompt caches and semantic caches. One user's answer must never be served to another user. Include the user or tenant in the cache key whenever the prompt contains private data.
  • Evaluation sets. Examples copied from production into your test set should be anonymized first; engineers who run evals should not need to read real customer text.
  • Few-shot examples. An example with a real person's data is shown to every user of that prompt.
  • Fine-tuning data. Models can memorize and later repeat training text.

Retention and provider settings

Decide how long you keep prompts and outputs, and delete on schedule. When you choose a provider, read its data policy: whether API inputs are used for training, how long they are retained, whether a zero-retention option exists, where data is processed and whether it will sign the agreements you need (such as a data processing agreement). For regulated data, a self-hosted model may be the right call.

Do not put secrets in prompts

Assume anything in a prompt can leak (system prompts are often extractable). Keep API keys, passwords and internal URLs out of them. If the model needs to call a service, let your tool code hold the credentials (see function calling); the model only requests the action.

A short checklist

  1. Send only the fields the task needs.
  2. Detect and redact PII in free text; drop secrets.
  3. Keep the placeholder mapping local; restore after the call.
  4. Scope caches and logs by user, and redact logs.
  5. Anonymize data used for tests and examples.
  6. Set retention limits and read the provider's policy.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the strongest privacy protection for a prompt?

    Data that never leaves your system cannot leak from the provider or from logs.
  2. 2. Why use numbered placeholders like [EMAIL_1] and [EMAIL_2]?

    The mapping from placeholder to value lets you restore the right value in the right place.
  3. 3. Which kind of PII do simple regexes handle worst?

    Names and addresses have no fixed pattern; you need entity-recognition models or other tools.
  4. 4. A cache stores answers to prompts that contain private account data. What must the cache key include?

    Without the user in the key, one user's private answer could be served to another.
  5. 5. Where should an API key that a tool needs live?

    Prompts can leak. Credentials belong in code you control.

Summary

  • Minimize: allow-list the fields that enter prompts.
  • Detect PII (patterns plus entity recognition), redact with local reversible placeholders, drop secrets.
  • Protect logs, caches, test sets, examples and training data.
  • Set retention rules, read provider data policies and keep credentials out of prompts.

Next: Prompt Libraries in Production