Every chain built so far forgets everything instantly. Build the real memory abstraction that fixes that.
1Chains Are Stateless By Default
Every chain you've built in this course so far is a pure function: same inputs in, same output out, with zero awareness of any previous call. That's not a bug — it mirrors the underlying LLM API itself, which has no session or memory of its own. Anything resembling 'memory' has to be built explicitly, on top, by you or a library.
2Structured Storage, Not a Raw String
Storing each message as a {role, content} dictionary rather than a flat string is a deliberate design choice. It keeps the underlying data flexible — you can format it as a plain buffer string for one kind of prompt, or as a role-tagged messages array for a chat-completions API, from the exact same stored data.
3Step-by-Step Breakdown
Every chain you've built so far forgets everything the moment .invoke() returns — call it again and it has no idea what happened last time. LangChain's ConversationBufferMemory fixes this: it stores every message and formats them into a string you inject back into the next prompt.
Build ConversationBufferMemory Yourself. Finish get_buffer_string(): loop over self.messages and build one "User: ..." or "AI: ..." line per message, joined by newlines. This exact string is what gets injected into the next prompt so the model can see the conversation so far.
Why does memory store raw messages and format them into a string on demand, instead of storing the already-formatted string directly?
- →Keeping messages as structured data (role + content) lets you format them differently for different purposes — a plain buffer string for one prompt style, a role-tagged message list for another — without losing information.
- →Because strings take up more disk space than structured data.
You have a real buffer string now. Next lesson: injecting it into an actual prompt and watching a real model answer a question it could only get right because it remembered.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Preserve Speaker Attribution When Displaying Conversation History
When rendering conversation history in a UI, always keep clear User/AI attribution on each message (as this buffer string format does), so screen reader users can follow who said what.
<p><strong>User:</strong> My name is Sam.</p>SEO Implications
- 1
Target 'LangChain ConversationBufferMemory example' as a distinct, high-intent search
This is one of the most frequently searched LangChain memory classes by developers building their first chatbot.
Best Practices
Store Conversation Data as Structured Messages, Not Pre-Formatted Strings
Structured {role, content} storage keeps your options open for different formatting needs later — a chat API's messages array, a plain buffer string, or a UI rendering — without needing to re-parse a flattened string.
Frequent Bugs
Formatting memory into a buffer string once and caching it, then adding new messages that never appear because the stale cached string is reused.
Always regenerate the buffer string fresh from the current message list on each call, rather than caching a formatted string that can go stale.
Real-World Examples
Multi-Turn Support Chat
A support chatbot uses ConversationBufferMemory to let a customer say 'my order number is 12345' in one message and 'what's its status?' three messages later, with the model correctly connecting the two because the full buffer is injected into every subsequent prompt.
prompt = f"{memory.get_buffer_string()}\n\nUser: {new_question}"