Module 8 Lesson 2: ConversationBufferMemory
The Raw Transcript. Using the simplest memory type to keep a literal record of every message in a conversation.
ConversationBufferMemory: The Literal Record
ConversationBufferMemory is the simplest and most common memory type. It keeps a literal "Transcript" of the conversation. Every time a message is sent, it's appended to a list. Every time a prompt is made, that list is injected.
1. How it Works
from langchain.memory import ConversationBufferMemory
# 1. Initialize
memory = ConversationBufferMemory()
# 2. Add some context
memory.save_context({"input": "Hi!"}, {"output": "How can I help you?"})
memory.save_context({"input": "My name is Sudeep"}, {"output": "Nice to meet you, Sudeep!"})
# 3. View the state
print(memory.load_memory_variables({}))
# Output: {'history': 'Human: Hi!\nAI: How can I help you?\nHuman: My name is Sudeep\nAI: Nice to meet you, Sudeep!'}
2. Using it in a Chain
In an LCEL chain, you don't call save_context manually. You use a specialized Runnable that handles the reading and writing for you.
from langchain.chains import ConversationChain
# Legacy wrapper for easy memory testing
chat = ConversationChain(llm=model, memory=memory)
chat.predict(input="What is my name?")
3. Visualizing the Memory Stack
graph TD
M1[Human: Hi] --> DB[(Memory Buffer)]
A1[AI: Hello] --> DB
M2[Human: My name is X] --> DB
DB --> Prompt[System Instructions + Buffer + New Question]
Prompt --> LLM[Chat Model]
4. Why use a Buffer?
- Pros: 100% accurate. The model sees exactly what was said.
- Cons: It grows forever. After 100 messages, your prompt will be huge, expensive, and slow.
5. Engineering Tip: return_messages=True
When using Chat Models (Module 2), you should always set return_messages=True. This tells the memory store to keep the data as a list of Objects rather than one giant string. This makes it much easier for the Chat Model to "Identify" who said what.
Key Takeaways
- Buffer Memory is a raw transcript of the conversation.
- It is the most accurate way to maintain context.
- Token growth is the primary drawback of this method.
save_contextandload_memory_variablesare the two core operations.