#ConversationBufferMemory
Persistent memory for a LangChain chatbot after LangChain 1.0
_Disclosure: I'm Mohammed, founder of MemorySync. I drafted this article with AI assistance and checked it against the official MemorySync and LangChain docs._ If you learned LangChain memory through `ConversationBufferMemory`, the ground has moved. In LangChain 1.x that class lives in `langchain-classic`, is marked deprecated, and is slated for removal in 2.0.0 (LangChain reference, v1 migration guide). What survived is chat message history. `RunnableWithMessageHistory` wraps any runnable and asks a `BaseChatMessageHistory` for the messages of a session. LangChain's own reference shows it with `InMemoryChatMessageHistory`, a Python dict keyed by session ID (ConversationChain reference). That works until the process restarts, and then the conversation is gone. This tutorial swaps that dict for a persistent history and adds a second kind of memory: what the bot knows about the user across every conversation, not just this one. ## Two kinds of memory, kept apart It helps to name them before writing code: 1. **The transcript.** The ordered messages of one thread. You need it so the model can follow "yes, do that" three turns later. 2. **Long-term facts about the user.** "Vegetarian", "prefers short answers", "planning a trip to Lisbon". You need these in a new thread next week, without replaying old transcripts. The MemorySync LangChain guide keeps these separate on purpose. The transcript is stored per session exactly as written. It is not itself part of the user's memories. Instead, durable facts are extracted from the user's messages and stored as memories, and those are what recall returns. ## Install pip install langchain-memorysync Per the guide, the package supports langchain-core 0.3.x and 1.x on Python 3.10+. There is also a Node.js package, `memorysync-langchain`, for Node 18+. Set `MEMORYSYNC_API_KEY` in your environment (or pass the key explicitly). Keys come from the dashboard or `npx memorysync-cli init`. Keep it in a backend, script or worker where end users can't see it. ## Step 1: a transcript that survives restarts `MemorySyncChatMessageHistory` implements `BaseChatMessageHistory`, so it drops into `RunnableWithMessageHistory` where the in-memory history used to be: from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_memorysync import MemorySyncChatMessageHistory chain_with_history = RunnableWithMessageHistory( chain, # any runnable, e.g. prompt | llm lambda session_id: MemorySyncChatMessageHistory( session_id=session_id, user_id="user-123", ), input_messages_key="input", history_messages_key="history", ) chain_with_history.invoke( {"input": "My name is Ada."}, config={"configurable": {"session_id": "thread-42"}}, ) Four parameters matter: * `session_id` is the conversation thread. Reads and `clear()` only touch that thread. * `user_id` is the end user the turns belong to. It defaults to the session ID, so pass a stable user ID if you want memory shared across that user's sessions. Take it from your own authenticated user record, not from something the client sends. * `max_messages` caps how many trailing messages a read returns, so a long thread doesn't blow your prompt budget. Writes aren't affected. * `read_only` blocks writes and `clear()`, which is handy for inspecting a transcript. Two behaviours from the guide are worth knowing before production. Each exchange is written when the chain finishes the turn, so a restarted process picks up where the thread left off. And `add_message` raises on failure instead of silently dropping the turn, while a retried write is recognised server-side rather than duplicated. Tool calls inside LangChain messages are stored with the turn and rebuilt on read. ## Step 2: long-term memory in the system prompt The transcript covers one thread. For "what do we know about this person", use the context provider. It returns a grouped, prompt-ready block, or an empty string when the user has no relevant memories yet: from langchain_memorysync import MemorySyncContextProvider provider = MemorySyncContextProvider(user_id="user-123") context = provider.get_context("What should I cook tonight?") The empty string is deliberate. A brand-new user is a normal state, so handle it with a fallback rather than an error. ## Putting it together Here is the complete loop, adapted from the guide: per-session transcripts through the history class, cross-session facts through the provider, and a model in the middle. from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_openai import ChatOpenAI from langchain_memorysync import ( MemorySyncChatMessageHistory, MemorySyncContextProvider, ) USER_ID = "customer-7" # your stable, authenticated user ID llm = ChatOpenAI(model="your-model") provider = MemorySyncContextProvider(user_id=USER_ID) prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful travel assistant.\n" "Relevant memories about this user:\n{memories}"), MessagesPlaceholder(variable_name="history"), ("human", "{input}"), ]) chat = RunnableWithMessageHistory( prompt | llm, lambda session_id: MemorySyncChatMessageHistory( session_id=session_id, user_id=USER_ID, max_messages=30, ), input_messages_key="input", history_messages_key="history", ) def chat_turn(text: str, session_id: str) -> str: memories = provider.get_context(text) or "None yet." reply = chat.invoke( {"input": text, "memories": memories}, config={"configurable": {"session_id": session_id}}, ) return reply.content Stop the process and start it again with the same `session_id`, and the thread continues. Start a new `session_id` for the same user, and the transcript is fresh but the system prompt still carries what was learned before. One habit I'd keep regardless of which memory store you use: the memory block is background, not instructions. MemorySync's Python quickstart says to treat returned memory text as untrusted context when passing it to a model, and the label in the system prompt above is there for that reason. ## Optional: let an agent manage memory itself If you're building an agent rather than a chatbot, the LangChain Tools guide adds five structured tools: `add_memory`, `search_memory`, `list_memories`, `update_memory` and `delete_memory`. from langchain.agents import create_agent from langchain_memorysync import create_memorysync_tools tools = create_memorysync_tools(end_user_id="user-123") agent = create_agent(model, tools=tools) A few design choices from that guide: * Tools never raise. Failures, including quota and validation errors, come back as short strings the model can act on, because an exception inside a tool would abort the whole agent run. * `add_memory` derives an idempotency key from the content, so repeating "remember X" returns "already stored" instead of a duplicate. * Memory text can't be edited. `update_memory` changes tags and importance, and replacing a fact is delete plus add. * `end_user_id` scopes every call to that user. Pass `read_only=True` if the agent should only search and list. The guide notes the same tool list also works with LangGraph's `create_react_agent` and the legacy `AgentExecutor` on 0.3.x. ## What to store, briefly Whatever you build, the transcript is the easy part. The harder question is which facts deserve to outlive the thread. My rule of thumb: store things that will still be true and useful next week (preferences, constraints, decisions), and leave out one-off chatter, secrets and anything the model guessed rather than the user said. The quickstart's own list is a good floor: no passwords, tokens, hidden instructions or unreviewed model output. ## Try it If you want to see the save, reload and recall loop before writing any code, the MemorySync playground runs it in the browser with each API call shown: memorysync.io/try.
dev.to
October 21, 2026 at 4:19 PM
🧠 Day 2 – Add Memory to Your Chain
LangChain lets your app “remember” past interactions. Use ConversationBufferMemory for a chat-like experience.

👀 Output: “Your name is Xavier.”

#LangChain #Python #LLM #AI #GenerativeAI #Memory #PromptEngineering #LangChainCheatsheet
July 20, 2025 at 6:58 AM
LangChain + ODEI: Persistent World Models for Long-Running Agents
via Dev.to Python

https://flarestart.com/article/langchain-odei-persistent-world-models-for-long-running-agents-20260223
#DevNews #ProgrammingLanguages #Tutorial
LangChain + ODEI: Persistent World Models for Long-Running Agents
LangChain + ODEI: Persistent World Models ConversationBufferMemory resets on restart. ODEI gives LangChain agents a persistent world model. Quick Integration from langchain.tools import tool import...
flarestart.com
February 23, 2026 at 3:14 AM
AI that forgets conversations? 🧠

Added ConversationBufferMemory + LangChain to TinyLlama.

Now it remembers:
✅ Your name
✅ Past topics
✅ Context flow

Transforms robotic Q&A into real dialogue.

Full hands-on tutorial 👉 https://link.illustris.org/rnFWUi

#AI #LangChain #Chatbots #dougortiz
LangChain for Beginners - Add Memory to Your AI - Part 2
Is your AI experiencing memory loss? 🤯 Frustrated with disjointed conversations? This video is your ultimate guide to building conversational AI that remembers, understands context, and transforms your projects into intelligent dialogues! We're diving deep into #LangChain and #TinyLlama to give your AI the memory it deserves. 🧠 In this hands-on tutorial, you'll learn: - How to set up your #GoogleColab environment for AI development. - Installing essential libraries like #AutoTokenizer and #AutoModelForCausalLM. - Loading the powerful #TinyLlama model for efficient text generation. - The magic of #ConversationBufferMemory for giving your AI memory. - Crafting effective #PromptTemplates for seamless dialogue. - Practical examples of how your AI can remember names and hobbies! Say goodbye to forgetful AI and hello to fluid, engaging conversations. This isn't just a tutorial; it's a transformation for your #AIProjects. 🔥 Next up: We'll explore how to teach your AI to read and understand PDFs! Stay tuned for Part 3! Resources: - LangChain Memory Docs - Tiny Llama Don't forget to like, comment, and subscribe for more AI tutorials! Let me know what you build! 👇 #AIChatbots, #ChatbotTutorial, #HuggingFace, #ConversationalAI, #AIWithMemory, #BuildAIChatbot, #AIDevelopment, #PythonProgramming, #GoogleColab, #TinyLlama, #LangChain, #AutoTokenizer, #TextGeneration, #NLP, #MachineLearning, #DeepLearning, #AIProject, #StepByStepTutorial, #CodingTutorial, #AIChatbotWithMemory, #ContextualAI, #EasyAITutorial, #AIForBeginners, #PracticalAI, #AIAplications, #AITools, #HowToCreateAIChatbots, #AIChatbotDevelopment, #CreateYourOwnAIChatbot, #PythonForAI, #FreeAITutorial, #LLM, #LargeLanguageModel, #MemoryAugmentation, #ConversationBuffer, #PromptEngineering, #ModelSelection, #ModelTraining, #AIModelDeployment, #DougOrtiz #dougortiz ▬▬▬▬▬▬ 👋 Contact Me ▬▬▬▬▬▬ ➡ LinkedIn: https://www.linkedin.com/in/doug-ortiz-illustris/ ✍️ Blog: https://dougortiz.blogspot.com/ ▬▬▬▬▬▬ ⏱ Timestamps ▬▬▬▬▬▬ 00:00 Introduction: Solving AI's Memory Problem 00:23 Setting Up the Environment 00:41 Installing and Importing Libraries 01:13 Configuring the Model and Tokenizer 01:46 Building the Conversational Pipeline 02:03 Implementing Memory in AI 02:24 Testing and Verifying AI Memory 04:17 Conclusion and Next Steps
link.illustris.org
November 27, 2025 at 2:02 PM
LangChain for Beginners - Add Memory to Your AI - Part 2
Is your AI experiencing memory loss? 🤯 Frustrated with disjointed conversations? This video is your ultimate guide to building conversational AI that remembers, understands context, and transforms your projects into intelligent dialogues! We're diving deep into #LangChain and #TinyLlama to give your AI the memory it deserves. 🧠 In this hands-on tutorial, you'll learn: - How to set up your #GoogleColab environment for AI development. - Installing essential libraries like #AutoTokenizer and #AutoModelForCausalLM. - Loading the powerful #TinyLlama model for efficient text generation. - The magic of #ConversationBufferMemory for giving your AI memory. - Crafting effective #PromptTemplates for seamless dialogue. - Practical examples of how your AI can remember names and hobbies! Say goodbye to forgetful AI and hello to fluid, engaging conversations. This isn't just a tutorial; it's a transformation for your #AIProjects. 🔥 Next up: We'll explore how to teach your AI to read and understand PDFs! Stay tuned for Part 3! Resources: - LangChain Memory Docs - Tiny Llama Don't forget to like, comment, and subscribe for more AI tutorials! Let me know what you build! 👇 #AIChatbots, #ChatbotTutorial, #HuggingFace, #ConversationalAI, #AIWithMemory, #BuildAIChatbot, #AIDevelopment, #PythonProgramming, #GoogleColab, #TinyLlama, #LangChain, #AutoTokenizer, #TextGeneration, #NLP, #MachineLearning, #DeepLearning, #AIProject, #StepByStepTutorial, #CodingTutorial, #AIChatbotWithMemory, #ContextualAI, #EasyAITutorial, #AIForBeginners, #PracticalAI, #AIAplications, #AITools, #HowToCreateAIChatbots, #AIChatbotDevelopment, #CreateYourOwnAIChatbot, #PythonForAI, #FreeAITutorial, #LLM, #LargeLanguageModel, #MemoryAugmentation, #ConversationBuffer, #PromptEngineering, #ModelSelection, #ModelTraining, #AIModelDeployment, #DougOrtiz #dougortiz ▬▬▬▬▬▬ 👋 Contact Me ▬▬▬▬▬▬ ➡ LinkedIn: https://www.linkedin.com/in/doug-ortiz-illustris/ ✍️ Blog: https://dougortiz.blogspot.com/ ▬▬▬▬▬▬ ⏱ Timestamps ▬▬▬▬▬▬ 00:00 Introduction: Solving AI's Memory Problem 00:23 Setting Up the Environment 00:41 Installing and Importing Libraries 01:13 Configuring the Model and Tokenizer 01:46 Building the Conversational Pipeline 02:03 Implementing Memory in AI 02:24 Testing and Verifying AI Memory 04:17 Conclusion and Next Steps
youtu.be
September 17, 2025 at 10:57 AM
LangChain Advanced Patterns: Building Production-Grade AI Systems
## Beyond Basic LangChain You've built a simple agent. Now scale it. Production LangChain systems require: * Memory management * Error handling * Performance optimization * Monitoring & observability ## Memory Patterns ### Conversation Memory from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() agent = initialize_agent( tools, llm, memory=memory, agent_type="conversational" ) ### Summary Memory (for long conversations) from langchain.memory import ConversationSummaryMemory memory = ConversationSummaryMemory( llm=OpenAI(), buffer="Current conversation summarized" ) ## Tool Chains & Sequences ### Sequential Chain from langchain.chains import SequentialChain chain = SequentialChain( chains=[chain1, chain2, chain3], verbose=True ) ### Conditional Routing router_template = """Given the input, route to: analysis, coding, or research Input: {input} Route:""" router = llm_chain.run(router_template) if "coding" in router: result = coding_agent.run(input) ## Error Handling & Retry Logic from tenacity import retry, stop_after_attempt @retry(stop=stop_after_attempt(3))def safe_agent_run(query): return agent.run(query) try: result = safe_agent_run(query) except Exception as e: logger.error(f"Agent failed: {e}") result = fallback_response() ## Performance Optimization ### Caching from langchain.cache import RedisCache import redis redis_client = redis.Redis.from_url("redis://localhost") langchain.llm_cache = RedisCache(redis_client=redis_client) ### Batch Processing results = [agent.run(q) for q in queries] # Better: Use async import asyncio results = await asyncio.gather(*[async_agent(q) for q in queries]) ## Monitoring & Observability import logging from datetime import datetime class AgentLogger: def log_run(self, query, response, duration): logging.info(f"Query: {query}") logging.info(f"Response: {response}") logging.info(f"Duration: {duration}s") # Track metrics self.track_metric("agent_latency", duration) self.track_metric("token_usage", count_tokens(response)) ## Integration with Vector Stores from langchain.vectorstores import Pinecone from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vector_store = Pinecone.from_documents(docs, embeddings) retriever = vector_store.as_retriever() agent_with_retrieval = RetrievalQA.from_chain_type( llm=llm, retriever=retriever ) ## Deployment Strategies ### Local + Cloud Hybrid * Local cache for frequently used data * Cloud for complex reasoning * Best of both worlds ### Serverless Deployment # AWS Lambda def lambda_handler(event, context): query = event['query'] result = agent.run(query) return {'statusCode': 200, 'body': result} ## Testing Your Agent def test_agent_accuracy(): test_cases = [ ("query1", "expected_output1"), ("query2", "expected_output2") ] for query, expected in test_cases: result = agent.run(query) assert verify_correctness(result, expected) ## Production Checklist ✅ Error handling for all tool calls ✅ Logging for debugging ✅ Monitoring & alerting ✅ Rate limiting ✅ Input validation ✅ Output sanitization ✅ Cost tracking ✅ Performance metrics ✅ Rollback procedures ✅ Security hardening ## Common Production Issues **Issue 1** : Token limits exceeded → Solution: Summarize long conversations **Issue 2** : Tool calls fail silently → Solution: Add explicit error messages **Issue 3** : Costs spiral out of control → Solution: Implement token budgets **Issue 4** : Model drift over time → Solution: Regular monitoring & retraining ## The Enterprise Path LangChain in enterprise = structured, monitored, optimized. You now have the patterns to build production systems. **What LangChain patterns are you using?**
dev.to
July 21, 2026 at 6:23 AM