LangChain/Memory & Middleware
★ OverviewIntermediate14 min

Conversation Memory

RunnableWithMessageHistory wraps any LCEL chain with per-session conversation history — but before you use it, you need to know when LangGraph is the better choice, what unbounded history costs you per request, and how to defend against the two failure modes that kill most production chatbots.

Quick Reference

  • Use LangGraph checkpointers for new projects — RWMH is best for existing LCEL chains
  • RunnableWithMessageHistory wraps your chain and manages history automatically per session_id
  • BaseChatMessageHistory is the interface — InMemoryChatMessageHistory for dev, Redis/SQL for prod
  • History is injected via MessagesPlaceholder in your prompt
  • At turn 20 (~250 tok/turn), you're sending ~5k extra tokens per request — always trim
  • trim_messages() keeps a sliding window; a summary chain compresses old turns
  • Old classes (ConversationBufferMemory, ConversationChain) are fully deprecated since 0.3

Should You Use RunnableWithMessageHistory?

Need conversationmemory?NoStatelessYesUsing LangGraphor new project?YesLangGraphcheckpointerNo / LCEL chainLong conversationsor token budget?NoRWMHsimple historyYesRWMH + trim_messages / summary

Start with LangGraph if building new — use RWMH for existing LCEL chains

RunnableWithMessageHistory (RWMH) is not the recommended approach for new projects as of LangChain 0.3. LangChain's own documentation now recommends LangGraph checkpointers for anything that needs persistence, state management, or complex memory patterns. RWMH is still fully supported and not deprecated — it's the right tool when you already have an LCEL chain and want to add session memory without rewriting in LangGraph.

SituationUse
New project, or already using LangGraphLangGraph checkpointer (MemorySaver for dev, PostgresSaver for prod)
Existing LCEL chain, need per-session historyRunnableWithMessageHistory
Single-turn chain, no session neededNeither — keep it stateless
Long-running agent with tool calls and branchesLangGraph — RWMH doesn't support branching state
RWMH doesn't support branching or tool state

RunnableWithMessageHistory only stores a flat message list. If your agent makes tool calls, forks, or needs to roll back state, RWMH will lose that context. LangGraph's checkpointer stores the full graph state including pending tool calls and intermediate results.