Sub topic 11
Keep secrets and private data out of prompts
This chapter assumes you have used a chat or editor-integrated AI assistant and can read short Python and shell snippets. You should know what an environment variable is and how to run tests.
Why this matters
Everything you put in a prompt leaves your machine. It goes to a service that may log it, retain it, or use it under terms you did not read closely. The assistant also sees only what you paste, so the habit of "include everything so it has context" works against you here.
Earlier chapters told you to add context such as error messages, existing code, and configuration. This chapter sets the limit on that advice. You want enough context to get a good answer and no more data than the task needs.
What counts as sensitive
- Credentials: API keys, passwords, access tokens, private keys, connection strings with embedded passwords.
- Personal data: names, emails, phone numbers, addresses, and ID numbers of real people.
- Business data: customer records, internal hostnames, proprietary algorithms, unreleased product details.
- Contractual material: code or data your employer or a client has barred you from sharing.
Secrets hide in places you may not check. A pasted stack trace can include a URL with a token in the query string. A config file can contain a database password. A log excerpt can contain customer emails.
Redact before you paste
Replace each sensitive value with a placeholder that keeps the shape of the original. The assistant usually needs the structure, not the real value.
Unsafe prompt:
Why does this fail?
DATABASE_URL=postgresql://admin:[email protected]:5432/orders
Error: password authentication failed for user "admin"Safer prompt:
Why does this fail?
DATABASE_URL=postgresql://<USER>:<PASSWORD>@<HOST>:5432/orders
Error: password authentication failed for user "<USER>"
Context: the password is read from an environment variable and may contain special characters such as @ and /.The safer version still lets the assistant suggest the likely cause, which is that special characters in a password must be URL-encoded in a connection string. It never sees the real password.
For personal data, use fake values that look real. Replace [email protected] with [email protected], and replace real customer rows with invented ones that have the same columns and formats.
Keep secrets out of code you will paste
The best protection is code that never contains secrets, so there is nothing to leak when you paste it. Read credentials from the environment.
import os
def get_database_url() -> str:
url = os.environ.get("DATABASE_URL")
if not url:
raise RuntimeError("DATABASE_URL is not set")
return urlWhen you paste this function, the prompt holds a variable name and no value. Ask the assistant for code in this style too. Add a constraint such as "Read all credentials from environment variables; never hard-code them." Assistants sometimes produce example code with placeholder keys inline, and people then replace the placeholder with a real key and commit it.
Scan before you send
Manual review misses things. A quick scan of text you are about to paste catches common patterns. This script flags a few well-known shapes. It is a safety net, not a guarantee.
import re
import sys
PATTERNS = {
"AWS access key id": r"AKIA[0-9A-Z]{16}",
"private key block": r"-----BEGIN [A-Z ]*PRIVATE KEY-----",
"password in URL": r"://[^/\s:@]+:[^/\s@]+@",
"email address": r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}",
"bearer token": r"(?i)bearer\s+[A-Za-z0-9._~+/-]{20,}",
}
def scan(text: str) -> list[str]:
return [name for name, pattern in PATTERNS.items() if re.search(pattern, text)]
if __name__ == "__main__":
findings = scan(sys.stdin.read())
if findings:
print("Do not paste yet. Found: " + ", ".join(findings))
sys.exit(1)
print("No known patterns found.")Run it as cat snippet.txt | python scan_prompt.py. A clean result does not prove the text is safe. It misses custom token formats, names, and business secrets.
Check how your tool handles data
Different assistants handle data differently. Before using one with work material, find answers to these questions in its documentation or your organization's policy:
- Are prompts stored, and for how long?
- Are prompts used to train models?
- Does the tool read files in your project automatically, such as
.env? - Is there an approved tool or enterprise plan for company code?
Editor-integrated assistants are a particular risk because they may attach open files as context without you choosing each one. Exclude secret files through the tool's ignore settings if it offers them, and keep .env files out of the workspace you share.
Common mistakes
| Mistake | Safer habit |
|---|---|
| Pasting a whole config file to ask about one setting | Paste only the relevant lines, redacted |
| Sharing a production log with real user data | Use a synthetic sample with the same format |
| Trusting the assistant to "ignore" a secret you included | Remove it before sending; the model cannot unsee it |
| Committing generated code that has a placeholder key replaced by a real one | Load keys from the environment and scan commits |
Exercise
A teammate wants to ask an assistant why a request fails and pastes curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.abc.def" https://api.internal.example.com/users/4821. Name two things to change before sending, and one thing to do if it was already sent.
Show answer
Replace the token with a placeholder such as <TOKEN>, and replace the real user ID and internal hostname with generic values such as <USER_ID> and api.example.com if they are not needed to explain the failure. If it was already sent, treat the token as compromised and revoke or rotate it at the issuer.
Exercise
Why does reading a password from os.environ make prompting safer, even though the assistant still sees your code?
Show answer
The pasted code contains only the variable name, not the secret value. There is nothing sensitive in the text you share, so the code can be pasted and committed without leaking the credential.