Why did LangChain replace `ConversationBufferMemory` with `RunnableWithMessageHistory`?
Old `Memory` held state on the chain object, breaking multi-tenancy; `RunnableWithMessageHistory` parameterises history by `session_id` and keeps the chain stateless.
Picture a customer-service phone line where every caller hears the previous caller's last sentence whispered into their ear because the operator never resets her notepad. That's the old `ConversationBufferMemory`, one shared notepad on the chain object. The new pattern hands every caller their own labeled folder; the operator pulls the right folder for the right caller, reads from it, writes to it, and puts it back. The phone line itself stays clean, each caller's history lives in their own folder addressed by a session id.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
State the structural mismatch (stateful Memory vs stateless Runnable), describe the multi-tenant breakage concretely, walk the `RunnableWithMessageHistory` contract (session_id config, history factory, store backends), list production wins (horizontal scaling, pluggable persistence), and finish with the LangGraph checkpointer connection.
Real products, models, and research that use this idea.
- Production LangChain apps using `RedisChatMessageHistory` keyed by user-session id, with TTL configured at the Redis layer for automatic cleanup.
- LangChain's own migration docs show the before/after pattern explicitly, deprecating `ConversationBufferMemory` and showing `RunnableWithMessageHistory` as the replacement.
- LangGraph's `checkpointer` (Postgres/Redis backed, keyed by `thread_id`) is the analogous pattern for stateful agent graphs, same architectural principle at a different layer.
- FastAPI-backed LangChain services typically inject the chain as a module-level singleton and pass `session_id` from the request headers, the chain serves many tenants without isolation contortions.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you implement `ConversationSummaryMemory`'s semantics on top of `RunnableWithMessageHistory`?
QWalk through a multi-tenant production setup with `RunnableWithMessageHistory` and Redis.
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Sharing a chain across users when the chain has a stateful Memory attached, two users now silently share each other's conversation history.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.