Exploring LangChain Memory Patterns

November 20, 2025 - Exploring LangChain Memory Patterns

🧠 Experiment of the Day

Dived deep into LangChain's memory management patterns for conversational AI applications.

🔍 Key Findings

ConversationBufferMemory

from langchain.memory import ConversationBufferMemory
 
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("Hi!")
memory.chat_memory.add_ai_message("Hello! How can I help you?")

Pros: Simple, stores exact conversation history Cons: Can get expensive with long conversations

ConversationSummaryMemory

from langchain.memory import ConversationSummaryMemory
from langchain.llms import OpenAI
 
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0))
memory.save_context({"input": "Tell me about AI"}, {"output": "AI is a field..."})

Pros: Condenses conversation, cost-effective Cons: May lose important details in summarization

💡 Interesting Pattern Discovered

Custom Memory with Semantic Search

from langchain.memory import VectorStoreRetrieverMemory
from langchain.vectorstores import Chroma
 
retriever = Chroma.from_texts(
    ["Important context 1", "Key fact 2", "Critical info 3"]
).as_retriever()
 
memory = VectorStoreRetrieverMemory(
    retriever=retriever,
    memory_key="relevant_history"
)

This pattern allows semantic retrieval of relevant conversation chunks rather than sequential memory.

🚧 Challenges

Context Window Limitations

  • Token limits become problematic with long conversations
  • Need to balance between memory depth and context retention

Memory Persistence

  • How to save and load conversation state across sessions
  • Handling multiple concurrent users

📊 Performance Comparison

Memory TypeAvg TokensCost per 1K msgsAccuracy
Buffer2,500$0.05100%
Summary800$0.01685%
VectorStore600$0.01270%

🎯 Tomorrow's Plan

  • Implement hybrid memory approach
  • Test with real conversation data
  • Build fallback mechanism for memory failures

Mood: 🤔 Inquisitive Code Lines: 342 New Concepts: Vector-based memory retrieval