Deep dive 20 of 27: Sanitization layer
Nov 19, 2026 · Michael Chang · Status in NurseOps Now: ✅ built
Show definitions in ( ) · hover a highlighted word for its meaning, click it for details · open with ?defs=1 to show definitions
Part of: Remembering the right things: state management in NurseOps Now, section Implementing Long-Term Memory: External Persistence Architecture (7.3) · All 27 solutions · ← The Rehydration Pattern · TTL (time-to-live) expiration →
Read in this order
- In plain words: what it is, without jargon
- The problem it solves: the problem, and the standard approach in my words
- How to implement it (ServiceApps): scenario, steps, code and real output
- In NurseOps Now: built or not, and where
- Trade-offs and Interview answer
In plain words
A sanitization layer (code that masks personal details and secrets before anything is saved) masks personal data and secrets before anything is saved: chat memory, logs, traces (step-by-step records of each request: what ran, how long, what it cost), vector stores (a database that finds text by meaning, using embeddings). Saved state (everything the app must remember to continue correctly) outlives the conversation, so whatever goes in can leak later.
In plain terms: blacking out names and account numbers before a document goes into the archive.
The problem it solves
Persistence creates liability (saved data can leak personal details or secrets): if memory or logs store emails, phone numbers, passwords or API keys (a secret code that lets the app use a paid service), you've built a data leak.
The standard approach (in my words): always implement a sanitization layer (code that masks personal details and secrets before anything is saved) that scrubs secrets and personal data before writing state (everything the app must remember to continue correctly) to disk.
How to implement it (ServiceApps)
Illustrative example: no ServiceApps (a company platform for IT tickets, HR requests and help-center articles) instance is connected, so the scenario and data are made up. The code is a self-contained sketch; it was run on that made-up data on Nov 19, 2026, and the output below is from that run.
The setting. ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist, an AI helper inside a company's ServiceApps (a company platform for IT tickets, HR requests and help-center articles) portal. Employees ask it IT and HR questions; it answers from knowledge-base (KB (knowledge base: a library of help articles)) articles, looks up the employee's own tickets (incidents (a ServiceApps ticket for something broken) like INC0012345) and requests, and keeps one conversation per employee and ticket.
The scenario. An employee pastes their email, phone, a password and an API key (a secret code that lets the app use a paid service) into a ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist chat.
Steps.
- List the patterns to mask (emails, phones, IDs, passwords, API keys (a secret code that lets the app use a paid service)) plus known names.
- Run every text through
redact()before saving to memory. - Add a logging filter, so logs are masked automatically.
- Test it with fake data, like the example below.
The code (Python):
import re, logging, io
PATTERNS = [(re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+"), "[EMAIL]"),
(re.compile(r"\(?\b\d{3}\)?[ .-]\d{3}[ .-]\d{4}\b"), "[PHONE]"),
(re.compile(r"\b(sk|pk|api)[-_][A-Za-z0-9]{16,}\b"), "[SECRET]"),
(re.compile(r"(?i)password\s*[:=]\s*\S+"), "password: [SECRET]"),
(re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), "[SSN]")]
def redact(t):
for rx, rep in PATTERNS:
t = rx.sub(rep, t)
return t
class RedactingFilter(logging.Filter):
def filter(self, record):
record.msg, record.args = redact(record.getMessage()), ()
return True
buf = io.StringIO()
log = logging.getLogger("assist"); h = logging.StreamHandler(buf); h.addFilter(RedactingFilter())
log.addHandler(h); log.setLevel(logging.INFO)
msg = "I'm dana@example.com, 555-201-3344, password: Hunter2! and my key is sk_live1234567890abcdefXYZ"
log.info("question=%s", msg)
print("logged:", buf.getvalue().strip())
print("saved :", redact(msg))
Output (from running the code above):
logged: question=I'm [EMAIL], [PHONE], password: [SECRET] and my key is [SECRET] saved : I'm [EMAIL], [PHONE], password: [SECRET] and my key is [SECRET]
What to watch for.
- Pattern masking misses unusual formats; add exact-name masking for known people.
- Mask before the data reaches any third-party tracing (recording each request step by step) or analytics tool (a function the AI can call, such as policy search).
- Keep the original only where it's truly needed, with access control.
In plain terms: every note is run through a black marker before it's filed.
In NurseOps Now
Built: backend/app/pii.py masks SSNs (Social Security number), dates, emails, phones, member (a person covered by the health plan) IDs, NPI (a provider's national ID number) numbers, addresses and the case's member (a person covered by the health plan) and provider (the doctor or clinic asking for the test) names before chat memory is saved; a logging filter masks logs; LangSmith (a service that records each AI request step by step (tracing)) gets a masked thread (one saved conversation, here one per user and case) ID. The parent post's demo showed DOB, phone and SSN (Social Security number) masked. See Step 6.
Trade-offs
- Masking can remove something useful (a phone number the user wanted noted).
- Regex (short for regular expression: a text pattern the code matches) is fast but imperfect; NER models catch more but cost more.
Interview answer
"Everything persisted, from memory to logs to traces (step-by-step records of each request: what ran, how long, what it cost), goes through a sanitization layer (code that masks personal details and secrets before anything is saved) first: pattern masking for emails, phones, IDs and keys, plus exact-name masking for known people, and a logging filter so nobody can forget. Saved state (everything the app must remember to continue correctly) outlives the chat, so that's where leaks happen."
All data in this post is made up. No real people, tickets or patient data.
← Back to all 27 solutions in the state management blog
Key terms
Every highlighted word in this post links here. Click ↩ Back to return to the spot you came from.
- API key
- A secret code that lets the app use a paid service.Used in: The problem it solves, How to implement it (ServiceApps)↩ Back to where you were
- incident
- A ServiceApps ticket for something broken.Used in: How to implement it (ServiceApps)↩ Back to where you were
- KB
- Knowledge base: a library of help articles.Used in: How to implement it (ServiceApps)↩ Back to where you were
- LangSmith
- A service that records each AI request step by step (tracing).Used in: In NurseOps Now↩ Back to where you were
- member
- A person covered by the health plan.Used in: In NurseOps Now↩ Back to where you were
- NPI
- A provider's national ID number.Used in: In NurseOps Now↩ Back to where you were
- persistence creates liability
- Saved data can leak personal details or secrets.Used in: The problem it solves↩ Back to where you were
- provider
- The doctor or clinic asking for the test.Used in: In NurseOps Now↩ Back to where you were
- regex
- Short for regular expression: a text pattern the code matches.Used in: Trade-offs↩ Back to where you were
- sanitization layer
- Code that masks personal details and secrets before anything is saved.Used in: In plain words, The problem it solves, Interview answer↩ Back to where you were
- ServiceApps
- A company platform for IT tickets, HR requests and help-center articles.Used in: How to implement it (ServiceApps)↩ Back to where you were
- SSN
- Social Security number.Used in: In NurseOps Now↩ Back to where you were
- state
- Everything the app must remember to continue correctly.Used in: In plain words, The problem it solves, Interview answer↩ Back to where you were
- thread
- One saved conversation, here one per user and case.Used in: In NurseOps Now↩ Back to where you were
- tool
- A function the AI can call, such as policy search.Used in: How to implement it (ServiceApps)↩ Back to where you were
- traces
- Step-by-step records of each request: what ran, how long, what it cost.Used in: In plain words, Interview answer↩ Back to where you were
- tracing
- Recording each request step by step.Used in: How to implement it (ServiceApps)↩ Back to where you were
- vector store
- A database that finds text by meaning, using embeddings.Used in: In plain words↩ Back to where you were