← Back to tutorials

AI Agent Memory Systems: Short-Term and Long-Term Memory

Build AI agents that remember and learn from interactions

AI Agent Memory Systems

Why Memory Matters for AI Agents

Without memory, AI agents are stateless - every conversation starts fresh. Memory enables:
  • Personalization based on past interactions
  • Learning from mistakes
  • Maintaining context across sessions
  • Building relationships over time
  • Types of Memory

    1. Working Memory (In-Context)

    Short-term memory stored directly in the conversation context:
    python
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "My name is Alice"},
        {"role": "assistant", "content": "Nice to meet you, Alice!"},
        {"role": "user", "content": "What's my name?"},  # Agent can answer from context
    ]
    

    2. Episodic Memory

    Records of past interactions stored in a database:
    python
    class EpisodicMemory:
        def __init__(self, db):
            self.db = db
        
        def store(self, user_id, interaction):
            self.db.insert({
                "user_id": user_id,
                "timestamp": datetime.now(),
                "summary": interaction.summary,
                "key_facts": interaction.extract_facts()
            })
        
        def retrieve(self, user_id, query, k=5):
            return self.db.semantic_search(
                query=query,
                filter={"user_id": user_id},
                limit=k
            )
    

    3. Semantic Memory (Knowledge Base)

    Factual knowledge stored as embeddings:
    python
    from langchain.vectorstores import Chroma
    from langchain.embeddings import OpenAIEmbeddings

    semantic_memory = Chroma( embedding_function=OpenAIEmbeddings() )

    Store facts

    semantic_memory.add_texts([ "Alice prefers Python over JavaScript", "Alice works at TechCorp as a data scientist", "Alice is interested in machine learning" ])

    Retrieve relevant context

    results = semantic_memory.similarity_search( "What programming language does the user prefer?" )

    Memory Management

    As memories accumulate, you need strategies to:
  • Summarize old conversations
  • Prioritize important memories
  • Forget irrelevant information
  • Handle contradictions
  • Privacy Considerations

    Always implement memory with user consent and provide options to view/delete stored memories.

    Also available in 中文.

    AI Agent Memory Systems: Short-Term and Long-Term Memory | AI Skill Navigation | AI Skill Navigation