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.
11 min readIntermediateOccasional · estimateFacts checked 27 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
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.
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.
Key words
- 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.
One claim, two logs
Claim message
Name, diagnosis, card
Old way
Write it all to the log
Raw text is stored as is
Safer way
Mask, then write
Only IDs and masked values
Anyone with log access
Reads the diagnosis
Claim C-4471 routed
Fast track, 1.8 s
- 1The raw message goes straight into the log.
- 2The same message is masked before anything is written.
- 3Anyone who can search the logs reads the diagnosis.
- 4The 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.
# 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
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."
BetterEncryption 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."
BetterCopies 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."
BetterYou 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?
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?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Learn it in depth
Last updated on 27 November 2026
Building an AI system that handles personal data?
I design and build AI products and agents, including the logging, masking and access rules behind them. Book a short call and tell me what data your system touches and who reads the logs.