Sub topic 1
Why You Log LLM Requests and What Can Go Wrong
Prerequisites
Assume you can call an LLM API, use basic Python, read application logs, and understand simple regular expressions. This chapter focuses on safe observability, not on building an API client.
Why You Log LLM Requests and What Can Go Wrong
Logging helps you diagnose failures, measure latency, understand token usage, investigate unexpected costs, and correlate an LLM call with the user request that caused it. However, an LLM request is also a data boundary. A prompt may contain private messages, customer records, source code, or credentials. A response may repeat or transform that data.
Logs often have broader access and longer retention than the original application data. An innocent debug statement can therefore create a second copy of sensitive information.
Where Sensitive Data Hides in LLM Traffic
Sensitive data can appear in several places:
- Prompt text, including retrieved documents and conversation history.
- Model output, including copied personal data or generated secrets.
- HTTP headers, URLs, and exception messages.
- Tool calls, function arguments, and tool results.
- Metadata such as user IDs, tenant IDs, and document names.
A redactor reduces exposure, but pattern matching cannot recognize every secret or private value. Avoid sending credentials to the model in the first place, and keep provider API keys in environment variables or a secret manager.
A Logging Policy: Metadata by Default, Content by Exception
Use a default-deny policy for content. Record operational metadata such as status, latency, model name, token counts, and a correlation ID. Log prompt or response content only for a specific debugging case, with authorization, redaction, truncation, access controls, and a short expiration.
| Field | Default policy | Reason |
|---|---|---|
| Model and status | Log | Operational diagnosis |
| Token counts and latency | Log | Cost and performance |
| Prompt and response text | Exclude | High data exposure |
| Correlation ID | Log | Traceability without content |
Structured Logs as the Foundation
Structured logs store one event as fields, commonly in JSON. Consumers can filter status, calculate latency distributions, and join events without parsing human prose. Serialize the record once. This also prevents model output containing newlines from forging additional log entries.
import json
import logging
logger = logging.getLogger("llm")
logger.info(json.dumps({
"event": "llm_call",
"request_id": "req-123",
"model": "example-model",
"status": "ok",
"prompt_tokens": 120,
"completion_tokens": 48,
"latency_ms": 842,
}))Logging Model, Tokens, Latency, and Status
Capture the selected model, input and output token counts, elapsed time, and a small status value such as ok, error, or timeout. Record an exception type rather than its full message unless the message has been reviewed for sensitive content. Add provider request IDs when available, but do not assume they are safe if they contain user-controlled values.
Exercise
Which should be logged by default: the full prompt or its token count and latency?
Show answer
Log the token count and latency. Keep the full prompt out of ordinary logs because it may contain sensitive data.