Memory: ConversationBufferMemory
What You'll Learn
You'll learn how to add conversation history to your LangChain chains so the LLM remembers what was said earlier in the conversation.
The Problem: Stateless LLMs
By default, LLMs have no memory between calls. Each prompt is processed in isolation, like talking to someone with amnesia who forgets everything you just said. If a user asks "What's the weather in Paris?" and follows up with "What about tomorrow?", the LLM won't know what "there" refers to.
ConversationBufferMemory: The Simplest Solution
ConversationBufferMemory is LangChain's basic memory component that stores the entire conversation history in a simple buffer (like a list). When you send a new message, it automatically includes all previous messages as context.
Think of it like a notepad where you write down everything said in a conversation. Before responding to a new question, you read the entire notepad to refresh your memory.
How It Works
The memory stores messages in a format the LLM understands (Human: ... AI: ...), and injects this history into your prompt template automatically. Each time you run the chain, it:
- Retrieves stored conversation history
- Adds it to the current prompt
- Gets the LLM's response
- Saves both the input and output back to memory
This creates continuity across multiple interactions, enabling natural back-and-forth conversations where the model can reference earlier context, answer follow-up questions, and maintain topic coherence.
When to Use It
Use ConversationBufferMemory for simple chatbots, Q&A sessions, or any scenario where you need the model to remember recent exchanges. It's perfect for short conversations but can become unwieldy with very long histories (hitting context window limits).
Key Takeaway: ConversationBufferMemory transforms stateless LLM chains into stateful conversations by automatically storing and injecting the complete message history into each prompt.