What an agent should remember—and when it should forget
Agent memory is information deliberately saved for later use. Short-term or session memory supports the current conversation; durable memory can preserve approved facts across sessions. Memory is not the model's weights, and it is not automatically everything in the current context—the application chooses what to write, retrieve, update, and delete.
What You Will Learn
- Distinguish working, episodic, semantic, and procedural memory.
- Separate memory from context, task checkpoints, and model weights.
- Apply explicit write, recall, update, and deletion policies.
- Handle stale or conflicting memories using provenance and recency.
Memory is a managed application capability
The model does not automatically decide that every conversation detail should be retained. The application applies a write policy: what is useful later, what is permitted to persist, how long it lives, and whether the user can inspect or delete it. A recall policy then selects only memories relevant to the current goal.
When memories conflict, prefer an authoritative source and newer verified evidence rather than silently combining both. Store provenance—who or what supplied the fact, when it was recorded, and why it was saved—so the system can resolve conflicts and explain important decisions.
A practical memory taxonomy
Do not confuse four different stores
Write, retrieve and retire a memory
Keep immediate messages and temporary task facts in short-term memory; keep durable task checkpoints mainly in the long-running-agent system.
Apply a write policy: save only information that will be useful later and that the user or policy allows you to retain.
Apply a recall policy: retrieve a few relevant memories instead of replaying everything, using semantic search when meaning-based lookup helps.
Update, version, or delete stale and conflicting memories, and record where important facts came from.
Protect stored information with authorization, privacy controls, retention limits, and user-visible correction or deletion.
Instructional figure
The memory lifecycle
How does an approved fact become useful memory without being stored forever or recalled blindly?
Worked Example: A preference is written, recalled, superseded, and deleted
A user explicitly says, 'Save PDF as my preferred report format.' The candidate value comes from that request; the application—not the model weights—owns the memory lifecycle.
- Write: policy confirms that a non-sensitive format preference may persist and records {format: 'PDF', source: 'user U-17', saved_at: '2026-09-12T09:00Z', version: 1}.
- Recall: during a later report task, the recall service retrieves the preference because report format is relevant. It adds the selected value and provenance to the current context; unrelated memories stay out.
- Conflict: the user later says, 'Use HTML from now on.' The newer explicit instruction conflicts with version 1, so the service creates version 2 and marks PDF superseded rather than combining both formats.
- Delete: when the user asks to forget the preference, the memory record is removed according to policy. A future report task asks for a format instead of guessing from the deleted value.
What the result means: The current context changes on each call, the memory store keeps only policy-approved history, a workflow checkpoint would track unfinished report work, and model weights remain unchanged throughout.
Instructional figure
Memory is managed application state—not automatic model knowledge
How does one explicit preference move through write, recall, conflict, update, and deletion?
A Realistic Example: A study-planning agent
During the session it remembers which quiz is active. Across sessions it may remember an approved preference like “use 20-minute lessons,” but should not silently store sensitive personal details.
Read the Mechanism in Code
Use the label and note to understand what this block represents before reading each line.
memory = {
"working": recent_messages[-8:],
"task_state": {"lesson": 4, "quiz_score": 7},
"retrieved": search_memories(user_id, current_goal)
}
response = agent.run(goal, memory=memory)Hands-On Runnable Python Lab: Update, Protect and Forget a Report Preference
User U-17 consents to storing a report-format preference. Revision 1 is PDF, revision 2 is HTML. The clock is a fixed integer so expiry and output are reproducible.
- Create PDF as version 1 with expiry time 200.
- Update to HTML using expected_version=1; store version 2.
- Reject another update still based on version 1. Also demonstrate that U-99 cannot retrieve U-17’s preference.
- Delete the current record and verify it is no longer returned.
Memory has versions, an owner and an end of life
Only version 2 is current. Stale writes are rejected, another namespace cannot recall this record, and expiry or deletion prevents subsequent recall.
memory = {}
def save(user, value, expected_version, consent, now=100):
if not consent:
raise PermissionError("memory consent required")
key = (user, "report_format")
old = memory.get(key)
version = old["version"] if old else 0
if version != expected_version:
raise ValueError("stale version")
memory[key] = {"value": value, "version": version + 1,
"source": "explicit user preference", "expires": now + 100}
return memory[key]["version"]
def recall(user, now=100):
record = memory.get((user, "report_format"))
if record is None or now >= record["expires"]:
return None
return record["value"]
print("version", save("U-17", "PDF", 0, True), recall("U-17"))
print("version", save("U-17", "HTML", 1, True), recall("U-17"))
try:
save("U-17", "PDF", 1, True)
except ValueError as error:
print("rejected:", error)
print("other user:", recall("U-99"))
print("after expiry:", recall("U-17", now=200))
del memory[("U-17", "report_format")]
print("after forget:", recall("U-17"))
version 1 PDF
version 2 HTML
rejected: stale version
other user: None
after expiry: None
after forget: NoneWhat This Run Shows
- HTML supersedes PDF rather than becoming a competing active preference.
- The stale writer fails visibly; automatically retrying with the new version without reconsidering the intent would defeat the protection.
- Expiry hides a record from recall; explicit deletion removes it from this demonstration store.
Memory should improve continuity without turning old information into permanent authority. Users need ways to inspect, correct and remove appropriate stored facts.
From the Exercise to an Application
Experiment: Call save with consent=False.
The write is denied even if the version is correct. Permission to remember and correctness of an update are separate requirements.
Summary / Key Takeaways
- Agent memory is information deliberately persisted for later use; context is the working information supplied for the current decision.
- Short-term/session memory supports the current interaction, while durable memories can preserve approved facts, preferences, or experiences across sessions.
- Useful memory types include episodic experiences, semantic facts/preferences, and procedural guidance, but each needs different retention and retrieval rules.
- A write policy decides what is worth storing; a recall policy decides what should be retrieved for the current goal.
- Memories can become stale or conflicting, so important records need provenance, update/version rules, and deletion paths.
- More memory is not automatically better because irrelevant or outdated memories can distract the model.
- Memory systems must respect privacy, authorization, retention, and user-visible correction or deletion requirements.
Verified Further Reading
These primary research papers and current official specifications support the concepts and implementation guidance in this lesson. The explanations, analogies, exercises, and code above remain original teaching material.