Sub topic 13
Opt-in, time-limited content logging for debugging
Sometimes metadata is not enough. A model returns a wrong answer, and you need to see the prompt that produced it. This chapter shows how to turn on content logging for a narrow scope and for a short time, with a record of who turned it on.
This chapter assumes you have read the earlier chapters on the logging policy and redaction. It also assumes you know basic Python and have the call_logged wrapper and redact function from those chapters.
Why content logging needs a gate
Your policy logs metadata by default and content by exception. An exception that nobody controls becomes the default. A developer sets log_content=True during an incident and never reverts it. Months later the logs hold thousands of redacted-but-imperfect prompts.
A gate makes the exception safe. It has four properties:
- Opt-in: content logging is off until someone enables it on purpose.
- Scoped: it applies to one tenant, user, feature, or request ID, not the whole service.
- Time-limited: it expires on its own, with a hard maximum.
- Audited: each enablement records who, why, and for how long.
The lifecycle of a gate
The important transition is the expiry. Nobody has to remember to turn the gate off.
A small gate in Python
This version keeps state in memory, so it suits a single process. A multi-instance service needs a shared store such as a database row or a Redis key with a TTL (time to live). The logic stays the same.
import logging
import threading
import time
audit = logging.getLogger("llm.audit")
class DebugGate:
def __init__(self, max_seconds: int = 3600):
self._until: dict[str, float] = {}
self._max = max_seconds
self._lock = threading.Lock()
def enable(self, scope: str, seconds: int, reason: str, who: str) -> None:
seconds = min(seconds, self._max)
with self._lock:
self._until[scope] = time.time() + seconds
audit.info("content_logging_enabled scope=%s seconds=%d who=%s reason=%r",
scope, seconds, who, reason)
def disable(self, scope: str) -> None:
with self._lock:
self._until.pop(scope, None)
audit.info("content_logging_disabled scope=%s", scope)
def is_enabled(self, scope: str) -> bool:
with self._lock:
expires = self._until.get(scope)
if expires is None:
return False
if time.time() >= expires:
del self._until[scope]
return False
return True
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
gate = DebugGate(max_seconds=3600)
gate.enable("tenant:acme", seconds=1, reason="ticket 4821", who="ana")
print(gate.is_enabled("tenant:acme")) # True
time.sleep(1.1)
print(gate.is_enabled("tenant:acme")) # FalseThe min(seconds, self._max) line enforces the hard cap, even if someone asks for a week. The expiry check runs on every read, so a stale entry can never keep logging on.
Connecting the gate to the wrapper
The gate only decides. The wrapper still redacts and truncates, as before:
response = call_logged(
client_call,
model="my-model",
prompt=prompt,
log_content=gate.is_enabled(f"tenant:{tenant_id}"),
)Gated content is still passed through redact and truncated to 2,000 characters. The gate adds a control. It does not replace the earlier ones.
Practical rules
- Choose the narrowest scope that reproduces the bug. A single request ID beats a tenant, and a tenant beats the whole service.
- Keep the time limit short, minutes rather than days.
- Write gated records to a separate log destination with stricter access and shorter retention. The next chapter covers this.
- Disable the gate when you have the data you need.
Common errors
- Storing the expiry in a global flag with no timestamp, so it never turns off.
- Letting the caller choose any duration without a maximum.
- Skipping the audit record, which leaves you unable to explain later why content was captured.
- Enabling the gate in a config file that gets deployed and forgotten.
Exercise
A teammate proposes an environment variable, LOGLLMCONTENT=true, to enable content logging during an incident. Name two ways it fails the gate requirements from this chapter, and say what you would change.
Show answer
It is not scoped, because it applies to all traffic in every process that reads it. It is also not time-limited, because it stays on until someone removes it, and it records no audit of who enabled it or why. Replace it with a gate that takes a scope, a TTL capped at a maximum, a reason, and the name of the person enabling it, and that logs each enablement.