What is Memory Module (LangChain)?
A memory module in LangChain is a component that manages state and context across multiple interactions with an agent or chain. Memory modules implement a standardized interface for storing information from conversations or executions and retrieving relevant context when needed. They abstract the complexity of state management, providing simple methods for adding new information and accessing stored context that can be incorporated into prompts.
LangChain provides several memory implementations for different use cases: ConversationBufferMemory stores complete conversation history, ConversationSummaryMemory maintains compressed summaries, ConversationTokenBufferMemory enforces token limits, VectorStoreMemory uses semantic search for retrieval, and EntityMemory tracks information about specific entities. Each implementation follows a common interface, making it easy to swap memory types or experiment with different approaches without changing other parts of the application.
Memory modules integrate with LangChain's chains and agents through input/output processing. When a chain executes, the memory module retrieves relevant context based on the current input and adds it to the prompt. After execution, the memory saves the new interaction for future reference. This pattern enables stateful conversations, learning from past interactions, and maintaining context across multi-turn interactions. Memory modules are a key component in building conversational agents and applications that need continuity across sessions.