Memory
Memory
Learn context, session state, durable memory, summarization, and retrieval boundaries.
flowchart TB Q[Current request] --> C[Current context] C --> R[Memory retrieval] R --> X[Relevant memories] Q --> S[(Session state)] K[(Knowledge)] --> R C --> M[Model]
Lesson overview
Memory
Memory is information an agent can use beyond the immediate model output. The key design decision is to distinguish current context, session state, durable memory and external knowledge.
Context
Context is what the model sees for the current decision: recent messages, current task state, retrieved evidence and tool results. It is temporary and bounded.
Session state
Session state represents the current workflow: selected IDs, pending approvals, tool results, retries and status. It should be durable when a task can outlast one request.
Long-term memory
Durable memory contains facts that are useful across sessions, such as stable preferences or project identifiers. Store only information with a clear future value and retention policy.
Retrieval
Do not inject every memory into every prompt. Search for relevant memories, rank them, validate freshness and preserve provenance. Memory retrieval is a data-access step, not an authorization step.
Safety
Memory is data, not authority. A stored instruction must not override policy. Do not silently store secrets, credentials or unnecessary personal data.
Forgetting
A complete memory system supports correction, expiration and deletion. Define what happens when a user changes a preference, requests deletion or a stored fact becomes false.
The practical architecture is simple: current context for this decision, session state for this workflow and selective durable memory for reusable facts.
Learning path
Theory → Example → Code → Practice → Quiz → Challenge → Completion
Step 1
Theory
Memory
Memory is information an agent can use beyond the immediate model output. The key design decision is to distinguish current context, session state, durable memory and external knowledge.
Context
Context is what the model sees for the current decision: recent messages, current task state, retrieved evidence and tool results. It is temporary and bounded.
Session state
Session state represents the current workflow: selected IDs, pending approvals, tool results, retries and status. It should be durable when a task can outlast one request.
Long-term memory
Durable memory contains facts that are useful across sessions, such as stable preferences or project identifiers. Store only information with a clear future value and retention policy.
Retrieval
Do not inject every memory into every prompt. Search for relevant memories, rank them, validate freshness and preserve provenance. Memory retrieval is a data-access step, not an authorization step.
Safety
Memory is data, not authority. A stored instruction must not override policy. Do not silently store secrets, credentials or unnecessary personal data.
Forgetting
A complete memory system supports correction, expiration and deletion. Define what happens when a user changes a preference, requests deletion or a stored fact becomes false.
The practical architecture is simple: current context for this decision, session state for this workflow and selective durable memory for reusable facts.
Step 2
Example
Example: coding agent
Current repository, active branch and files changed belong to session state. A preference such as “prefer TypeScript” may be durable memory. A payment credential is neither and should never become agent memory.
Step 3
Code
Selective memory retrieval
const session = await db.sessions.get(sessionId);
const memories = await memory.search({ userId, query: currentGoal, limit: 5 });
const context = { currentGoal, session, relevantMemories: memories };Keep memory lookup separate from authorization decisions.
Step 4
Practice
Practice
Classify ten pieces of information from your agent as current context, session state, durable memory, external knowledge or secret. Define retention and deletion behavior for each.
Step 5
Quiz
1. What is session state for?
2. Can memory override authorization?
Step 6
Challenge
Challenge
Design memory for a support agent that remembers preferences but never stores payment credentials. Include retrieval, expiration, correction and deletion rules.
Complete every stage
Work through every step in order, then the lesson will be marked complete.
Each chapter and subtopic has its own public URL under /ai-agent.