Conversation Memory in LangChain
Conversation memory stores selected history and supplies it to later requests. It does not alter the model's weights. Re-sending the whole transcript every turn can make a long session expensive and bury the current task.
#Choose what to retain
| Strategy | Benefit | Cost or risk |
|---|---|---|
| Full history | Original wording is preserved | Growing context and cost |
| Recent window | Predictable size | Earlier constraints can disappear |
| Token budget | Bounds request size | Needs careful message trimming |
| Summary plus recent turns | Retains continuity compactly | Summaries can omit or distort facts |
| Retrieval from stored records | Selects relevant past information | Retrieval and permission errors |
Keep durable preferences and permissions as structured application data rather than relying on a lossy summary to remember them.
#Match the API to the project version
Earlier tutorials use ConversationBufferMemory or RunnableWithMessageHistory. Current agent designs commonly use graph state and a checkpointer. Do not mix a legacy memory class into a newer example without checking the version's documentation.
For a runnable with message history, ensure the history placeholder and configured input/history keys agree. An empty history should be an empty supported message-history object, not None.
#Isolate conversations
Derive access from the authenticated user and tenant on the server. A random session or thread identifier helps name a conversation; it is not authorization. Check ownership before every read and write. Two browser tabs or users must not share one global history accidentally.
An in-memory dictionary or checkpointer loses data when the process restarts. Persistent storage needs retention, deletion, encryption and concurrency handling. A checkpointer can resume state, but an external write may have already happened before a crash; use idempotency for actions.
#Verify the behavior
Test a long conversation, an important early constraint, refresh/restart, two users with similar requests, and deletion. Compare retained facts against the original transcript. Report what the system forgot rather than claiming unlimited memory.