Sub topic 4
Keep Secrets and Private Data Out of Prompts
This chapter assumes you can run Python from a terminal, read a traceback, and use git. It covers what to remove from code, logs, and errors before you paste them into an AI assistant, and how to do it without losing the information the assistant needs.
Why this step comes before the prompt
Anything you paste into a prompt leaves your machine. It may be logged, stored, or reviewed under the provider's data policy. Even if you trust the provider, a pasted secret can end up in shared chat histories, screenshots, or team transcripts. A leaked key must be rotated, and that costs far more time than redacting the text first.
You gather context in the previous step. This chapter is the filter between that context and the prompt.
What to look for
Check every piece of text you plan to paste: code, tracebacks, logs, config files, and shell output.
| Category | Examples |
|---|---|
| Credentials | API keys, passwords, tokens, private keys, session cookies |
| Connection strings | Database URLs with a user and password, internal hostnames |
| Personal data | Names, emails, phone numbers, addresses, customer IDs |
| Business data | Proprietary algorithms, unreleased product details, real records from a production database |
Secrets hide in places people forget:
- Environment dumps such as
envorprintenvoutput. - Request logs containing
Authorizationheaders. - Stack traces that include a connection string in an exception message.
- Files like
.env,config.yaml, andsettings.pywith defaults filled in.
Replace values, keep the shape
The assistant rarely needs the real value. It needs to know what kind of value it is and how it is used. Replace the value with a labeled placeholder and keep the structure.
Before:
sqlalchemy.exc.OperationalError: could not connect to
postgresql://admin:[email protected]:5432/ordersAfter:
sqlalchemy.exc.OperationalError: could not connect to
postgresql://<DB_USER>:<DB_PASSWORD>@<DB_HOST>:5432/ordersThe error type, the driver, and the port are still visible, so the diagnosis is still possible. Use the same placeholder for the same value each time. If two different users appear, call them <USER_A> and <USER_B> so the assistant can still see that they differ.
Do the same for data. Use fake records that trigger the same bug, such as [email protected] in place of a real customer address. This follows from the earlier chapter on minimal examples: a small made-up input that reproduces the failure is both safer and easier to reason about.
Load secrets from the environment
Code that reads secrets from the environment is safe to paste, because the secret itself is not in the file.
import os
api_key = os.environ["PAYMENTS_API_KEY"]If a key is hardcoded today, move it to an environment variable or a secrets manager before you share the file.
Scan before you send
A quick search catches obvious mistakes. This script flags lines that look like secrets in a file you plan to paste.
import re
import sys
PATTERNS = [
re.compile(r"(?i)(api[_-]?key|secret|token|password)\s*[=:]\s*\S+"),
re.compile(r"[A-Za-z]+://[^/\s:]+:[^@\s]+@"),
re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"),
]
with open(sys.argv[1], encoding="utf-8") as f:
for number, line in enumerate(f, start=1):
if any(p.search(line) for p in PATTERNS):
print(f"line {number}: possible secret")Run it as python scan.py trace.txt. It misses things, so read the text yourself too. Dedicated scanners such as gitleaks or truffleHog are more thorough.
Check your tool's settings
Editor assistants and agents can read files you never pasted. Check which files and folders the tool can see, and exclude .env files and key directories using whatever ignore mechanism your tool offers. Confirm what your provider's data policy says about retention and training, especially for work code.
Exercise
A traceback contains the line redis://default:[email protected]:6379/0. Rewrite it so the assistant can still diagnose a connection problem, and say why you keep the port.
Show answer
redis://<REDIS_USER>:<REDIS_PASSWORD>@<REDIS_HOST>:6379/0. The port and scheme stay because a wrong port or protocol is a common cause of connection errors, so the assistant needs them. The user, password, and host are replaced because they are credentials or internal details that the diagnosis does not depend on.