Skip to content
AgenticAIWiki
  1. Learn
  2. Part 13
  3. 67. Observability and tracing
  4. 67.3

Lesson 67.3 · 3 of 9 · 4 min read · Reviewed 24 Nov 2026

Logging prompts safely: redaction and retention

By the end, you can

  • Log prompts and outputs without leaking personal data.
  • Set retention and access rules for AI logs.

Full prompt and response text makes debugging easy. It also turns your logging system into a copy of your users’ private data.

A safe default

  1. RedactRedaction: Removing or masking sensitive data, such as PII or secrets, before it is stored, logged or sent. at the source. Before a span is exported, replace card numbers, government ids, emails and similar with placeholders like [CARD]. Do it in code with patterns and, where needed, a small classifier.
  2. Split the data. Metadata (ids, timings, tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id., status) goes to the main tracingTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. tool. Full text, if kept at all, goes to a separate store with tighter access.
  3. Limit access. Only people debugging production can read full text, and reads are themselves logged.
  4. Set retention. For example, full text for 14 days, metadata for 13 months. Delete on schedule, not when someone remembers.
  5. Respect user choices. If a user deletes their data or opts out, their prompts must go too.

Sampling

You rarely need the full text of every request. Keep full text for errors, flagged runs and a small random sample, and metadata for everything.

Never log secrets

API keys, tokens and passwords must never reach a prompt or a log. If one does, treat it as leaked: rotate it. This site blanks anything that looks like a real key in the Playground for the same reason.

Key points

  • Prompt and output text is the most useful debugging data and often the most sensitive.
  • Redact or tokenise personal data before it is stored, limit who can read raw logs, and delete them on a schedule.
  • Keep metadata (tokens, timings, ids) longer than full text.
Hands-on labRuns in the Playground

Redact prompts before they reach a trace

Strip emails, card numbers and phone numbers from prompt text before it is stored.

You will have: A redact() function and span records that split metadata from redacted text.

Steps

  1. Run the starter: it reports leaks because nothing is redacted.
  2. Add card and phone patterns to PATTERNS.
  3. Implement redact() with subn so it counts replacements.
  4. Run again and read the metadata and text for each prompt.
  5. Add one more fake prompt of your own and check it is cleaned.

Done when

  • leaks after redaction prints 0.
  • The first prompt shows both [EMAIL] and [CARD].
  • The prompt with no personal data has redactions: 0.

Open in the Playground No API key needed.

Starter code (Python)
# Redact personal data before a prompt is stored on a span.
# All values below are fake test data.
import re

PATTERNS = [
    ("[EMAIL]", re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")),
    # TODO: add a "[CARD]" pattern for 16 digits with optional spaces or dashes
    # TODO: add a "[PHONE]" pattern for numbers like 07700 900123
]

PROMPTS = [
    "Hi, I am jo.walker@example.com and test card 4000 1234 5678 9010 was charged twice.",
    "Please call me on 07700 900123 about order 1120.",
    "What time does the shop open on Sunday?",
]


def redact(text):
    """Replace each match with its label. Return the clean text and how many were replaced."""
    found = 0
    # TODO: loop over PATTERNS and use pattern.subn(label, text) to replace and count
    return text, found


def to_span(prompt, run_id):
    """Metadata is kept long; redacted text goes to a short-lived store."""
    clean, n = redact(prompt)
    return {"run_id": run_id, "chars": len(prompt), "redactions": n}, clean


leaks = 0
for i, p in enumerate(PROMPTS, 1):
    meta, clean = to_span(p, f"run-{i}")
    print(meta)
    print("   text:", clean)
    if "@" in clean or re.search(r"\d{4} \d{4}", clean) or "07700" in clean:
        leaks += 1
print("leaks after redaction:", leaks)
Show a solution
# Redact personal data before a prompt is stored on a span.
# All values below are fake test data.
import re

PATTERNS = [
    ("[EMAIL]", re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")),
    ("[CARD]", re.compile(r"\b(?:\d[ -]?){15}\d\b")),
    ("[PHONE]", re.compile(r"\b0\d{4} ?\d{6}\b")),
]

PROMPTS = [
    "Hi, I am jo.walker@example.com and test card 4000 1234 5678 9010 was charged twice.",
    "Please call me on 07700 900123 about order 1120.",
    "What time does the shop open on Sunday?",
]


def redact(text):
    """Replace each match with its label. Return the clean text and how many were replaced."""
    found = 0
    for label, pattern in PATTERNS:
        text, n = pattern.subn(label, text)
        found += n
    return text, found


def to_span(prompt, run_id):
    """Metadata is kept long; redacted text goes to a short-lived store."""
    clean, n = redact(prompt)
    return {"run_id": run_id, "chars": len(prompt), "redactions": n}, clean


leaks = 0
for i, p in enumerate(PROMPTS, 1):
    meta, clean = to_span(p, f"run-{i}")
    print(meta)
    print("   text:", clean)
    if "@" in clean or re.search(r"\d{4} \d{4}", clean) or "07700" in clean:
        leaks += 1
print("leaks after redaction:", leaks)
Quick checkYour traces store full prompts that include customers' card numbers. What is the best fix?
How sure are you?
Pick an answer and how sure you are