Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.2 — Logging Prompts and Responses Safely
LLM Engineering · Example 1 of 2

A log line, unsafe and redacted

The first line logs the prompt as it arrived. The second redacts it and adds the metadata. In Java the course's Json helper writes the compact JSON (real projects use Jackson).

Prompts and replies are the most useful thing to log and the most dangerous: they contain names, emails, secrets and private documents. Safe logging uses structured lines, a redactor before the write, sampling, and a retention limit.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write mask_email(text) that replaces each email with its first letter, *** and the domain, so ana@example.com becomes a***@example.com. Use re.sub with a function.

02

Write is_expired(age_days, kept_days) and purge(logs, kept_days) where logs is a list of (request_id, age_days); purge returns the ids of logs to delete (older than kept_days).

Explain it without notes

01

Why must redaction happen before the log line is written, not when someone reads the log?

02

Why do you always keep failed calls but only sample healthy ones?

03

What are the limits of pattern-based redaction, and what do you do about them?

A log line, unsafe and redacted
Sign in to run this example in your browser.

Expected output

{"ts":1000,"level":"info","event":"llm_call","request_id":"r1","prompt":"Hi, I am ana@example.com (call 9876543210). My key is sk-test12345678. Summarise my invoice."}
{"ts":1000,"level":"info","event":"llm_call","request_id":"r1","model":"small","tokens_in":38,"tokens_out":61,"latency_ms":640,"status":"ok","prompt":"Hi, I am [EMAIL] (call [PHONE]). My key is [KEY]. Summarise my invoice."}