Memory and Context / Memory Types

Persistent Memory

Intermediate [3/5]
Long-term memory Cross-session memory User memory

Definition

Persistent memory refers to information stored outside the immediate context window that survives across sessions. Unlike working memory that clears with each conversation, persistent memory allows the model to recall user preferences, past interactions, and learned facts over time.

This is typically implemented through external databases, embeddings, or explicit memory storage systems.

Key Concepts

  • Cross-session retention: Information persists between conversations
  • External storage: Database, vector store, or file system
  • Retrieval mechanism: How memories are recalled when relevant
  • Memory updates: Adding, modifying, or forgetting information

Examples

Architecture
Persistent Memory System
Session 1 (Monday): User: "I prefer Python over JavaScript" System: [Stores preference in memory DB] Session 2 (Wednesday): User: "What language should I use for this script?" System: [Retrieves: "User prefers Python"] Assistant: "Given your preference for Python, I'd suggest..." Memory Database Structure: ┌─────────────────────────────────────────┐ │ user_id: "alice_123" │ │ memories: [ │ │ { │ │ type: "preference", │ │ content: "Prefers Python over JS", │ │ timestamp: "2024-01-15", │ │ confidence: 0.95 │ │ }, │ │ { │ │ type: "fact", │ │ content: "Works at TechCorp", │ │ timestamp: "2024-01-10", │ │ confidence: 0.90 │ │ } │ │ ] │ └─────────────────────────────────────────┘
Implementation
Memory Management
class PersistentMemory: def __init__(self, user_id, vector_db): self.user_id = user_id self.db = vector_db def store(self, content, memory_type="general"): """Save a new memory""" embedding = embed(content) self.db.insert({ "user_id": self.user_id, "content": content, "type": memory_type, "embedding": embedding, "timestamp": now() }) def recall(self, query, k=5): """Retrieve relevant memories""" query_embedding = embed(query) return self.db.search( query_embedding, filter={"user_id": self.user_id}, limit=k ) def inject_into_prompt(self, query, system_prompt): """Add memories to working memory""" memories = self.recall(query) memory_context = "\n".join([m.content for m in memories]) return f"{system_prompt}\n\nUser context:\n{memory_context}"

Interactive Exercise

Design Memory Categories

For a personal assistant, what categories of information should be stored in persistent memory?

Pro Tips
  • Distinguish facts (stable) from preferences (may change)
  • Include timestamps for temporal relevance
  • Implement confidence scores for uncertain memories
  • Allow users to view and delete their stored memories

Related Terms