- LLMs are stateless: each API call is independent and only has access to the prompt/context you send in that call.
- To make an LLM behave as if it remembers prior turns, you must include the relevant history in the prompt.
- LangChain provides memory components that automate collecting, persisting, and retrieving conversation context.
Short-term vs Long-term memory
Short-term memory makes the model appear to remember during a single session by re-injecting prior messages into the prompt. Long-term memory persists important facts externally and retrieves only the most relevant items when composing a new prompt so the LLM can leverage past information without exceeding context limits.
How LangChain implements memory
LangChain provides several built-in memory implementations:- Conversation buffers: keep a running transcript.
- Summary memory: compress older context into a summary.
- Windowed buffers: keep only the latest N turns.
- Integrations with external stores: allow you to persist to databases or vector stores for retrieval later.

Example: short-term conversational memory with LangChain
The following shows a simple in-memory conversational setup using LangChain’sConversationBufferMemory. This keeps conversation context for the lifetime of the process/session.
Pattern for long-term memory
For long-term memory use the following pattern:- Extract and persist important snippets or facts during interactions to an external store (e.g.,
SQLite,Redis, or a vector DB). - At the start of a new session (or before answering a query), retrieve the most relevant items from that store.
- Inject these retrieved items as context (or build a retrieved context) into the prompt so the LLM can use them when generating a response.
Short-term memory exists only while a session or process runs. To retain information across sessions, persist it externally (for example
SQLite, Redis, or a vector database). Retrieve and include only the most relevant items to fit within the model’s context window.Be mindful of privacy, security, and data retention when storing user data. Persisted memory may contain sensitive information—apply appropriate encryption, access controls, and retention policies.
Design considerations
When designing memory for your application, consider:- Token/context limits: Only include the most relevant history to avoid exceeding the model’s context window. Use summarization or windowed buffers to limit tokens.
- Relevance and retrieval: Use embeddings and vector retrieval (or similarity search) to find the most useful past items to include in a prompt.
- Privacy and compliance: Avoid storing unnecessary sensitive data. Implement encryption, anonymization, and deletion policies as needed.
- Cost and latency: External retrieval adds latency and cost. Cache frequently-used retrievals and balance recall depth with performance.
Links and references
- LangChain course: https://learn.kodekloud.com/user/courses/langchain
- Mastering Generative AI with OpenAI: https://learn.kodekloud.com/user/courses/mastering-generative-ai-with-openai
- SQLite: https://www.sqlite.org/
- Redis: https://redis.io/
- Vector databases and retrieval: https://learn.kodekloud.com/user/courses/vector-database-for-genai