Securing AI applicationsMiddleLesson 1458 min read

Logging and monitoring AI apps in production

You cannot investigate what you did not record. Log the right things about every AI interaction, protect the personal data inside those logs, and watch for abuse.

Lesson in motion

In 60 seconds

Logging and monitoring AI apps in production

You cannot investigate what you did not record. Log the right things about every AI interaction, protect the personal data inside those logs, and watch for abuse.

1/4
In simple words
A shop's CCTV only helps if it is recording, if someone watches for trouble, and if old recordings are deleted on time so they don't pile up forever.
The lesson "Watching what your agent does" listed what to record for agents. This lesson covers any AI app in production: chatbots, RAG search and AI features. The goals are to investigate incidents, spot abuse early, control cost and improve quality, without creating a new store of personal data that becomes a risk itself.

What to log for each interaction

FieldWhyPrivacy care
Request id, time, feature, tenantTie events togetherLow risk
User id (pseudonymous where possible)Per-user abuse and quotasUse an internal id, not name or phone
Model name and version, prompt template versionKnow which change caused whatLow risk
User input and model outputInvestigate abuse and qualityRedact personal data; restrict access; shorter retention
Retrieved sources (document ids)What influenced the answerLog ids, not full document text
Tool calls, arguments and resultsWhat the system actually didRedact sensitive arguments
Tokens, cost, latencyDenial of wallet and performanceLow risk
Safety signalsGuardrail blocks, refusals, canary hits, limit hitsLow risk

Redact before you store

Python: simple redaction of common Indian identifierspython
import re

PATTERNS = [
    ("EMAIL", re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")),
    ("PAN", re.compile(r"\b[A-Z]{5}[0-9]{4}[A-Z]\b")),
    # Indian mobile: optional +91, then 10 digits starting 6-9.
    # Runs before LONG_NUMBER, which would otherwise eat "+91 98..." whole.
    ("PHONE", re.compile(r"(?<!\d)(?:\+91[ -]?)?[6-9]\d{9}(?!\d)")),
    # 12 to 16 digits, spaces or dashes allowed: Aadhaar-like or card-like
    ("LONG_NUMBER", re.compile(r"(?<!\d)\d(?:[ -]?\d){11,15}(?!\d)")),
]

def redact(text: str) -> str:
    for label, pattern in PATTERNS:
        text = pattern.sub(f"[{label}]", text)
    return text
Pattern-based redaction catches common formats but misses a lot: names, addresses, health details, numbers written in words or in other scripts. Treat it as one layer. Also restrict who can read logs, and keep full conversation text for less time than metadata.

Signals worth alerting on

SignalWhat it may mean
One account near its token limit every day, or a sudden cost spikeDenial of wallet, or a bot using your feature
Repeated prompt-extraction or "ignore previous instructions" style inputSomeone probing your app
Your prompt canary appears in an outputThe system prompt has leaked
Many refusals or blocked outputs for one userJailbreak attempts
Outputs with links or images to unknown domainsPossible data exfiltration attempt
A tool used far more than usual, or for the first timeMisuse, or a loop
Retrieved documents from another tenantA permission filter failure: treat as an incident
  1. 1

    Baseline

    Learn what normal looks like for a few weeks: volume, cost, refusals, tools.
  2. 2

    Rules

    Write alert rules for the signals above; many teams use their SIEM, and formats such as Sigma for sharing detection rules.
  3. 3

    Route

    Each alert goes to an owner who knows the feature.
  4. 4

    Runbook

    Write what to do: block the account, turn off a tool, use the kill switch.
  5. 5

    Tune

    Remove noisy alerts, add new ones after every incident.

Retention and rules

Keep logs only as long as you need them for their purpose. India's Digital Personal Data Protection Act 2023 expects organisations to protect personal data and not keep it longer than needed. CERT-In issues directions on reporting cyber incidents and keeping logs. Ask your legal and compliance team which rules apply to you, and write the retention period down for each log type.
Watch out
Logs of AI conversations can hold everything users typed: personal details, complaints, sometimes passwords pasted by mistake. They are a valuable target. Encrypt them, limit access, log who reads them, and review before sending them to any third-party tool.
Try this. Look at one log line from an AI feature you know. Could you tell which model version, prompt version and documents produced that answer? Is any personal data in it that did not need to be there?

Watch and read more

Lab

A log record design, a working redaction function with tests, three alert rules and a retention table.

~35 min

The problem

Work on your own computer with the fake data in the starter.

  1. Run the starter. Make the redaction tests pass (the patterns may need fixing or reordering).
  2. Add one test of your own for a case the patterns miss, and write one sentence on why regex alone is not enough.
  3. Design a JSON log record for one chatbot turn with at least eight fields from this lesson. Mark which fields hold personal data.
  4. Write three alert rules in plain words: signal, threshold, who gets told, first action.
  5. Write a retention table: full text, metadata, cost data, security alerts.

Practice lab

Edit the code and run it. Python runs in your browser — nothing is sent to our server.

Output

No output. Add print(...) to see values.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1Why can the order of redaction patterns matter?Reveal

Because an earlier pattern can consume text that a later one needs. In the starter, the general "long number" pattern runs first and sees "91 9876543210" as a 12-digit number, so a phone number gets the wrong label and a stray "+" is left behind. Run the more specific patterns (a phone number with its country code) before the general catch-all ones, and use boundaries so a pattern cannot match inside a bigger number.

Q2An investigator needs the exact user message from 6 months ago, but your full-text retention is 30 days. How can you design for both privacy and investigations?Reveal

Keep metadata longer: request ids, user ids, model and prompt versions, document ids, tool calls and safety signals. When an alert fires or an incident opens, copy the related full records into a protected incident store with its own retention. Most investigations can start from metadata, and the most important text is preserved at the moment it becomes relevant.

Please sign in to continue.

Questions people ask

Should we log full prompts and outputs at all?

Often yes, because incidents are hard to investigate without them. But redact first, restrict access, and keep full text for a shorter time than metadata. For very sensitive features, consider logging only metadata plus a hash, with full text kept briefly for investigations.

How long should we keep AI logs?

There is no single answer. It depends on your purpose, your legal duties and your contracts. Decide per log type, write it down, and delete automatically when the time is up.

Can users ask us to delete their conversations?

Privacy laws often give people rights over their data, including erasure in many cases. Design logs so you can find and delete one user's data. Your legal team can say exactly what applies.

Does the model provider also keep our prompts?

It depends on the provider and your contract or settings. Read their data use and retention terms, and choose options that fit your data. This is part of your threat model, not an afterthought.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress