Data security
Data masking vs tokenization vs encryption for AI features
Decide what the model needs to see before you decide how to hide the rest.
In brief
Data masking vs tokenization comes down to one question: will anyone ever need the original value back? Masking replaces personal data for good, so use it for test data, logs and analytics. Tokenization swaps it for a token that only an authorized service can reverse, so use it in prompts when the answer must name the person. Encryption protects data at rest and in transit, but a model cannot read ciphertext, so it never replaces the other two.
On this page
01Data masking vs tokenization vs encryption: what each one does
All three keep personal data away from people and systems that should not see it. They differ in whether the original value can come back, and who holds the means to bring it back.
Data masking replaces a real value with a fake or blanked one, and the original is not recoverable from the masked copy. "Maria Lopez, born 03/14/1981" becomes "[NAME], born [DATE]" or a realistic fake such as "Ana Ruiz, born 07/02/1979". Static masking produces a masked copy of a dataset, which is what you want for test data and evaluation sets. Dynamic masking hides values at read time, so a support agent or a log viewer sees a masked field while the database keeps the real one. We cover the techniques in more depth in our guide to data masking for AI.
Tokenization replaces a value with a stand-in that maps back to the original through a lookup your own service controls. The token itself carries no information about the person. In an AI feature this usually means "Maria Lopez" goes to the model as "PATIENT_1", and your backend swaps the real name back into the response before the user sees it. The mapping table is the sensitive part, and it never leaves your environment.
Encryption transforms data with a key, and anyone with the key can reverse it. It is the right control for databases, backups, files and network traffic. It does not help inside a prompt: if you encrypt a name and send the ciphertext to a model, the model sees noise, and if you decrypt it before the call, the model sees the real name. Encryption decides who can read stored data. It does not decide what the model reads.
02The comparison table
| Data masking | Tokenization | Encryption | |
|---|---|---|---|
| Reversible | No, the original is gone from the masked copy | Yes, through your token map or vault | Yes, by anyone holding the key |
| Where it is applied | Copies of data, query results, log and trace output | At the boundary before data leaves your system, such as the prompt builder | Storage, backups, files, network traffic |
| What the model sees | A placeholder or a realistic fake value | A consistent stand-in such as PATIENT_1 | Nothing useful if encrypted; the real value if decrypted first |
| Effect on answer quality | Small if the task does not need the value; poor if it does | Close to the original, because the model can still tell people and records apart | None for the model, since it does not apply at the prompt |
| Key or map management | None once masked | A token map or vault, access controlled and audited | Keys in a key management service, with rotation |
| Typical use in an AI feature | Test data, evaluation sets, logs, analytics | Prompts where the answer must refer back to a real person or account; card data | Everything at rest and in transit, including stored prompts and responses |
The table hides one detail that matters in practice. Tokens must be consistent within a conversation. If "Maria Lopez" becomes PATIENT_1 in the first message and PATIENT_3 in the second, the model loses the thread. If two people both become [NAME], the model cannot tell them apart, and the answer quality drops. That is the main reason to tokenize prompts instead of masking them.
03Which to use where in an AI feature
Follow the data through the feature and choose a control at each stop. A typical path in a live product looks like this:
- 01
User input and records reach your backend.
Detect personal data here, on your side. Pattern matching catches structured values such as Social Security numbers, card numbers, phone numbers and dates. Names and addresses in free text need entity recognition, and you should test it on your own records, because notes written by clinicians or agents are messier than any sample set.
- 02
The prompt builder assembles the request.
Send only the fields the task needs. Tokenize what the answer must refer back to, and mask or drop the rest. A summary of symptoms does not need a date of birth. A reply that greets the customer by name needs a token, not the name.
- 03
The model call leaves your environment.
By this point the prompt should hold no value you would not show the model provider. Encryption in transit protects the call itself.
- 04
The response comes back.
Your backend swaps tokens for real values, inside your environment, for the user who is allowed to see them.
- 05
Logs and traces are written.
Mask here. Traces are where personal data leaks most quietly, because they capture full prompts and responses and are read by many engineers. Our article on AI data leakage covers the other routes.
- 06
Test data and evaluation sets are built.
Use statically masked copies with realistic fakes, so tests exercise the same shapes and edge cases as production without carrying real people into a test environment.
- 07
Analytics and reporting run.
Mask or aggregate. Analysts need counts and trends, not names.
Encryption sits under all seven steps: the database, the token map, stored prompts and responses, backups and the network. Treat it as the floor, not the strategy. For the wider set of controls, see our guide to AI data security.
One more rule saves real trouble: apply the same detection to retrieved documents as to user input. A retrieval step can pull a scanned referral letter or an email thread into the prompt, and those carry names and dates the user never typed.
04Healthcare and payments: what HIPAA and PCI DSS expect
Healthcare. HIPAA's minimum necessary standard asks covered entities and business associates to make reasonable efforts to limit protected health information to the minimum necessary for the purpose (45 CFR 164.502(b)). Read for an AI feature, that is a rule about the prompt: send what the task needs and nothing more.
The de-identification standard in 45 CFR 164.514 gives two routes. Safe Harbor removes 18 kinds of identifiers, starting with names and including all elements of dates except the year, such as a birth date or an admission date. Expert Determination has a qualified expert confirm that the risk of identifying someone is very small. Removing names and birth dates is a strong start, but it is not Safe Harbor on its own: free-text notes also carry addresses, phone numbers, record numbers and dates of visits.
The same section also speaks to tokenization. Under 164.514(c), a covered entity may assign a code that lets it re-identify de-identified data, provided the code is not derived from information about the individual and the re-identification mechanism is not disclosed. That rules out building tokens from the record itself, such as a hash of the medical record number. Use random tokens, keep the map inside your environment and never send it with the prompt. If protected health information still reaches the model provider, you need a business associate agreement with that provider; our HIPAA and AI article and the free HIPAA-ready AI guide cover that side.
Payments. The PCI Security Standards Council's tokenization guidelines define tokenization as replacing the primary account number with a surrogate value called a token, and say that storing tokens instead of card numbers can reduce the amount of cardholder data in the environment. The same document notes that if the merchant can retrieve the card number, its environment stays in scope, and that the tokenization system itself is part of the cardholder data environment. For an AI feature the practical answer is simple: a card number has no business in a prompt. A billing assistant can answer "why was I charged twice" from a token, the amount and the dates.
The OWASP Top 10 for LLM applications lists sensitive information disclosure as a top risk and recommends masking, tokenization and redaction before data is processed. The regulations and the security guidance point the same way: decide what reaches the model, and control it before the call.
05What we see in production
In MIRA's AI patient intake, the commercial version collects names and dates of birth, so both are removed on our backend before any intake reaches OpenAI. "Patient names and dates of birth are removed before any call to OpenAI, and the AI drafts a summary without diagnosing." Only de-identified answers reach the model, and each clinic sees only its own notes. The summary does not need the patient's name to be useful, so removing it costs nothing in answer quality.
In the CleanHaus assistant, the risk sat in the logs and the replies. Agent runs are traced in LangSmith and logged in CloudWatch, and the security boundary includes a runbook that requires redacting customer data and tokens before logs are shared. Guards keep tool names, IDs and internal fields out of the reply, so internal identifiers never surface to users.
In payments, we moved Coast Technology's live invoicing portal to a new gateway and kept tokenization at the center of the design. On the payment gateway case study, card data stays in the gateway's own form and the backend only sees a token. A small token service on its own instance requests the session token, so card data never reached the backend. The same principle applies to an AI feature on top of a billing system: the model, like the backend, should only ever see the token.
If your AI feature is already live, our AI Scorecard tests it on 50 of your own cases in two weeks for $5,000 and checks whether one account can reach another account's data. If you are still deciding what the model should see, start with a free 30-minute Product Review.
Frequently asked questions
Is tokenization the same as encryption?
No. Encrypted data can be reversed by anyone who holds the key, and the ciphertext is derived from the original value. A token is a random stand-in that can only be reversed through a lookup your own service controls. For prompts, tokenization is the useful one, because the model can work with a token but not with ciphertext.
Does removing names and birth dates make data de-identified under HIPAA?
Not by itself. Safe Harbor under 45 CFR 164.514(b) removes 18 kinds of identifiers, and free text often holds others, such as addresses, phone numbers, record numbers and visit dates. Removing names and birth dates sharply reduces risk in a prompt, but call data de-identified only after Safe Harbor or an Expert Determination.
Will masking hurt the model's answers?
Only when the task needs the masked value. A clinical summary or a support answer rarely needs a real name, so quality holds. When the answer must tell people apart or refer back to them, use consistent tokens instead of one generic placeholder, and test answer quality on your own cases before and after.
Should we mask data in LLM logs and traces?
Yes. Traces store full prompts and responses and are read by more people than the production database. Mask at write time, keep retention short, and treat the raw trace store with the same access controls as your production data. When an engineer needs a real value to debug, look it up in production under that access, not in the trace.
How Datics helps
Datics specializes in brownfield AI: production AI built into established software, one capability at a time, in your codebase and under your name.
›References (4)
Related work
Case studies from our projects on the same subject.
MIRA
Healthcare · Multi-clinic intake SaaS
Clinics sign up, pay per provider and run their own AI patient intake
The AI intake worked in three practices but was hard coded. Now any clinic signs up, pays per provider and runs it, and names never reach the model.
CleanHaus
Property Management · Turnover operations SaaS
Hosts and cleaners manage rental turnovers in chat, within each person's permissions
Owners, managers and cleaners all talk in one property chat. Now an AI there acts for each of them, only within that person's role.
Coast Technology
Other software · Multi tenant invoicing and payments portal
Organizations kept billing customers while we swapped the payment gateway and cloud account
A live invoicing portal had to change payment gateways and cloud accounts. We did both inside the running portal, and card data never reached the backend.
Your customers already expect AI from your product.
Let us make it real inside the product you run today.
