Preserving Privacy While Keeping Utility
This lesson covers privacy techniques that keep generative AI useful while limiting exposure of personal data, and retention policies that ensure data does not live longer than necessary.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish masking, tokenisation and anonymisation.
- Build a privacy-preserving inference flow.
- Define retention policies for prompts, responses and logs.
Masking, tokenisation and anonymisation
Several techniques reduce the exposure of personal data while keeping it useful, and they differ in whether the original values can be recovered. Masking replaces a value with a placeholder for its type, such as [NAME], which removes it entirely; it suits logs and analytics that never need the value. Tokenization, also called pseudonymisation, replaces each value with a consistent surrogate, such as CUSTOMER_17, and keeps the mapping in a secure store. The model can still tell people apart and reason about relationships between them, and authorised services can restore the real values afterwards.
Generalization replaces precise values with ranges or categories, such as an age of 30 to 39 instead of 34, or a region instead of a city, which keeps data useful for analysis while making individuals harder to identify. Anonymization irreversibly removes identifying information so that individuals cannot be re-identified, even by combining fields, which some analytics and data-sharing arrangements require.
The following diagram compares masking with consistent pseudonymisation.

Privacy techniques
A privacy-preserving inference flow
A typical flow detects PII in the user's input with Comprehend, replaces each entity with a token and stores the mapping in an encrypted DynamoDB table with a short TTL, sends the tokenised prompt to the model, and then replaces tokens in the response with the original values only for the authorised user before display. Logs and analytics keep only tokenised text. Guardrails provide a final mask for anything that was missed. The model sees no real identities, yet the user receives a personalised answer.
entities = comprehend.detect_pii_entities(Text=text, LanguageCode="en")["Entities"]
mapping, safe = {}, text
for e in sorted(entities, key=lambda e: e["BeginOffset"], reverse=True):
value = text[e["BeginOffset"]:e["EndOffset"]]
token = mapping.setdefault(value, f"[{e['Type']}_{len(mapping) + 1}]")
safe = safe[:e["BeginOffset"]] + token + safe[e["EndOffset"]:]
store_mapping(session_id, mapping, ttl_minutes=30) # encrypted, short-lived
answer = call_model(safe)
for value, token in mapping.items():
answer = answer.replace(token, value) # restore only for the authorized userThe following diagram shows personal data replaced with tokens before the model call.

A tokenised inference flow
Retention policies
Data that no longer exists cannot leak, so retention policies limit how long prompts, responses, transcripts, uploaded files and logs are kept. The periods come from legal and business requirements and are documented. Each store has its own mechanism. S3 Lifecycle configurations expire objects after a period, for example deleting raw uploads after 30 days and invocation logs after one year, or move them to cheaper storage classes first. CloudWatch Logs log groups have a retention setting, and DynamoDB TTL expires conversation history automatically.
Generative AI systems keep copies of data in places that are easy to forget. When a document is deleted from its source, it must also be removed from the knowledge base, by running a sync, and from any caches of responses that used it; otherwise the assistant continues to quote deleted information. The following table compares the privacy techniques.
| Technique | Reversible | Keeps utility for |
|---|---|---|
| Masking | No | Tasks that do not need identities |
| Tokenization / pseudonymisation | Yes, with the mapping | Reasoning about distinct entities; personalised replies |
| Generalization | No | Analytics and segmentation |
| Anonymization | No | Sharing and analytics without re-identification |
Retention decisions
Utility versus privacy
A claims assistant must compare statements from a policyholder and two witnesses. Masking all names as [NAME] made the model confuse who said what.
Consistent pseudonymisation (PERSON_1, PERSON_2, PERSON_3) keeps the distinction while hiding real names; the mapping is stored encrypted with a short TTL and used to restore names only in the adjuster's view.
Name three mechanisms that expire stored generative AI data.
Show answer
S3 Lifecycle rules, CloudWatch Logs retention settings and DynamoDB TTL.
Exam Tips
Remember for the exam
- Masking removes values; pseudonymisation keeps consistent, reversible surrogates.
- Restore real values after the model call, only for authorised users.
- Expire data with S3 Lifecycle, log retention and DynamoDB TTL.
- Deleting source data requires syncing the knowledge base and clearing caches.