Agent Memory & State Persistence
Memory is what transforms a stateless LLM into a persistent, context-aware agent. Without memory, every conversation starts from scratch. With memory, agents learn about users, retain important facts, and maintain continuity across sessions.
Memory Types
- Short-Term (Working) Memory: The current conversation's message history. It lives in the context window and is lost when the session ends unless explicitly persisted.
- Long-Term Memory: Persistent storage that survives across sessions. Typically backed by a vector database or structured storage. Includes facts about the user, important decisions, and learned preferences.
- Episodic Memory: Records of specific past interactions โ what happened, when, and what the outcome was. Useful for "remember when we discussed X?" queries.
- Semantic Memory: General knowledge and facts extracted from conversations and documents. Stored as embeddings for efficient retrieval.
Memory Architecture
User Message
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Memory Retrieval โ โ Query long-term store
โ (Vector Search) โ for relevant context
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Context Assembly โ โ Combine with session
โ (Prompt Building) โ history and system prompt
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LLM Inference โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Memory Storage โ โ Extract and persist
โ (Post-Processing) โ important new info
โโโโโโโโโโโโโโโโโโโโโโโโโโState Persistence Patterns
- Conversation Logs: Store the full message history as structured data (JSON or Markdown files)
- Memory Extraction: After each turn, extract key facts and decisions from the conversation and store them separately from raw history
- Embedding Updates: Convert new information into vector embeddings and index them for future retrieval
- Checkpoint & Resume: Save agent state at key points so it can resume interrupted tasks
The Heartbeat Daemon
Advanced agents include a heartbeat daemon โ a background process that wakes the agent on a schedule to perform proactive tasks without user prompting:
- Checking for new emails or notifications
- Summarizing daily calendars
- Running scheduled data collection or analysis
- Triggering reminders based on stored context
This transforms agents from reactive assistants into proactive collaborators that take initiative based on their memory and scheduled routines.
Memory Best Practices
- Be Selective: Not everything is worth remembering. Implement relevance filters.
- Decay & Forget: Old, unused memories should lose priority over time to prevent context pollution.
- User Control: Let users view, edit, and delete what the agent remembers about them.
- Privacy First: Encrypt sensitive memory. Implement data retention policies.