Agents that handle a single request are trivial. Real agents handle multi-turn conversations: user asks question 1, agent responds, user follows up with question 2 based on the previous answer. Memory systems maintain conversation history and enable agents to reference past interactions.
Conversation Buffer Memory
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
# First turn memory.save_context( {"input": "What is 2+2?"}, {"output": "2+2 equals 4"} )
# Second turn - agent can see history memory.load_memory_variables({}) # Output: {"history": "Human: What is 2+2?\nAI: 2+2 equals 4"}
Summarization Memory
For long conversations, buffer memory grows unbounded. Summarization compresses history.
from langchain.memory import ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm)
# After many turns, conversation is summarized summary = memory.buffer # Compressed conversation summary
Vector Memory
Store important facts in a vector store for long-term recall.
from langchain.memory import VectorStoreBackedMemory
memory = VectorStoreBackedMemory( vectorstore=vector_store, k=5 # Retrieve top 5 relevant memories )
Conclusion
Memory systems enable agents to handle multi-turn conversations and maintain long-term context. Buffer memory is simple but unbounded; summarization compresses; vector memory enables retrieval. Choosing the right memory strategy depends on conversation length and user expectations. Next: error handling and recovery in agents.
