Sub topic 1
Why you log LLM requests and what can go wrong
Prerequisites
This chapter assumes you have the following:
- Basic Python, including functions and the standard
loggingmodule. - Experience calling an LLM API and reading its response.
- Familiarity with log levels such as INFO and ERROR.
- Basic regular expressions, which later chapters use for redaction.
Why you log LLM calls
An LLM call is slow, costs money, and can return different output for the same input. Logs are often the only record of what happened. Teams log LLM traffic for four main reasons:
- Debugging. You need to know which model was called, whether it failed, and how long it took.
- Cost tracking. Providers bill by tokens, so you need usage per request, per feature, or per user.
- Quality review. When an answer is wrong, you want to see what the model was asked.
- Auditing. Some organizations must show what an automated system did and when.
Only some of these need the text itself. Debugging and cost tracking work with metadata: model name, sizes, status, and latency. This difference drives most of the tutorial.
What can go wrong
Ordinary request logging often writes the full prompt and full response to a log file. That is where the problems begin.
- Personal data. Users paste names, emails, addresses, and medical or financial details into prompts. Your log now holds personal data you may not be allowed to keep.
- Credentials. API keys can appear in request headers, in error messages, or in text a user pastes into a prompt.
- Proprietary text. Prompts often include internal documents, source code, or customer records pulled in for context.
- Wider exposure. Logs are usually readable by more people and tools than the database holding the original data. They are also copied to dashboards, backups, and third-party services.
- Long life. Logs are rarely cleaned up. Data that would have been deleted elsewhere stays in them.
- Untrusted text. Model output is text you do not control. If it contains newlines, it can forge extra log lines.
A naive example
This wrapper looks reasonable and does what many first versions do.
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("llm")
def call_naive(client_call, model: str, prompt: str) -> str:
logger.info("prompt=%s", prompt)
response = client_call(model=model, prompt=prompt)
logger.info("response=%s", response)
return response
def fake_client(model: str, prompt: str) -> str:
return "Sure, I will email [email protected]."
call_naive(fake_client, "demo-model", "Write to [email protected], key sk-abcdefghijklmnop1234")Running it prints both the email address and the key to the log. Anyone who can read the log can read them. The line also has no model name, latency, or status, so it is poor for debugging as well.
The approach in this tutorial
The rest of the tutorial replaces the naive wrapper with a safer one. The plan is:
- Log metadata by default: sizes, a prompt hash, status, and latency.
- Log content only by exception, and only after redaction and truncation.
- Handle streamed responses without logging partial text.
- Control who can read the logs and how long they are kept.
- Test that redaction works and keeps working.
The goal is logs that still answer "what happened and what did it cost?" without storing more sensitive text than you need.
Exercise
A teammate says, "We should log every full prompt and response so we can debug anything." Name two risks of doing this, and name one debugging need that does not require the text.
Show answer
Risks include storing personal data and credentials that users paste into prompts, and exposing proprietary text to everyone who can read the logs (logs are also copied to dashboards and backups and are rarely deleted). Model output could also forge log lines. Debugging needs that work without text include finding which model was called, whether the call failed, how long it took, and how many tokens it used.