09. Advanced Concepts
Advanced Memory Management
Advanced memory management for agents layers working memory (context window), short-term session history, episodic run logs, semantic facts/preferences, and procedural skills — with write, consolidate, retrieve, update, and forget lifecycles. Durable stores (SQL, vectors, caches) hold memory; the prompt only sees what retrieval selects under tenant and privacy rules.
What you will learn
- Name the main agent memory layers and what each stores
- Design write-consolidate-retrieve-update-forget flows
- Map memory types to SQL, vector, cache, and prompt stores
- Apply privacy, TTL, and conflict rules for durable memories
- Answer interview questions with short, solid answers
Definition
Agent memory is state the system keeps so future turns behave as if the agent “remembers.” Advanced management separates volatile context from durable stores, chooses what to write, how to summarize, what to retrieve, how to correct, and when to forget.
RAG retrieves world knowledge from a corpus. Memory retrieves user/run-specific state (preferences, prior decisions, skill tips). Many products need both.
Introduction
This lesson (#2 in Advanced Concepts) follows RAG and Agentic RAG. You can ground in documents; now persist and recall agent/user state across sessions without stuffing entire histories into every prompt.
Next: Long-Horizon Planning.
Why advanced memory
- Context windows fill; naive chat logs do not scale
- Users expect preferences and prior decisions to stick
- Long tasks need checkpoints beyond one request
- Wrong memories cause confident, personalized mistakes
- Privacy laws require delete/forget paths
Memory layers
Name the layer before you pick a database.
- Working - tokens in the active prompt/window
- Short-term - recent thread messages / session buffer
- Episodic - timestamped events from past runs
- Semantic - durable facts, profiles, preferences
- Procedural - how-to skills, tool recipes, SOPs
Lifecycle
Memory without consolidation becomes an unbounded dump.
- Write - extract candidates after a turn (structured fields)
- Consolidate - merge duplicates, summarize episodes
- Retrieve - relevance + recency + importance filters
- Update - user corrections overwrite or version facts
- Forget - TTL, user delete, legal hold exceptions
Where memory lives
Match store to access pattern — same Module 08 data lessons.
- Prompt/window - only the curated working set
- SQL/docs - profiles, episodic tables, audit
- Vector index - semantic recall of notes/summaries
- Redis/cache - short-lived session and locks
- Object storage - large artifacts referenced by id
Write and retrieve policies
- Prefer structured memory schemas (key, value, confidence, source)
- Do not write every utterance — write decisions and stable prefs
- Score retrieval by relevance, recency, and pinned importance
- Inject a small memory pack into the prompt, not the full DB
- Separate system facts from user-editable preferences
- Trace memory ids used in each run (observability)
Conflicts, privacy, safety
- Last-write-wins vs versioned facts — pick explicitly
- User “forget this” must delete or tombstone stores
- Tenant isolation on every memory query
- Never store secrets (API keys, raw cards) as “memories”
- Confirm high-impact memories (addresses, medical) before acting
- Retention schedules for episodic logs vs semantic profiles
Map to earlier modules
- RAG = corpus knowledge; memory = agent/user state
- SQL/NoSQL + vectors = durable memory backends
- Sessions in Agents SDK / frameworks ≈ short-term layer
- Checkpoints (LangGraph) ≈ episodic/run state
- Next: planning over long horizons using stored goals/state
Failure modes
- Stuffing full chat history every turn until quality collapses
- Hallucinated “memories” written without validation
- Stale preferences that contradict newer user statements
- Cross-tenant memory leaks from missing filters
- No forget path — GDPR/CCPA incidents waiting to happen
Program / implementation notes
Implement a memory service: extract structured candidates, upsert into SQL + optional vectors, retrieve top-N with ACL, and expose remember/forget tools. Keep the prompt pack tiny and cited by memory id.
- Unit-test consolidate/conflict rules without calling an LLM
- Next: Long-Horizon Planning
Advantages and limitations
- Advantage: personalized, multi-session agents
- Advantage: smaller prompts with better relevance
- Advantage: auditable remember/forget for compliance
- Limitation: bad writes poison future behavior
- Limitation: more stores and policies to operate
- Limitation: retrieval still approximate — confirm critical facts
Key points
- Separate working, short-term, episodic, semantic, and procedural memory.
- Lifecycle: write, consolidate, retrieve, update, forget.
- SQL for authoritative facts; vectors for fuzzy recall; cache for hot sessions.
- Retrieve a small, ACL-filtered memory pack into the prompt.
- Support corrections and user-driven delete/TTL.
- Memory is not RAG — but they often work together.
FAQs
Memory vs RAG?
RAG retrieves shared knowledge documents. Memory retrieves user- or agent-specific state (prefs, past events, skills). Many agents use both in one turn.
Is the chat transcript enough memory?
Only for short threads. Long-term products need consolidated semantic/episodic stores or the window and cost explode.
When use vector memory?
When you need semantic recall over many notes or summaries. Keep canonical profile fields in SQL for exact reads and updates.
How do you prevent wrong memories?
Structured extraction, confidence thresholds, user confirmation for high-impact fields, versioning, and easy correction/forget tools.
Interview questions
- What memory types do agents use?
Answer Working (context window), short-term/session, episodic (past events), semantic (facts/preferences), and procedural (skills/playbooks).
- What is memory consolidation?
Answer Turning raw interaction traces into compact, deduplicated durable memories — summaries and structured facts instead of endless transcripts.
- Where should long-term memories be stored?
Answer Authoritative fields in SQL (or similar), semantic notes often in a vector index, hot session state in a cache — not only inside the prompt.
- How do you retrieve memories safely?
Answer Filter by tenant/user, rank by relevance/recency/importance, inject a small pack with ids, and never bypass ACL for “helpfulness.”
- How does forget work?
Answer Delete or tombstone entries across SQL/vector/cache per user request or TTL, and ensure prompts stop retrieving those ids.
- How does this enable long-horizon planning?
Answer Goals, checkpoints, and intermediate results persist as episodic/semantic state so plans survive across many turns and process restarts.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Define one example record for episodic, semantic, and procedural memory for a travel agent.
- Design a consolidate rule when the user changes their preferred airline.
- List stores (SQL/vector/cache/prompt) for each memory type in that design.
- Write forget requirements for “delete my preferences” across those stores.
- Answer interview questions 1, 2, and 4 in two sentences each.