Sub topic 2
Where Sensitive Data Hides in LLM Traffic
This chapter assumes you can call an LLM API from Python, know application logging and log levels, and can read basic regular expressions. It covers where sensitive data appears in LLM traffic, so you know what a log line can leak before you decide what to record.
Why LLM traffic is different
A typical web request has a few known fields. An LLM request carries free text that users, documents, and tools write. You cannot list the sensitive fields ahead of time, because the sensitive part can be anywhere in the text.
Logging the full request and response is the easy default. It also puts everything the user typed, and everything the model produced, into a system that more people can read and that lasts longer.
The main places data hides
The table lists the common locations and what each one tends to contain.
| Location | What it often contains |
|---|---|
| User message | Names, emails, phone numbers, health or financial details, pasted documents |
| System prompt | Proprietary instructions, internal URLs, business rules |
| Retrieved context | Chunks of internal documents, customer records, search results |
| Tool calls and results | Database rows, file contents, API responses |
| Model response | Echoed user data, summaries of private documents, invented but realistic personal details |
| Request headers | `Authorization` bearer tokens, API keys, session cookies |
| Error messages | Echoed request bodies, key fragments, file paths |
User input and pasted content
Users paste whatever is convenient: a support email thread, a contract, a stack trace with a connection string. You did not design for that content, but your log will store it.
Context you add yourself
In retrieval-augmented systems, your code adds documents to the prompt. The user may type one harmless sentence while the prompt grows to thousands of tokens of internal data. Logging the final prompt logs all of it.
The model's response
Responses repeat and summarize what is in the prompt, so they carry the same sensitive data. A model can also produce text that looks like personal data. Treat the response as sensitive without checking each time.
Headers and credentials
Many HTTP client libraries can log full requests in debug mode. That output includes the Authorization header, which holds your API key. A key in a log file is a credential in every place that log is copied.
import logging
# Risky: debug logging of HTTP libraries can include request headers.
logging.basicConfig(level=logging.DEBUG)Set noisy client loggers to WARNING in production, and never enable wire-level debugging against real traffic without a plan for cleanup.
Secrets inside text
Keys also appear inside content. A user may paste a config file, or a tool result may include an environment dump. Pattern matching can catch common shapes:
import re
EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
API_KEY = re.compile(r"\b(?:sk|key|token)[-_][A-Za-z0-9_-]{16,}\b")
def redact(text: str) -> str:
text = EMAIL.sub("[EMAIL]", text)
text = API_KEY.sub("[SECRET]", text)
return text
print(redact("Mail [email protected], key sk-abcdefghijklmnop1234"))
# Mail [EMAIL], key [SECRET]How data flows into logs
Sensitive data reaches logs along several paths, not only your own logging statements.
Exception traces deserve attention. Some libraries include the request body in an exception message, and logger.exception writes that message to the log.
A quick audit
To find your own exposure, work through these steps:
- List every place a prompt is built, and note which parts come from users, documents, or tools.
- Search your code for log statements that receive a prompt, a response, or a request object.
- Check the log level of your HTTP and SDK loggers.
- Trigger a failing call on purpose and read the resulting log entry.
Exercise
A retrieval-based assistant logs only the user's question, which is one short sentence. Name two other places in the same request where sensitive data could still be logged.
Show answer
Examples: the retrieved context added to the prompt (internal document chunks), the system prompt, the model response, tool results, the Authorization header if client debug logging is on, and exception messages that echo the request body. Any two of these are correct.