tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > LangChain > Conversation Memory in LangChain

Conversation Memory in LangChain

Author: Venkata Sudhakar

By default, each call to an LLM through LangChain is stateless - the model has no memory of previous exchanges. Building a chatbot or conversational AI assistant requires maintaining conversation history and passing it to the LLM on each turn so it has the context of what was said before. LangChain provides several memory patterns ranging from simple in-memory message buffers to persistent storage backends that survive application restarts. Choosing the right memory strategy depends on conversation length, cost constraints, and whether the history needs to persist across sessions.

The modern LangChain approach to memory uses the RunnableWithMessageHistory wrapper, which integrates cleanly with LCEL chains. You provide a function that retrieves (or creates) a message history object for a given session_id, and LangChain automatically loads the history before calling the LLM and saves the new messages after. For development and single-server deployments, InMemoryChatMessageHistory stores history in a Python dict. For production multi-server deployments, use a persistent backend like RedisChatMessageHistory or a custom database-backed store.

The below example shows three memory patterns: simple in-memory history for development, windowed history to limit token usage, and a summary memory pattern that compresses old history into a running summary.


It gives the following output,

Turn 1: CDC (Change Data Capture) tracks and captures database changes in real time
by reading the transaction log. Debezium is an open-source CDC tool that reads
database logs and publishes change events to Apache Kafka topics.

Turn 2: Debezium supports MySQL, PostgreSQL, Oracle, SQL Server, MongoDB, Db2,
and several others through a Kafka Connect plugin architecture.

Turn 3: We discussed that CDC captures database changes via transaction logs,
and Debezium is the leading open-source CDC tool supporting multiple databases
including MySQL, PostgreSQL, Oracle, and SQL Server, publishing events to Kafka.

It gives the following output,

# Pattern 2 - windowed memory:
# After 4 turns (8 messages), only the last 6 messages are kept.
# The model naturally forgets the very first exchanges.
# This prevents exceeding the context window on long conversations.

# Pattern 3 - file-based persistence:
No history found for user-venkata-001 - starting fresh
Saved 6 messages for session user-venkata-001

# Next application restart:
Loaded 6 messages for session user-venkata-001
# Conversation resumes exactly where it left off

Memory strategy selection guide:

InMemoryChatMessageHistory - Best for: development, single-server apps, short-lived sessions. Limitation: lost on restart, not scalable across multiple server instances.

Windowed memory - Best for: long conversations where only recent context matters. Keep the last 6-10 messages (3-5 turns). Each message in history costs tokens on every subsequent call, so windowing is essential for cost control in production.

RedisChatMessageHistory - Best for: production multi-server deployments. Install langchain-community and use RedisChatMessageHistory(session_id, url="redis://..."). History persists across restarts and is shared across all server instances behind a load balancer.


 
  


  
bl  br