Skip to content

Module 04 · Lesson 4.5

Memory Retention, Privacy and Data Lifecycle

40 minUpdated 18 Nov 2026Part of State, Memory and Retrieval · AI Agent Development
On this page
  1. What you will learn
  2. The data lifecycle: from first note to deletion
  3. Decide what an AI agent should remember
  4. Pick a retention period for each kind of data
  5. Delete expired memory with a few lines of Python
  6. Let users see, correct and delete what is stored
  7. Write your memory and retention note
  8. Conclusion and your next step
  9. Common errors and fixes
  10. Validation checklist
  11. Hands-on exercise
  12. Knowledge check
  13. Frequently asked questions
  14. References

Think of a corner shop owner who writes notes about regular customers on a pad by the till. "Likes the crusty loaf." "Allergic to nuts." The notes help. But what if the pad is never thrown away, anyone walking past can read it, and the owner also scribbled a customer's card number on it? A helpful habit has turned into a risk.

An AI agent that remembers things is that pad. In this lesson you learn to decide what the agent may write down, how long it may keep it, who may read it, and how a person can ask to see or erase it. Most of the lesson needs no coding. You also get a fill-in memory and retention note that you will hand in with the Module 4 lab. One short Python example shows how old entries are removed automatically.

What you will learn

  • Describe the five stages of the data lifecycle for agent memory.
  • Decide what an agent should store and what it must never store.
  • Choose a retention period for each kind of stored data.
  • Write a memory and retention note, and run a short script that deletes expired entries.

Before you start

The data lifecycle: from first note to deletion

Persistent memory is information an agent keeps between conversations. Because it lasts, it needs a plan for its whole life. The data lifecycle is that life, from the day something is first written down to the day it is erased.

Compare it to milk in a fridge. You decide what to buy, where to put it, when to use it and when to check the date. Milk with no date check ends up at the back of the shelf, going off. Agent memory without a lifecycle does the same, except the thing going off is other people's personal data.

The five stages of the data lifecycle
  1. Collect

    Decide what to ask for or write down

  2. Store

    Choose where it lives and who can reach it

  3. Use

    Put it in the context only when a step needs it

  4. Review

    Check the dates and the purpose

  5. Delete

    Erase it when time is up or a person asks

Plan all five before the agent stores its first note. Most teams plan only the first three.

Personal data
Any information that points to a real person, such as a name, an email address, a delivery address or an order history.
Data minimisation
Keeping only what a stated purpose needs, and nothing more. If you do not store it, it cannot leak.
Retention period
How long a piece of data is kept before it is deleted automatically. Example: 90 days.
Purge
To delete a batch of data, usually everything that has passed its retention period.

Decide what an AI agent should remember

Start from the purpose, not from what is easy to save. Ask: "What would the agent do better next time if it remembered this?" If you cannot name a clear benefit, do not store it.

Store only what a stated purpose needs

Quillfeather Cycles wants its support agent to feel familiar. A customer once said they like parcels on Saturdays. Storing "preferred delivery day: Saturday" has a clear purpose. Storing the customer's full chat history "just in case" does not.

DataStore it?Why
Preferred delivery dayYes, with a retention periodSmall, useful and easy to explain to the customer.
Frame size the customer choseYes, with a retention periodSaves asking again. Fits the purpose of helping with sizing.
Full text of every conversationNo, keep only a short noteLarge, rarely needed and full of personal details.
Home addressNo, read it from the order system when neededThe order system is the single source of truth, so a copy only adds risk.
Card numbers, passwords, API keysNeverNo agent task needs them in memory. See the warning below.
Health details or other sensitive personal dataNeverThe harm from a leak is high and the shop has no need for it.
When in doubt, leave it out. It is easy to add a field later and hard to un-leak one.

Be open with users about what is remembered

People should not be surprised by what an agent knows about them. Tell them in plain words, for example: "I remember your preferred delivery day for 90 days. You can ask me to forget it at any time." A short, clear sentence at the moment you save something is better than a long policy nobody reads.

Pick a retention period for each kind of data

One rule for everything is easy to write and usually wrong. A delivery preference is useful for months. A conversation trace is useful for a few weeks, while you fix bugs. Pick a period for each kind of data, and base it on how long the purpose lasts.

Kind of dataPurposeExample retention period
Preference notes (delivery day, frame size)Make later chats smoother90 days after it was last saved
Conversation traces (what was asked, which tools ran)Find and fix mistakes30 days
Return requestsBusiness records. Your organisation's policy decides.Set by the organisation, not by the agent team
Test data with made-up customersDevelopment onlyDelete when the test run ends
The numbers are examples for a made-up shop. Your organisation sets the real ones.

A retention period only works if something enforces it. Writing "90 days" in a document does nothing. A scheduled job that deletes old entries does. The Python example below shows the idea.

Delete expired memory with a few lines of Python

The script keeps a tiny memory store in a list. Every entry has a customer ID, a note and the date it was saved. It uses two functions: one removes entries older than the retention period, and one removes everything about a single customer.

The script uses a fixed "today" of 18 November 2026, so you get the same output every time. A real job would use the current date.

Pythonmemory_purge.py
from datetime import date, timedelta

TODAY = date(2026, 11, 18)  # fixed, so the output never changes

store = [
    {"customer": "C-101", "note": "prefers delivery on Saturdays", "saved": date(2026, 10, 30)},
    {"customer": "C-102", "note": "prefers delivery before noon", "saved": date(2026, 6, 2)},
    {"customer": "C-101", "note": "asked about frame size M", "saved": date(2026, 4, 15)},
    {"customer": "C-103", "note": "prefers a phone call", "saved": date(2026, 9, 1)},
]


def purge_expired(store, today, keep_days):
    cutoff = today - timedelta(days=keep_days)
    return [entry for entry in store if entry["saved"] >= cutoff]


def forget_customer(store, customer_id):
    return [entry for entry in store if entry["customer"] != customer_id]


def show(title, entries):
    print(f"{title} ({len(entries)} stored)")
    for e in entries:
        print("  ", e["customer"], e["saved"], e["note"])


show("Before", store)
store = purge_expired(store, TODAY, keep_days=90)
show("After purge, 90 days", store)
store = forget_customer(store, "C-103")
show("After C-103 is forgotten", store)

Runs offline with Python 3.10 or later. Save it and run python memory_purge.py.

What each part does

  • TODAY = date(2026, 11, 18) is a fixed date. It lets you predict the result by hand.
  • store = [...] is the memory: a list of dictionaries (labelled records). Each has customer, note and saved.
  • purge_expired works out a cutoff date, which is today minus the retention days. It keeps only entries saved on or after that date.
  • timedelta(days=keep_days) is a length of time. Subtracting it from a date gives an earlier date.
  • forget_customer keeps every entry that does not belong to the given customer. Nothing about that customer is left.
  • Both functions return a new list instead of changing the old one. That makes them easy to test.
  • show prints the entries so you can compare before and after.
OutputCaptured from a real run
Before (4 stored)
   C-101 2026-10-30 prefers delivery on Saturdays
   C-102 2026-06-02 prefers delivery before noon
   C-101 2026-04-15 asked about frame size M
   C-103 2026-09-01 prefers a phone call
After purge, 90 days (2 stored)
   C-101 2026-10-30 prefers delivery on Saturdays
   C-103 2026-09-01 prefers a phone call
After C-103 is forgotten (1 stored)
   C-101 2026-10-30 prefers delivery on Saturdays

Entries from 2 June and 15 April are more than 90 days old, so the purge removes them. Then customer C-103 asks to be forgotten, and their entry disappears too, even though it was still inside the 90 days.

Let users see, correct and delete what is stored

Remembering something about a person gives them a reason to ask about it. Build three simple actions into the agent from the start.

  • See: "What do you remember about me?" The agent lists the stored notes in plain words.
  • Correct: "I now prefer Fridays." The agent replaces the old note rather than adding a second one.
  • Delete: "Forget my preferences." The agent removes the notes and confirms. In the example above, that is forget_customer.

Treat the delete action with care. It is a tool that destroys data, so check that the person asking is the owner of those notes. A customer must never be able to delete or read another customer's memory.

Logs and traces hold personal data too

A log is a record of what a program did. A trace is a log of one agent run: the question, the tool calls and the reply. Traces help you find bugs. They also contain the customer's words, order numbers and sometimes more than you expected.

  • Log what you need to debug, such as tool names and error codes. Skip the full customer text when you can.
  • Give traces their own, short retention period, such as 30 days.
  • Hide or remove obvious personal details before saving a trace, for example an email address.
  • Include traces when you handle a request to delete someone's data.

Who can access stored memory

Stored memory is a database. Apply the same least-privilege idea you met in Module 3: each person and each program gets only the access its job needs. The agent should read the current customer's notes only. Developers might see traces but not preference notes. Write the list down.

Write your memory and retention note

A memory and retention note is a one-page record of what your agent stores and why. It is the document you hand in with the Module 4 lab. Writing it forces you to answer every question above in one place.

The template

Copy this and fill in one block for each kind of data your agent stores. If the agent stores nothing, say so and still hand in the note.

Textmemory-and-retention-note.txt
Agent:                 [name of the agent]
Date written:          [YYYY-MM-DD]
Owner of this note:    [role, not a personal name]

Data kind 1:           [for example, preferred delivery day]
  What is stored:      [the exact fields]
  Why (purpose):       [what the agent does better because of it]
  Where it is stored:  [file, database or service name]
  Who can access it:   [roles and programs; say what they can do]
  Retention period:    [number of days and the event that starts the count]
  How it is deleted:   [automatic job, name and schedule]
  User control:        [how a person can see, correct and delete it]

Data kind 2:           [repeat the block for each kind of data]

Logs and traces:       [what is logged, where, how long, who can read it]
Never stored:          [secrets, card details, sensitive personal data]
Policy followed:       [name of your organisation's data policy]
Open questions:        [anything you are unsure about]

A filled-in example for the Quillfeather agent

Textquillfeather-memory-note.txt
Agent:                 Quillfeather support agent
Date written:          2026-11-18
Owner of this note:    Support team lead

Data kind 1:           Preferred delivery day
  What is stored:      customer ID, delivery day (Monday to Saturday), date saved
  Why (purpose):       Suggest a delivery day without asking again
  Where it is stored:  Table agent_preferences in the support database
  Who can access it:   The support agent (current customer only);
                       the support team lead (read only)
  Retention period:    90 days after it was last saved
  How it is deleted:   Nightly job removes entries older than 90 days
  User control:        Customer can ask "what do you remember?", change the
                       day, or say "forget my preferences" to delete it

Logs and traces:       Tool names, order IDs and error codes for each run.
                       Kept 30 days in the log service.
                       Readable by the two developers on the agent team.
Never stored:          Payment data, passwords, API keys, health details.
                       No payment data is kept by the agent at all.
Policy followed:       Quillfeather data handling policy (made-up example)
Open questions:        Do backups of the support database also expire
                       after 90 days? Ask the platform team.

Notice that every line is short and specific. "Kept for a while" would not pass. "Kept 30 days" would.

Conclusion and your next step

Agent memory is useful and risky. Plan its whole life: collect only what a purpose needs, store it with limited access, use it only when a step needs it, review it, and delete it on a schedule or when a person asks. Keep secrets and card details out entirely, and treat logs and traces as personal data too.

Your next step: fill in the memory and retention note for an agent you have in mind, or for the Quillfeather agent. Then continue to Practical Lab: Add Retrieval to a Support Agent, where you add retrieval to the agent and hand in a note like this one.

For more on keeping the context small and safe, read Context Design: What the Agent Needs at Each Step, or look up any unfamiliar word in the Glossary of AI Engineering and Agent Terms.

Common errors and how to fix them

The agent stores whole conversations "just in case".

Why it happens: Nobody asked what the stored data is for.

Fix: Name the purpose for each field. Store a short note instead of the full text, and drop anything with no purpose.

A retention period is written in the note but old data is still there.

Why it happens: No job enforces it.

Fix: Add a scheduled purge like purge_expired, and test it with a fixed date. Check backups and logs too.

A card number or password shows up in memory or in a trace.

Why it happens: Whole messages are saved without checking them.

Fix: Save only checked fields. Remove or mask sensitive text before it is logged, and tell the customer not to share such details.

A customer asks to be forgotten, but their data remains in traces.

Why it happens: The delete action only covers the main memory.

Fix: List every place data is kept in the note, and make the delete action cover each one or explain how fast it expires.

Validation checklist

Tick each item once you have confirmed it.

Hands-on exercise

Write a memory and retention note for a library agent

A town library agent helps members renew books. The team wants it to remember the member's preferred reminder method and keep conversation traces to fix bugs. Fill in the memory and retention note template for it.

You will need

  • The agent may remember one preference: reminder by email or by text message.
  • Traces contain the member's questions, tool names and the book titles discussed.
  • Members can ask the agent what it remembers.
  • Only the library's two developers read traces.

Acceptance criteria

Your work is done when every one of these is true.

Hints (3)
  • Ask what the agent does better because of each item. No answer means do not store it.
  • Think about who must not see the data as well as who may.
  • The note should name your organisation's policy instead of guessing the law.
Show a model answer

A strong answer has two data blocks and a logs line. Preference: reminder method (email or text) with the member ID and date saved. The purpose is sending reminders the way the member likes. It sits in the library database, and only the agent (current member) and the lead librarian (read only) can access it. Retention is 180 days after the last change, with a nightly purge. Members can ask what is remembered, change it, or say "forget my preference". Traces: tool names, book IDs and error codes, kept 30 days, readable by the two developers. Traces are shorter because they only exist for debugging. Never stored: passwords, library card PINs and payment details. The note also says which library data policy applies and flags backups as an open question.

Knowledge check

Choose an answer, then check it. Nothing is recorded — this is for you.

  1. 1.Which choice best follows data minimisation?
    Show the answer and explanation

    Answer: B. Minimisation means the purpose decides what is kept. Saving everything feels safe because it avoids missing something, but it creates a large pile of personal data that can leak or go stale.

  2. 2.What is a retention period?
    Show the answer and explanation

    Answer: B. It is the time limit on stored data. The first option sounds similar, but that is the context window, which is about a single conversation and not about stored records.

  3. 3.A customer types a card number into the chat. What should the agent's memory do?
    Show the answer and explanation

    Answer: C. Card details never belong in agent memory, logs or prompts. A short retention period does not make it safe, because the data would still be copied into traces and backups during that time.

Frequently asked questions

What is the difference between conversation state and persistent memory?

Conversation state is what the agent holds during one chat, such as the last few messages. Persistent memory is saved between chats, such as a preferred delivery day. State disappears when the chat ends. Memory stays until you delete it, which is why it needs a retention period.

How long should an AI agent keep user data?

Only as long as the purpose needs it. A preference might be kept a few months, and a debugging trace a few weeks. Your organisation's data policy has the final say, and privacy rules differ by country, so check it before storing real data.

Can an AI agent forget something I told it?

The agent itself does not forget. Your code deletes the stored entry, and then the agent can no longer read it. That is why a delete action, such as removing every note for one customer, is a feature you build on purpose. Remember to clear logs, backups and search indexes too.

Do logs count as personal data?

Often yes. A log that holds a customer's question, an order number or an email address is personal data. Keep logs small, remove details you do not need, and give them a short retention period like any other stored data.

Written by
PromptFable Editorial
Last updated 18 Nov 2026
Review
Reviewed by Waseem Nadir, Technical Reviewer
Last reviewed 27 Nov 2026

References

Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.

PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.