Skip to main content
In this lesson we’ll demonstrate how to add long-term memory to large language model (LLM) applications. Short-term “memory” is typically kept in-process as a sequence of messages and passed into a prompt using a MessagesPlaceholder. The example below shows an in-memory conversation history that is injected into the prompt on each invocation:
This returns a single response that used the provided history (short-term, in-memory). Short-term memory is fast but volatile — it lives in process and disappears when the process stops. Persisting conversation history across processes and over time (long-term memory) requires externalizing the message history to a datastore such as Redis, SQLite, or MySQL. LangChain provides a Redis-backed chat message history implementation plus a runnable wrapper that automatically reads and writes history when executing the chain. Below is an example integrating RedisChatMessageHistory with RunnableWithMessageHistory:
Make sure your Redis instance is reachable at REDIS_URL. In lab environments the URL may differ. The session_id is used as the key for the message history (for example: math-thread1).
Invoke the redis_chain and pass a configurable session_id so each session/thread maps to its own persisted history. The config parameter is passed as the second argument to invoke:
Example responses (illustrative):
This demonstrates using a persistent store to maintain multiple independent conversation threads. Each session_id corresponds to an independent history that the runnable wrapper reads and writes to populate the prompt for subsequent invocations. You can inspect the persisted data directly in Redis. For example, if you run Redis in Docker, use the Redis CLI to list keys and view stored lists:
List entries for a session with LRANGE:
Likewise for the physics thread:
The image shows a Jupyter Notebook interface displaying JSON data output, likely from a code cell execution.
All conversation history is persisted outside your application. You can use Redis, SQLite, MySQL, or any other persistent datastore. The pattern remains the same: replace RedisChatMessageHistory with the appropriate history class for your chosen store, and provide a function that returns the message history for a given session_id. Quick recap:
Be careful about storing sensitive or personally identifiable information (PII) in persistent conversation history. Persisted messages may be retained long-term and could be accessible by other systems or team members. Consider encryption, redaction, and retention policies.
Best practices and tips:
  • Use descriptive and unique session_id values (for example: user-1234-chat, tenantA-session-01) so each conversation maps to the correct history.
  • If you switch datastores, implement or use the corresponding *ChatMessageHistory class for your storage backend.
  • Monitor token usage and costs in persisted responses (response_metadata) to manage budget and optimization.
  • For large knowledge or documents, combine persistent chat history with retrieval-augmented generation (RAG) to provide the model with external knowledge at runtime.
Retrieval-augmented generation (RAG) and retrieval systems are important for combining external knowledge stores with LLMs, allowing your assistant to reference large documents or knowledge bases without inflating prompt size. Links and references:

Watch Video

Practice Lab