Lesson 3 of 3 Data Security and Privacy for GenAI

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.

Masking compared with consistent pseudonymisation for a claims assistant. Masking every name as NAME made the model confuse who said what. Replacing each person with a consistent token, PERSON_1 to PERSON_3, keeps the statements distinct while hiding real names, which are restored only in the adjuster's view.
Pseudonymisation keeps the relationships the task needs.
Match the pairs

Privacy techniques

Exam skills 3.2.3

Match each privacy technique with its description.

Log in to try this activity Interactive activities are free with an account.

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 user

The following diagram shows personal data replaced with tokens before the model call.

A privacy-preserving inference flow. Comprehend detects PII in the input, each entity is replaced with a token and the mapping is kept in an encrypted DynamoDB table with a short TTL, the model sees only tokens, and tokens in the response are replaced with the original values only for the authorised user.
The short TTL limits how long the mapping exists.
Put in order

A tokenised inference flow

Exam skills 3.2.3

Put the steps of a privacy-preserving inference flow in order.

Log in to try this activity Interactive activities are free with an account.

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.

TechniqueReversibleKeeps utility for
MaskingNoTasks that do not need identities
Tokenization / pseudonymisationYes, with the mappingReasoning about distinct entities; personalised replies
GeneralizationNoAnalytics and segmentation
AnonymizationNoSharing and analytics without re-identification
True or false

Retention decisions

Exam skills 3.2.3

Check three claims about setting retention for generative AI data.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.