# How Do You Log Without Storing Sensitive Data?

> How to keep logs useful for debugging and audits without storing names, card numbers or medical notes, explained simply for your AI interview.

**In short:** Log what happened, and keep the personal details out. Write IDs, times and outcomes, mask or remove personal and secret values before anything is written, and delete logs on a fixed schedule. OWASP says sensitive values should usually be removed, masked or hashed before they are logged.

AI and LLM interview question Q242, Guardrails and AI safety. Intermediate, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-log-without-storing-sensitive-data

## Say it in 30 seconds

A log is a record of what my software did, and it is how we debug and audit. The risk is that it also copies personal data, like names, card numbers and medical notes. So I log IDs, times and outcomes. I mask or remove sensitive values before anything is written, and I replace a person's name with a keyed code so events still link. I also set how long logs live and who can read them. So the rule is: log what happened, and point to the record.

## Say it in 2 minutes

Logs are how we find bugs, prove what an AI agent did, and spot attacks. But a log is a copy, and copies spread. Whatever my code prints can end up in a log store that many engineers, support staff and tools can search. So the real question is what each line is allowed to contain.

I start by deciding what each event needs. For an insurance claims agent, that is the claim ID, the step, the tool called, the route chosen, the time taken and the error code. It never needs the claimant's diagnosis or a card number to debug a route. Anything personal is masked or removed before the line is written, because a copy waiting in a queue is still a copy.

People get a stand-in code instead of a name. I make it with a keyed hash, so the same person still links across events, and nobody can reverse it without the secret key. OWASP's logging guidance says sensitive values should usually be removed, masked, sanitised, hashed or encrypted before they reach a log, and it lists passwords, access tokens and card data among them.

The last part is time and access. Each kind of log gets a retention period, written down, and then it is deleted. Only the people who need it can search it. My quick check: would I be fine with this line on a support ticket that a vendor reads? If not, it does not go in the log.

## What is a log, and why is it sensitive, in plain words?

A log is the diary your software writes while it runs: which step ran, when, what it chose and why it failed. Engineers read logs to fix bugs, auditors read them to prove what happened, and security teams use them to spot an attack.

The catch is that a diary written by code copies whatever passes through it. If a claimant's name, card number or diagnosis reaches the logging line, it lands in the log. From there it is stored, copied to other tools and read by people who never needed it.

Think of a hospital ward's shift handover sheet. It notes that bed 12 had a fall at 3 am and the doctor was called. It does not copy the patient's full file onto the sheet, because the sheet goes round the whole floor. A log should work the same way: say what happened, and point to the record. An AI claims agent is the same. Its log should show that claim C-4471 went to fast track, with no diagnosis in it.

- **Log:** A record your software writes as it runs, kept so people can debug and audit later.
- **Sensitive data:** Any value that can hurt a person or the business if it leaks: names with contact details, cards, health details, passwords.
- **Redact:** Remove or hide a value before it is written, so only a safe version is kept.
- **Pseudonym:** A stand-in code for a person, so events can be linked without storing the name.
- **Retention:** How long a log is kept before it is deleted, written down as a rule.

## Where does sensitive data sneak into a log?

Almost never on purpose. A developer logs a whole object to chase one bug, and the object carries everything in it. The usual leaks are:

- **Whole objects:** A debug line prints the entire claim record, with the name, date of birth and diagnosis inside it.
- **Prompts and replies:** The full text sent to the model, and its answer, often hold personal details the user typed.
- **Error handlers:** A failed tool call logs the whole request, so the payload travels into the log with it.
- **Tool results:** A web page or a database row the agent fetched can carry personal data nobody meant to log.
- **Copies:** Once a value is in a log, it reaches backups, search indexes and support exports too.

Here is a worked case. An insurance claims agent reads a nurse's note saying the claimant had a knee scan on 3 March. It calls a tool to fetch the scan report, the tool times out, and the error handler logs the whole request. Now the diagnosis sits in the log, where every engineer on call can search for it. The fix sits in the logging code: the claim text never reaches the logger.

## How do you keep the useful part and drop the rest?

Decide what each event needs before you write any logging code. A claims agent needs the claim ID, the step, the tool it called, the route it chose, how long it took and the error code. It never needs the diagnosis or a card number to debug a route.

1. **Decide the fields first:** Write down the few fields each event needs. Anything else is left out by default, not left in by accident.
2. **Mask before the write:** Remove or mask values in the logging code, before the line is written. A copy waiting in a queue or buffer is still a copy.
3. **Pseudonymise people:** Replace a name or email with a code made by a keyed hash. The same person still links across events, and nobody can reverse it without the secret key.
4. **Limit keeping and reading:** Set how long each kind of log lives, delete it on schedule, and limit who can search it.

*Diagram, One claim, two logs: The same claim makes two logs. The old way copies the raw message, so anyone who can search the logs reads the diagnosis. The safer way masks it first, so the log holds an ID, a route and a time.*
1. The raw message goes straight into the log.
2. The same message is masked before anything is written.
3. Anyone who can search the logs reads the diagnosis.
4. The log keeps only the claim ID, the route and the time.

OWASP's logging guidance says the same thing in its own words:

> "The following should usually not be recorded directly in the logs, but instead should be removed, masked, sanitized, hashed, or encrypted" (OWASP, logging cheat sheet)

Pseudonyms need a keyed hash. A plain hash of an email can be guessed by hashing every email someone might use. A keyed hash needs a secret key too. Python's standard hmac module is built for this, and its docs say the key is the secret part.

## What does a safe log line look like?

Here is a small claims step in plain Python. It masks an email and a card number, and swaps the claimant's email for a keyed code. The marked lines do the masking and the pseudonym, and log_line writes only the safe fields.

```python
# A claims triage agent logs every step, but never the raw claim text.
import hashlib
import hmac
import re

EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")
CARD = re.compile(r"\b\d{4}(?: \d{4}){3}\b")
KEY = b"demo-only-key"  # real keys live in a secret store, never in code

def redact(text):
    text = EMAIL.sub("[email]", text)
    return CARD.sub(lambda m: "card ending " + m.group(0)[-4:], text)

def pseudonym(person):
    return hmac.new(KEY, person.encode(), hashlib.sha256).hexdigest()[:10]

def log_line(claim_id, person, note):
    return f"claim={claim_id} person={pseudonym(person)} note={redact(note)}"

note = "Claimant asha.rao@example.com paid 4111 1111 1111 1111 for the MRI."
print(log_line("C-4471", "asha.rao@example.com", note))
print(log_line("C-4471", "asha.rao@example.com", "Adjuster asked for the scan report."))
print(redact("Second opinion from dr.mehta@example.org"))
```

Output:

```text
claim=C-4471 person=65588f27ad note=Claimant [email] paid card ending 1111 for the MRI.
claim=C-4471 person=65588f27ad note=Adjuster asked for the scan report.
Second opinion from [email]
```

The claimant's email and the card number never reach the log, and the person appears only as a keyed code. A name written in free text would need its own rule. A real system would also strip medical words it does not need, and check the output in tests.

### How would you log an AI agent that handles medical claims?

- **Log it: IDs, steps and timings.** Claim ID, step name, tool called, route chosen, latency and error code.
- **Keep it elsewhere: The claim record.** Diagnosis, names and bank details stay in the claims system, behind its own access rules.
- **Never log: Secrets and whole prompts.** Passwords, API keys, access tokens, card numbers and full prompts or replies.

Then set a retention period for each kind of log, for example a short one for debug output. The number is your team's choice, agreed with legal and privacy staff, and written down so it can be checked.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a log is a record, and it copies whatever reaches the code.
- [ ] Where sensitive data sneaks in: whole objects, prompts, replies and error handlers.
- [ ] Masking or removal before the write, not after.
- [ ] A keyed hash for people, so events still link without the name.
- [ ] Retention and access limits, written down and enforced.
- [ ] Logs are a data store too, with the same care as the database.

**Common traps**

- Trap: "We encrypt the logs, so it is fine." Better: Encryption protects the file, but everyone with the key still reads the data, and it stays until someone deletes it. Keep the value out.
- Trap: "We will delete it from the logs later." Better: Copies sit in backups, queues and search indexes too. Stop the value reaching the log in the first place.
- Trap: "We just turn debug logging off in production." Better: You then lose the trail for an incident. Keep IDs, times and outcomes on, and keep values out.

## Can you spot the leak?

**Spot the leak:** Which log line is safe to keep for a claims agent?

A. log.debug("Prompt sent: %s", full_prompt)
B. log.info("Claim C-4471 routed to fast track in 1.8 s")
C. log.info("Card on file for claim: %s", card_number)
D. log.error("Tool failed with request %r", request)

**Answer:** B. It has a claim ID, a route and a time, and no personal data. The others copy a whole prompt, a card number or a whole request, and each holds the diagnosis or payment details.

**Quick check:** A triage agent picks the wrong route, and the team needs the trail to fix it. What keeps the trail without storing the personal data?

A. Turn logging off in production
B. Log the whole prompt and encrypt the log file
C. Log claim ID, route and step with masked fields, and keep the full claim in its own system
D. Log a hash of the whole prompt

**Answer:** C. The trail stays, and the personal data stays in the claims system behind its own access rules. Turning logs off loses the trail, encryption still keeps the data until it is deleted, and a hash of the prompt cannot show what the agent saw.

## Can you answer these from memory?

**Flash cards**

- **What goes in a log?** IDs, times, steps and outcomes. Values only when they are masked or removed.
- **Where do you mask?** Before the write. A copy in a queue or buffer is still a copy.
- **Why a keyed hash for people?** Events still link to one person, and nobody can reverse it without the secret key.
- **Which values should stay out of logs?** Passwords, access tokens and bank or card holder data.
- **What is retention?** How long a log lives. Write it down, then delete on schedule.
- **Is encryption enough?** No. It protects the file, but people with the key still see the data.

## Likely follow-up questions

- Q212 How do you detect and redact PII? https://designervenkat.online/interview/ai/how-to-detect-and-redact-pii
- Q213 What is sensitive information disclosure? https://designervenkat.online/interview/ai/what-is-sensitive-information-disclosure
- Q260 What is tracing for LLM apps? https://designervenkat.online/interview/ai/what-is-tracing-for-llm-apps
- Q464 Data retention and zero-data-retention? https://designervenkat.online/interview/ai/data-retention-and-zero-data-retention
- Q238 How do you keep API keys away from the model? https://designervenkat.online/interview/ai/how-do-you-keep-api-keys-away-from-the-model

## Learn it in depth

- [Tracing and logging](https://designervenkat.online/learn/ai-engineering/tracing-and-logging)
- [Security and Privacy Concerns](https://designervenkat.online/learn/ai-engineering/security-and-privacy-concerns)
