Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 39 ยท Memory and Retrieval

Long-Term Agent Memory

Give an AI agent memory that survives between sessions: episodic, semantic and procedural memory, when to write and read them, and a runnable memory store.

Short-term memory disappears when a session ends. A useful assistant should remember that you prefer metric units, that your project uses PostgreSQL, or that last week's deploy failed because of a missing environment variable. That is long-term memory: information stored outside the context window and brought back when it is relevant.

In this lesson you will learn the three kinds of long-term memory, the decisions every memory system must make (what to write, where to store it, when to read it), and you will build a small working memory store you can run here.

Real memory products use databases and embedding models that are not available on this site. The code below is a plain-Python stand-in that shows the same design with keyword scoring.

Three kinds of memory

Cognitive scientists split human memory into types, and agent designers borrow the names.

Three kinds of long-term memory

Episodic What happened: past runs and chats
Semantic Facts and user preferences
Procedural How to do things: rules and recipes

Most agents start with semantic memory.

1 / 2
  • Episodic memory stores events: "On Monday the user asked for a refund and we issued one." It lets the agent learn from past runs.
  • Semantic memory stores facts: "The user lives in Berlin and prefers short answers."
  • Procedural memory stores instructions the agent follows, such as a refined system prompt or a saved checklist. Some systems let the agent rewrite its own rules after feedback.

A minimal memory store

A memory item needs text, a type, a timestamp and some way to find it. Below, retrieval scores items by how many words they share with the query. Real systems replace that scoring with embeddings (see embeddings and vector search), but the interface stays the same.

Python Write and recall memories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that ties are broken by recency (tick): when two memories match equally well, the newer one wins. Recency is a common ranking signal.

When should the agent write memory?

Writing everything produces noise. Two strategies work well:

  1. Explicit writes. The agent has a remember(text) tool and decides for itself. This is transparent and easy to audit.
  2. Background extraction. After a conversation ends, a separate model call reads the transcript and extracts durable facts. The user is not slowed down.

Either way, apply a filter. Good memories are durable (still true next month), useful (will change future behaviour) and safe (not secrets or sensitive data you have no right to keep).

When should the agent read memory?

Again two approaches. You can always retrieve at the start of a turn and inject the top hits into the prompt, or give the agent a recall(query) tool and let it decide. Always-retrieve is simpler and reliable for preferences; tool-based recall saves tokens when most turns do not need memory.

Python Inject recalled memories into a prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keeping the memory block short and clearly labelled helps the model treat it as background, not as an instruction.

Maintenance: updating and forgetting

Memories go stale. A good store supports:

  • Update: when the user says "I moved to Madrid", replace "lives in Berlin" instead of keeping both.
  • Merge: collapse near-duplicates into one item.
  • Expire: give episodic items a time-to-live, or lower their score as they age.
  • Delete on request: users must be able to see and remove what the agent knows about them.

Privacy and safety

Treat memory as personal data. Store only what you need, tell users what is remembered, let them delete it, and never write passwords or payment details. Also beware of memory poisoning: if untrusted web text can be saved as a memory, an attacker can plant instructions that influence every future session. Save facts from the user, not commands from documents (see guardrails and prompt injection).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which kind of memory stores facts such as a user's preferred language?

    Semantic memory holds facts and preferences; episodic holds events; procedural holds how-to rules.
  2. 2. A memory item is worth saving when it is...

    Filtering keeps the store small and relevant, and avoids storing sensitive data.
  3. 3. What is memory poisoning?

    If an attacker's instructions are stored as memory, they affect future sessions.
  4. 4. The user says they moved cities. What should the store do?

    Updating prevents the agent from holding two conflicting facts.

Summary

  • Long-term memory lives outside the window: episodic (events), semantic (facts), procedural (rules).
  • Every system decides what to write, how to store it, when to read it and how to maintain it.
  • Rank recalled items by relevance and recency, and inject only a few.
  • Keep memory auditable, deletable and free of secrets and untrusted instructions.

Next: Embeddings and Vector Search