Unlocking Context: Why Memory Is Crucial in LangChain and How to Build Custom Memory

LangChain’s stateless LLMs require a memory component to retain conversation context, and this article explains the importance of memory, compares built‑in memory types, and walks through two practical examples—custom buffer memory and basic message history—showing how to implement and use them with code.

BirdNest Tech Talk
BirdNest Tech Talk
BirdNest Tech Talk
Unlocking Context: Why Memory Is Crucial in LangChain and How to Build Custom Memory

Why Memory Matters

Large language models (LLMs) are stateless: each request is processed without knowledge of prior interactions. Single‑turn Q&A tolerates this, but multi‑turn chatbots, agents, or any context‑aware application require persisted state. LangChain’s Memory component solves the problem by storing past exchanges so that subsequent turns can reference earlier user inputs, personalize responses, avoid repeating actions, and reflect on past successes or failures.

How Memory Works in LangChain

Memory implementations manage a chat_history variable that holds a list of HumanMessage and AIMessage objects. When a chain or agent runs, the memory injects chat_history into the prompt, giving the LLM the required context.

Common Memory Types

ChatMessageHistory (Base Message History)

Features : stores raw HumanMessage and AIMessage objects in a list.

Pros : flexible, direct, easy to understand.

Cons : no built‑in length management or summarization; user must handle those concerns.

ConversationBufferMemory (Buffer Memory) – legacy/recommended

Features : keeps the entire raw conversation in a buffer.

Pros : simple, retains all details.

Cons : history can grow beyond the LLM’s context window, increasing token cost.

ConversationBufferWindowMemory (Sliding‑Window Memory) – legacy/recommended

Features : retains only the most recent k turns; older turns are dropped.

Pros : controls context length, avoids exceeding limits.

Cons : loses older context.

ConversationSummaryMemory (Summary Memory)

Features : uses an LLM to generate a running summary instead of storing raw messages.

Pros : dramatically reduces context length for very long dialogs.

Cons : consumes LLM tokens for summarization and may omit fine details.

ConversationSummaryBufferMemory (Summary‑Buffer Hybrid)

Features : combines a sliding window of raw messages ( k recent turns) with a summary of earlier turns.

Pros : balances detail retention and length control.

ConversationKGMemory (Knowledge‑Graph Memory)

Features : extracts entities and relations from the dialog via an LLM, builds a knowledge graph, and queries it for context.

Pros : remembers factual information even in long conversations.

Cons : higher complexity, extra LLM calls for graph construction and querying.

VectorStoreRetrieverMemory (Vector‑Store Retrieval Memory)

Features : embeds each turn and stores embeddings in a vector store; retrieves the most relevant past turns based on the current input.

Pros : suitable for very long dialogs, returns the most pertinent history.

Cons : requires an embedding model and a vector database.

Example 1: Custom Buffer Memory ( example_1_buffer_memory.py )

This example builds a bespoke buffer memory called SimpleBufferMemory on top of ChatMessageHistory, reproducing the core behavior of ConversationBufferMemory.

Core Concepts

SimpleBufferMemory class : wraps a ChatMessageHistory instance to store each session’s messages.

get_session_history : given a session_id, retrieves or creates the corresponding ChatMessageHistory.

add_conversation : mimics ConversationBufferMemory.save_context by appending a HumanMessage and an AIMessage to the history.

get_messages : returns the full list of messages for a session.

get_conversation_string : formats the message list into a readable string, similar to the legacy ConversationBufferMemory.buffer attribute.

Example Highlights

Modular implementation : shows how ChatMessageHistory can serve as a building block for more sophisticated memory logic.

Clear workflow : adds a turn, retrieves history, and renders it as text.

Adaptation to new architecture : achieves the same functionality without directly depending on the deprecated ConversationBufferMemory class.

Example 2: Basic Message History ( example_2_message_history.py )

This example demonstrates the most straightforward use of ChatMessageHistory, the core component for managing chat messages in LangChain.

Core Concepts

Instantiating ChatMessageHistory : creates an object that holds a single session’s messages.

add_user_message : appends a HumanMessage to the history.

add_ai_message : appends an AIMessage to the history.

history.messages : directly accesses the internal list of HumanMessage and AIMessage objects.

Example Highlights

Simplicity : demonstrates the minimal API needed to record a conversation.

Message types : emphasizes the role of HumanMessage and AIMessage in constructing a dialog.

Foundational building block : clarifies that ChatMessageHistory underpins all higher‑level memory classes.

References

https://python.langchain.com/docs/how_to/memory

https://python.langchain.com/docs/how_to/manage_large_chat_history

PythonLLMLangChainMemoryChatbotconversation
BirdNest Tech Talk
Written by

BirdNest Tech Talk

Author of the rpcx microservice framework, original book author, and chair of Baidu's Go CMC committee.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.