AI

    Memory Systems for AI Agents: Short-term vs Long-term

    Memory is what separates sophisticated AI agents from simple chatbots. This guide covers memory architectures that enable agents to learn, remember, and improve over time.

    VE
    9 min read
    Share

    Key Takeaways

    • Three memory layers: short-term (conversation context), working (intermediate results during tasks), and long-term (persistent knowledge across sessions).
    • Summary memory is the most practical buffer strategy: older messages are summarized by the LLM, maintaining context while staying within token limits.
    • Vector databases enable semantic long-term memory: store embeddings of past interactions and retrieve relevant memories when similar situations arise.
    • Memory management is essential: without decay, consolidation, and pruning, memory stores become noisy and degrade retrieval quality.
    • Episodic memory (past experiences) enables agents to learn from mistakes and repeat successful strategies over time.

    Memory Architecture: Three Layers

    LayerScopeStorageRetentionPurpose
    Short-termCurrent sessionLLM context windowSession onlyMaintain conversation flow
    WorkingCurrent taskStructured JSON/stateTask durationTrack intermediate results
    Long-termCross-sessionVector DB + metadataPersistentLearn and personalize

    Each layer serves a distinct purpose and requires different implementation. Short-term memory is managed by the LLM's context window. Working memory is structured state. Long-term memory requires vector databases and retrieval logic.

    Conversation Buffer Strategies

    StrategyHow It WorksProsConsBest For
    Full historyInclude all messagesComplete contextToken limit exceeded quicklyShort conversations (<20 turns)
    Sliding windowLast N messages onlyBounded token costLoses early contextHigh-volume simple interactions
    Summary memoryLLM summarizes older messagesBalanced context/costSummary loses detailMost production agents
    Token bufferTrim to fit token limitPredictable costArbitrary cutoff pointCost-constrained applications
    Entity memoryTrack entities mentionedStructured recallComplex implementationCRM-like applications
    Recommendation

    Summary memory is the best default. Keep the last 5-10 messages verbatim and summarize everything older. This preserves recent context while maintaining awareness of the full conversation. The summary step adds ~$0.001-0.005 per summarization.

    Long-Term Memory with Vector Databases

    Long-term memory enables agents to learn from past interactions and provide personalized experiences:

    1. After each significant interaction, extract key information: facts learned, decisions made, user preferences, outcomes
    2. Generate embeddings of the extracted information using an embedding model (OpenAI, Cohere Embed)
    3. Store in a vector database (Pinecone, Weaviate, Chroma, Qdrant) with metadata (timestamp, context, confidence)
    4. At the start of each new interaction, retrieve semantically similar memories and inject them into the agent's context
    5. Implement memory management: decay older memories, consolidate related ones, prune outdated information
    Semantic vs Episodic

    Semantic memory stores facts and knowledge ('User prefers email communication'). Episodic memory stores experiences ('Last time we recommended product X, the user was dissatisfied'). Both types improve agent intelligence, but episodic memory enables learning from mistakes.

    Memory Management and Maintenance

    • Decay: reduce the priority/weight of older memories over time. Recent interactions are more relevant than year-old ones.
    • Consolidation: merge related memories into unified entries. Five separate mentions of a user preference become one strong memory.
    • Pruning: remove outdated, contradicted, or low-confidence memories. Stale information is worse than no information.
    • Relevance scoring: combine semantic similarity with recency, frequency, and importance when retrieving memories.
    • Privacy compliance: implement memory deletion for user data removal requests (GDPR right to be forgotten).

    Frequently Asked Questions

    Need Help Implementing This?

    Our team can help you put these insights into practice.

    Schedule a Consultation