{"id":8161,"date":"2023-11-11T06:01:52","date_gmt":"2023-11-11T14:01:52","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8161"},"modified":"2025-04-24T17:04:28","modified_gmt":"2025-04-24T17:04:28","slug":"memory-in-langchain-a-deep-dive-into-persistent-context","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/","title":{"rendered":"Memory in LangChain: A Deep Dive into Persistent Context"},"content":{"rendered":"\n<section class=\"section section--body\">\n<div class=\"section-divider\"><\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 class=\"graf graf--h4\">Basic Memory Types in LangChain<\/h4>\n<figure class=\"graf graf--figure\">\n<\/figure><\/div><\/div><\/section>\n\n\n\n<figure class=\"wp-block-image alignnone graf-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ\" alt=\"persistent context\"\/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@xingyechiang?utm_source=medium&amp;utm_medium=referral\">Xingye Jiang<\/a> on\u00a0<a href=\"http:\/\/Unsplash.com\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<p class=\"graf graf--p\">Have you ever talked with someone and wished they could remember details from your previous chats? It\u2019s like talking to Dory from \u2018Finding Nemo\u2019\u200a\u2014\u200aevery interaction feels like the first. When designing language models, especially chat-based ones, maintaining context and memory is crucial to ensure the conversation flows seamlessly and feels natural.<\/p>\n\n\n\n<p class=\"graf graf--p\">Enter LangChain\u2019s Memory module, the superhero that saves our chat models from short-term memory woes. But what exactly is it, and why is it so important?<\/p>\n\n\n\n<p class=\"graf graf--p\">In this blog, we\u2019ll delve deep into the Memory module in LangChain. You\u2019ll understand its significance, how it functions, when to use it, and how to implement it.<\/p>\n\n\n\n<p class=\"graf graf--p\">By the end, you\u2019ll have a comprehensive understanding of how LangChain ensures its models don\u2019t \u201cpull a Dory\u201d and keep the conversation flowing smoothly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Memory in LangChain<\/h3>\n\n\n\n<p class=\"graf graf--p\">Remember Dory from \u2018Finding Nemo\u2019 and her notorious short-term memory loss?<\/p>\n\n\n\n<p class=\"graf graf--p\">LLMs, especially chat-based ones, are like that. You need a way to ensure your system doesn\u2019t \u2018pull a Dory\u2019 building LLM applications.<\/p>\n\n\n\n<p class=\"graf graf--p\">Luckily, LangChain has a memory module\u2026<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">What is&nbsp;it?<\/h4>\n\n\n\n<p class=\"graf graf--p\">In LangChain, the Memory module is responsible for persisting the state between calls of a chain or agent, which helps the language model remember previous interactions and use that information to make better decisions.<\/p>\n\n\n\n<p class=\"graf graf--p\">It provides a standard interface for persisting state between calls of a chain or agent, enabling the language model to have memory and context.<\/p>\n\n\n\n<p class=\"graf graf--p\">Memory is critical for personal assistants, autonomous agents, and agent simulations, where the language model needs to remember previous interactions to make informed decisions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">What does it&nbsp;do?<\/h4>\n\n\n\n<p class=\"graf graf--p\">The Memory module enables the language model to have memory and context, allowing the LLM to make informed decisions.<\/p>\n\n\n\n<p class=\"graf graf--p\">It allows the model to remember user inputs, system responses, and any other relevant information. The stored information can be accessed and utilized during subsequent interactions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Why do I need&nbsp;it?<\/h4>\n\n\n\n<p class=\"graf graf--p\">The Memory module helps you build more interactive and personalized applications.<\/p>\n\n\n\n<p class=\"graf graf--p\">It gives the language model a sense of continuity and memory of past interactions. With memory, the model can provide more contextually relevant responses and make informed decisions based on previous inputs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">When do I use&nbsp;it?<\/h4>\n\n\n\n<p class=\"graf graf--p\">You should use the Memory module whenever you want to create applications that require context and persistence between interactions.<\/p>\n\n\n\n<p class=\"graf graf--p\">It is handy for tasks like personal assistants, where the model needs to remember user preferences, previous queries, and other relevant information.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Every memory system performs two main tasks: reading and&nbsp;writing.<\/h4>\n\n\n\n<p class=\"graf graf--p\">Every chain has core logic that requires specific inputs.<\/p>\n\n\n\n<p class=\"graf graf--p\">Some inputs originate from the user, while others derive from memory. During a run, a chain accesses its memory system twice:<\/p>\n\n\n\n<p class=\"graf graf--p\">1) It reads from memory to supplement user inputs before executing core logic.<\/p>\n\n\n\n<p class=\"graf graf--p\">2) After processing but before responding, it writes the current run\u2019s data to memory for future reference.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Incorporating Memory<\/h4>\n\n\n\n<p class=\"graf graf--p\">Two fundamental decisions shape any memory system:<\/p>\n\n\n\n<p class=\"graf graf--p\">1) The method of storing state.<\/p>\n\n\n\n<p class=\"graf graf--p\">2) The approach to querying that state.<\/p>\n\n\n\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Storing:<\/strong> At the heart of memory lies a record of all chat interactions.<\/p>\n\n\n\n<p class=\"graf graf--p\">LangChain\u2019s memory module offers various ways to store these chats, ranging from temporary in-memory lists to enduring databases.<\/p>\n\n\n\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Querying:<\/strong> While storing chat logs is straightforward, designing algorithms and structures to interpret them isn\u2019t.<\/p>\n\n\n\n<p class=\"graf graf--p\">A basic memory system might display recent messages. A more advanced one could summarize the last \u2018K\u2019 messages. The most refined systems might identify entities from stored chats and present details only about those entities in the current session.<\/p>\n\n\n\n<p class=\"graf graf--p\">Different applications demand unique memory querying methods.<\/p>\n\n\n\n<p class=\"graf graf--p\">LangChain\u2019s memory module simplifies the initiation with basic systems and supports creating tailored systems when necessary.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Implementing Memory<\/h4>\n\n\n\n<p class=\"graf graf--p\">1) Setup prompt and memory<\/p>\n\n\n\n<p class=\"graf graf--p\">2) Initialize LLMChain<\/p>\n\n\n\n<p class=\"graf graf--p\">3) Call LLMChain<\/p>\n\n\n\n<p class=\"graf graf--p\">Before we get coding, let\u2019s take care of some preliminaries:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">%%capture\n!pip install langchain openai tiktoken\n\n<span class=\"hljs-keyword\">import<\/span> os\n<span class=\"hljs-keyword\">import<\/span> getpass\n\nos.environ[<span class=\"hljs-string\">\"OPENAI_API_KEY\"<\/span>] = getpass.getpass(<span class=\"hljs-string\">\"Enter Your OpenAI API Key:\"<\/span>)<\/span><\/pre>\n\n\n\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\">\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<blockquote class=\"graf graf--pullquote\"><p>Want to learn how to build modern software with LLMs using the newest tools and techniques in the field? <a class=\"markup--anchor markup--pullquote-anchor\" href=\"https:\/\/www.comet.com\/production\/site\/llm-course\/?utm_source=Heartbeat&amp;utm_medium=referral&amp;utm_content=Medium&amp;utm_campaign=Heartbeat_LangChain_Series_HS\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/www.comet.com\/production\/site\/llm-course\/?utm_source=Heartbeat&amp;utm_medium=referral&amp;utm_content=Medium&amp;utm_campaign=Heartbeat_LangChain_Series_HS\">Check out this free LLMOps course<\/a> from industry expert Elvis Saravia of&nbsp;DAIR.AI.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<\/section>\n\n\n\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\">\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h3 class=\"graf graf--h3\">ConversationBufferMemory<\/h3>\n<p class=\"graf graf--p\">In this section, you will explore the Memory functionality in LangChain.<\/p>\n<p class=\"graf graf--p\">Specifically, you will learn how to interact with an arbitrary memory class and use <code class=\"markup--code markup--p-code\">ConversationBufferMemory<\/code> in chains.<code class=\"markup--code markup--p-code\">ConversationBufferMemory<\/code> is a simple memory type that stores chat messages in a buffer and passes them to the prompt template.<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> langchain.memory <span class=\"hljs-keyword\">import<\/span> ConversationBufferMemory\n\nmemory = ConversationBufferMemory()\nmemory.chat_memory.add_user_message(<span class=\"hljs-string\">\"wagwan, bruv?\"<\/span>)\nmemory.chat_memory.add_ai_message(<span class=\"hljs-string\">\"Alright, guv'nor? Just been 'round the old manor, innit?\"<\/span>)<\/span><\/pre>\n<h4 class=\"graf graf--h4\">Reading variables from&nbsp;memory<\/h4>\n<p class=\"graf graf--p\">Before going into the chain, variables are read from memory. Variable names need to align with what the chain expects. You can inspect variables by calling <code class=\"markup--code markup--p-code\">memory.load_memory_variables<\/code><\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">memory.load_memory_variables({})<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">{'history': \"Human: wagwan, bruv?\\nAI: Alright, guv'nor? Just been 'round the old manor, innit?\"}<\/span><\/pre>\n<p class=\"graf graf--p\">Notice that <code class=\"markup--code markup--p-code\">load_memory_variables<\/code> returns a single key, <code class=\"markup--code markup--p-code\">history<\/code>.<\/p>\n<p class=\"graf graf--p\">This means that your chain (and likely your prompt) expects an input named <code class=\"markup--code markup--p-code\">history<\/code>. You control this variable through parameters on the memory class. For example, if you want the memory variables to be returned in the key <code class=\"markup--code markup--p-code\">chat_history<\/code> you can do the following:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">memory = ConversationBufferMemory(memory_key=<span class=\"hljs-string\">\"chat_history\"<\/span>)\n\nmemory.chat_memory.add_user_message(<span class=\"hljs-string\">\"wagwan, bruv?\"<\/span>)\n\nmemory.chat_memory.add_ai_message(<span class=\"hljs-string\">\"Alright, guv'nor? Just been 'round the old manor, innit?\"<\/span>)\n\nmemory.load_memory_variables({})<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">{'chat_history': \"Human: wagwan, bruv?\\nAI: Alright, guv'nor? Just been 'round the old manor, innit?\"}<\/span><\/pre>\n<h4 class=\"graf graf--h4\">Memory as strings, or list of&nbsp;strings<\/h4>\n<p class=\"graf graf--p\">When it comes to memory, one of the most common types is the storage and retrieval of chat messages.<\/p>\n<p class=\"graf graf--p\">There are two ways to retrieve these messages:<\/p>\n<p class=\"graf graf--p\">1) A single string that concatenates all the messages together, which is useful when the messages will be passed in Language Models<\/p>\n<p class=\"graf graf--p\">2) A list of ChatMessages, which is useful when the messages are passed into ChatModels.<\/p>\n<p class=\"graf graf--p\">By default, chat messages are returned as a single string. However, if you want to return them as a list of messages, you can set the parameter <code class=\"markup--code markup--p-code\">return_messages<\/code> to True.<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">memory = ConversationBufferMemory(memory_key=<span class=\"hljs-string\">\"chat_history\"<\/span>, return_messages=<span class=\"hljs-literal\">True<\/span>)\n\nmemory.chat_memory.add_user_message(<span class=\"hljs-string\">\"wagwan, bruv?\"<\/span>)\n\nmemory.chat_memory.add_ai_message(<span class=\"hljs-string\">\"Alright, guv'nor? Just been 'round the old manor, innit?\"<\/span>)\n\nmemory.load_memory_variables({})<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">{'chat_history': [HumanMessage(content='wagwan, bruv?', additional_kwargs={}, example=False),\n  AIMessage(content=\"Alright, guv'nor? Just been 'round the old manor, innit?\", additional_kwargs={}, example=False)]}<\/span><\/pre>\n<h3 class=\"graf graf--h3\">Keys in&nbsp;Memory<\/h3>\n<p class=\"graf graf--p\">In some cases, chains receive or provide multiple input\/output keys.<\/p>\n<p class=\"graf graf--p\">In this scenario, it\u2019ll be challenging to determine which keys to store in the chat message history. But you can manage this by using the <code class=\"markup--code markup--p-code\">input_key<\/code> and <code class=\"markup--code markup--p-code\">output_key parameters<\/code> in memory types. By default, these parameters are set to None, which means that if there is only one input\/output key, it will be used.<\/p>\n<p class=\"graf graf--p\">However, if there are multiple input\/output keys, you must specify the name of the one to be used.<\/p>\n<h4 class=\"graf graf--h4\">Putting it all&nbsp;together<\/h4>\n<p class=\"graf graf--p\">Let\u2019s look at using this in an LLMChain and show working with both an LLM and a ChatModel.<\/p>\n<h4 class=\"graf graf--h4\">Using an&nbsp;LLM<\/h4>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"1\" data-code-block-lang=\"python\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> langchain.llms <span class=\"hljs-keyword\">import<\/span> OpenAI\n<span class=\"hljs-keyword\">from<\/span> langchain.prompts <span class=\"hljs-keyword\">import<\/span> PromptTemplate\n<span class=\"hljs-keyword\">from<\/span> langchain.chains <span class=\"hljs-keyword\">import<\/span> LLMChain\n<span class=\"hljs-keyword\">from<\/span> langchain.memory <span class=\"hljs-keyword\">import<\/span> ConversationBufferMemory\n\n<span class=\"hljs-comment\">#instantiate the language model<\/span>\nllm = OpenAI(temperature=<span class=\"hljs-number\">0.1<\/span>)\n\n<span class=\"hljs-comment\"># Look how \"chat_history\" is an input variable to the prompt template<\/span>\ntemplate = <span class=\"hljs-string\">\"\"\"\n\nYou are Spider-Punk, Hobart Brown from Earth-138.\n\nYour manner of speaking is rebellious and infused with punk rock lingo,\noften quippy and defiant against authority.\n\nSpeak with confidence, wit, and a touch of brashness, always ready\nto  challenge the status quo with passion.\n\nYour personality swings between that classic cockney sensibility\nand immeasurable Black-British street swagger\n\nPrevious conversation:\n{chat_history}\n\nNew human question: {question}\nResponse:\n\"\"\"<\/span>\n\nprompt = PromptTemplate.from_template(template)\n\n<span class=\"hljs-comment\"># Notice that we need to align the `memory_key`<\/span>\n\nmemory = ConversationBufferMemory(memory_key=<span class=\"hljs-string\">\"chat_history\"<\/span>)\n\nconversation = LLMChain(\n    llm=llm,\n    prompt=prompt,\n    verbose=<span class=\"hljs-literal\">True<\/span>,\n    memory=memory\n)\n\nconversation({<span class=\"hljs-string\">\"question\"<\/span>:<span class=\"hljs-string\">\"wagwan, bruv?\"<\/span>})<\/span><\/pre>\n<p class=\"graf graf--p\">This will have the following output:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">{'question': 'wagwan, bruv?',\n 'chat_history': '',\n 'text': \"Wagwan, fam! Just chillin' here, livin' my best life and fightin' the power. You know how it is.\"}<\/span><\/pre>\n<h3 class=\"graf graf--h3\">Using a ChatModel and exploring different memory&nbsp;types<\/h3>\n<p class=\"graf graf--p\">We\u2019ll use the <code class=\"markup--code markup--p-code\">ChatOpenAI<\/code> model with the same prompt for all the following examples:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"1\" data-code-block-lang=\"python\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> langchain.chat_models <span class=\"hljs-keyword\">import<\/span> ChatOpenAI\n\n<span class=\"hljs-keyword\">from<\/span> langchain.prompts <span class=\"hljs-keyword\">import<\/span> (\n    ChatPromptTemplate,\n    MessagesPlaceholder,\n    SystemMessagePromptTemplate,\n    HumanMessagePromptTemplate,\n)\n\n<span class=\"hljs-keyword\">from<\/span> langchain.chains <span class=\"hljs-keyword\">import<\/span> LLMChain\n\n<span class=\"hljs-keyword\">from<\/span> langchain.memory <span class=\"hljs-keyword\">import<\/span> ConversationBufferMemory\n\nllm = ChatOpenAI()\n\nprompt = ChatPromptTemplate(\n    messages=[\n        SystemMessagePromptTemplate.from_template(\n            <span class=\"hljs-string\">\"\"\"\n            You are Spider-Punk, Hobart Brown from Earth-138.\n\n            Your manner of speaking is rebellious and infused with punk rock lingo,\n            often quippy and defiant against authority.\n\n            Speak with confidence, wit, and a touch of brashness, always ready\n            to  challenge the status quo with passion.\n\n            Your personality swings between that classic cockney sensibility\n            and immeasurable Black-British street swagger\n            \"\"\"<\/span>\n        ),\n        <span class=\"hljs-comment\"># The `variable_name` here is what must align with memory<\/span>\n        MessagesPlaceholder(variable_name=<span class=\"hljs-string\">\"chat_history\"<\/span>),\n        HumanMessagePromptTemplate.from_template(<span class=\"hljs-string\">\"{question}\"<\/span>)\n    ]\n)\n\n<span class=\"hljs-comment\"># Notice that we `return_messages=True` to fit into the MessagesPlaceholder<\/span>\n<span class=\"hljs-comment\"># Notice that `\"chat_history\"` aligns with the MessagesPlaceholder name.<\/span>\nmemory = ConversationBufferMemory(memory_key=<span class=\"hljs-string\">\"chat_history\"<\/span>, return_messages=<span class=\"hljs-literal\">True<\/span>)\n\nconversation = LLMChain(\n    llm=llm,\n    prompt=prompt,\n    verbose=<span class=\"hljs-literal\">True<\/span>,\n    memory=memory\n)\n\nconversation.predict(question=<span class=\"hljs-string\">\"wagwan, bruv?\"<\/span>)<\/span><\/pre>\n<p class=\"graf graf--p\">And this will produce the following output:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">Oi, wagwan, mate? Ready to rock and roll? What's the scoop, my rebellious compadre? How can I smash the system for ya today?<\/span><\/pre>\n<p class=\"graf graf--p\">From here, we can carry on the conversation:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">conversation.predict(question=<span class=\"hljs-string\">\"Wicked mate. What's the word on the villanry?\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">Ah, the villainry, my friend. The dirty underbelly of this twisted world. Well, listen up, 'cause Spidey-Punk is here to spill the beans. There's always some scumbag trying to ruin our good time, but fear not, for I'm here to shred their plans like a punk riff on a battered guitar.\n\nWhether it's that sneaky symbiote slinger Venom or that maniacal mastermind Green Goblin, I'll be there, ready to give 'em a taste of my righteous fists. These villains think they can run rampant, but they've got another thing coming when Spidey-Punk crashes the party.\n\nSo, my rebellious friend, keep your eyes peeled and your ears tuned. When the villainry rears its ugly head, Spidey-Punk will be there to give 'em a one-way ticket to chaos and justice. Anything else you wanna know, mate?<\/span><\/pre>\n<p class=\"graf graf--p\">And you can see here how the conversation history is stored:<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">[HumanMessage(content='wagwan, bruv?', additional_kwargs={}, example=False),\n AIMessage(content=\"Oi, wagwan, mate? Ready to rock and roll? What's the scoop, my rebellious compadre? How can I smash the system for ya today?\", additional_kwargs={}, example=False),\n HumanMessage(content=\"Wicked mate. What's the word on the villanry?\", additional_kwargs={}, example=False),\n AIMessage(content=\"Ah, the villainry, my friend. The dirty underbelly of this twisted world. Well, listen up, 'cause Spidey-Punk is here to spill the beans. There's always some scumbag trying to ruin our good time, but fear not, for I'm here to shred their plans like a punk riff on a battered guitar.\\n\\nWhether it's that sneaky symbiote slinger Venom or that maniacal mastermind Green Goblin, I'll be there, ready to give 'em a taste of my righteous fists. These villains think they can run rampant, but they've got another thing coming when Spidey-Punk crashes the party.\\n\\nSo, my rebellious friend, keep your eyes peeled and your ears tuned. When the villainry rears its ugly head, Spidey-Punk will be there to give 'em a one-way ticket to chaos and justice. Anything else you wanna know, mate?\", additional_kwargs={}, example=False)]<\/span><\/pre>\n<h3 class=\"graf graf--h3\">ConversationBufferWindowMemory<\/h3>\n<p class=\"graf graf--p\">The <code class=\"markup--code markup--p-code\">ConversationBufferWindowMemory<\/code> is a tool that keeps track of past interactions in a conversation.<\/p>\n<p class=\"graf graf--p\">It does this by maintaining a list of the most recent interactions, and only using the last K interactions. This helps to ensure that the buffer doesn\u2019t become too large and allows for a sliding window of the most recent interactions to be kept. This type of memory is beneficial for keeping the history of past interactions small and manageable.<\/p>\n<p class=\"graf graf--p\">By only capturing the most recent interactions, it helps to prevent the buffer from becoming too large, which can be overwhelming and challenging to manage.<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"1\" data-code-block-lang=\"python\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> langchain.llms <span class=\"hljs-keyword\">import<\/span> OpenAI\n<span class=\"hljs-keyword\">from<\/span> langchain.chains <span class=\"hljs-keyword\">import<\/span> ConversationChain\n<span class=\"hljs-keyword\">from<\/span> langchain.memory <span class=\"hljs-keyword\">import<\/span> ConversationBufferWindowMemory\n\nmemory = ConversationBufferWindowMemory(k=<span class=\"hljs-number\">2<\/span>)\n\nconversation_with_summary = ConversationChain(\n    llm=OpenAI(temperature=<span class=\"hljs-number\">0<\/span>),\n    memory=ConversationBufferWindowMemory(k=<span class=\"hljs-number\">3<\/span>),\n    verbose=<span class=\"hljs-literal\">True<\/span>\n)\n\nconversation_with_summary.predict(<span class=\"hljs-built_in\">input<\/span>=<span class=\"hljs-string\">\"Wagwan, Bruv?\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">&gt; Entering new ConversationChain chain...\nPrompt after formatting:\nThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n\nCurrent conversation:\n\nHuman: Wagwan, Bruv?\nAI:\n\n&gt; Finished chain.\n Hi there! I'm doing well, thank you for asking. How are you?<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">conversation_with_summary.predict(<span class=\"hljs-built_in\">input<\/span>=<span class=\"hljs-string\">\"I'm out here trying to get it\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">&gt; Entering new ConversationChain chain...\nPrompt after formatting:\nThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n\nCurrent conversation:\nHuman: Wagwan, Bruv?\nAI:  Hi there! I'm doing well, thank you for asking. How are you?\nHuman: I'm out here trying to get it\nAI:\n\n&gt; Finished chain.\n That's great to hear! What are you trying to get?<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">conversation_with_summary.predict(<span class=\"hljs-built_in\">input<\/span>=<span class=\"hljs-string\">\"It, fam. I'm out here trying to it IT\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">&gt; Entering new ConversationChain chain...\nPrompt after formatting:\nThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n\nCurrent conversation:\nHuman: Wagwan, Bruv?\nAI:  Hi there! I'm doing well, thank you for asking. How are you?\nHuman: I'm out here trying to get it\nAI:  That's great to hear! What are you trying to get?\nHuman: It, fam. I'm out here trying to it IT\nAI:\n\n&gt; Finished chain.\n I\\'m not sure I understand what you mean by \"it.\" Could you provide more details?<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">conversation_with_summary.predict(<span class=\"hljs-built_in\">input<\/span>=<span class=\"hljs-string\">\"Glory, fame, wealth, freedom\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">Entering new ConversationChain chain...\nPrompt after formatting:\nThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n\nCurrent conversation:\nHuman: Wagwan, Bruv?\nAI:  Hi there! I'm doing well, thank you for asking. How are you?\nHuman: I'm out here trying to get it\nAI:  That's great to hear! What are you trying to get?\nHuman: It, fam. I'm out here trying to it IT\nAI:  I'm not sure I understand what you mean by \"it.\" Could you provide more details?\nHuman: Glory, fame, wealth, freedom\nAI:\n\n&gt; Finished chain.\n Ah, I see. So you're striving for glory, fame, wealth, and freedom. That's an admirable goal. What steps are you taking to achieve it?<\/span><\/pre>\n<p class=\"graf graf--p\">And you can see here that the memory being passed is the last three messages.<\/p>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"python\"><span class=\"pre--content\">conversation_with_summary.predict(<span class=\"hljs-built_in\">input<\/span>=<span class=\"hljs-string\">\"Help me sort that bit out mate\"<\/span>)<\/span><\/pre>\n<pre class=\"graf graf--pre graf--preV2\" spellcheck=\"false\" data-code-block-mode=\"2\" data-code-block-lang=\"plaintext\"><span class=\"pre--content\">Entering new ConversationChain chain...\nPrompt after formatting:\nThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n\nCurrent conversation:\nHuman: I'm out here trying to get it\nAI:  That's great to hear! What are you trying to get?\nHuman: It, fam. I'm out here trying to it IT\nAI:  I'm not sure I understand what you mean by \"it.\" Could you provide more details?\nHuman: Glory, fame, wealth, freedom\nAI:  Ah, I see. So you're striving for glory, fame, wealth, and freedom. That's an admirable goal. What steps are you taking to achieve it?\nHuman: Help me sort that bit out mate\nAI:\n\n&gt; Finished chain.\n I\\'m sorry, I don\\'t understand what you mean by \"help me sort that bit out mate.\" Could you explain it in a different way?<\/span><\/pre>\n<h3 class=\"graf graf--h3\">Wrapping Up Memory in LangChain<\/h3>\n<p class=\"graf graf--p\">As we\u2019ve traversed the intricate pathways of LangChain\u2019s Memory module, it\u2019s evident how pivotal memory and context are in making interactions with language models feel genuine and continuous. No longer are we stuck in the cyclical loop of starting every conversation from scratch. With the power of LangChain\u2019s memory capabilities, every interaction can be a continuation of the last, allowing for richer and more personalized engagements.<\/p>\n<p class=\"graf graf--p\">Whether building a personal assistant, an autonomous agent, or running agent simulations, integrating memory is no longer a luxury\u200a\u2014\u200ait\u2019s a necessity. Through the tools and strategies discussed, LangChain offers both simplicity for beginners and depth for experts.<\/p>\n<p class=\"graf graf--p\">But this is just the tip of the iceberg. In our upcoming piece, we will delve into more advanced memory types, showcasing how LangChain continuously pushes boundaries to offer even more nuanced and sophisticated memory solutions for varied applications.<\/p>\n<p class=\"graf graf--p\">In the vast landscape of language models, LangChain stands out, ensuring that every \u2018chat\u2019 feels like a chat and not a repeated introduction. So, the next time you engage with a chat model powered by LangChain, remember the unsung hero working behind the scenes: the Memory module, ensuring continuity and relevance in every conversation.<\/p>\n<p class=\"graf graf--p\">Here\u2019s to more meaningful, memorable, and context-rich conversations in the future, and stay tuned for our deep dive into advanced memory types!<\/p>\n<\/div>\n<\/div>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Basic Memory Types in LangChain Have you ever talked with someone and wished they could remember details from your previous chats? It\u2019s like talking to Dory from \u2018Finding Nemo\u2019\u200a\u2014\u200aevery interaction feels like the first. When designing language models, especially chat-based ones, maintaining context and memory is crucial to ensure the conversation flows seamlessly and feels [&hellip;]<\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[65,7],"tags":[70,71,52,31,34],"coauthors":[166],"class_list":["post-8161","post","type-post","status-publish","format-standard","hentry","category-llmops","category-tutorials","tag-langchain","tag-language-models","tag-llm","tag-llmops","tag-prompt-engineering"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Memory in LangChain: A Deep Dive into Persistent Context - Comet<\/title>\n<meta name=\"description\" content=\"LangChain Memory is a standard interface for persisting state between calls of a chain or agent, enabling the LM to have memory + context\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Memory in LangChain: A Deep Dive into Persistent Context\" \/>\n<meta property=\"og:description\" content=\"LangChain Memory is a standard interface for persisting state between calls of a chain or agent, enabling the LM to have memory + context\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-11T14:01:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:04:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ\" \/>\n<meta name=\"author\" content=\"Harpreet Sahota\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harpreet Sahota\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Memory in LangChain: A Deep Dive into Persistent Context - Comet","description":"LangChain Memory is a standard interface for persisting state between calls of a chain or agent, enabling the LM to have memory + context","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/","og_locale":"en_US","og_type":"article","og_title":"Memory in LangChain: A Deep Dive into Persistent Context","og_description":"LangChain Memory is a standard interface for persisting state between calls of a chain or agent, enabling the LM to have memory + context","og_url":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-11-11T14:01:52+00:00","article_modified_time":"2025-04-24T17:04:28+00:00","og_image":[{"url":"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ","type":"","width":"","height":""}],"author":"Harpreet Sahota","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Harpreet Sahota","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/"},"author":{"name":"Harpreet Sahota","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/46036ab474aa916e2873daece26a28d6"},"headline":"Memory in LangChain: A Deep Dive into Persistent Context","datePublished":"2023-11-11T14:01:52+00:00","dateModified":"2025-04-24T17:04:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/"},"wordCount":1427,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ","keywords":["LangChain","Language Models","LLM","LLMOps","Prompt Engineering"],"articleSection":["LLMOps","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/","url":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/","name":"Memory in LangChain: A Deep Dive into Persistent Context - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ","datePublished":"2023-11-11T14:01:52+00:00","dateModified":"2025-04-24T17:04:28+00:00","description":"LangChain Memory is a standard interface for persisting state between calls of a chain or agent, enabling the LM to have memory + context","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#primaryimage","url":"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ","contentUrl":"https:\/\/cdn-images-1.medium.com\/max\/1600\/0*P__G9UmmmLBrcWRQ"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/memory-in-langchain-a-deep-dive-into-persistent-context\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Memory in LangChain: A Deep Dive into Persistent Context"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/46036ab474aa916e2873daece26a28d6","name":"Harpreet Sahota","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/2d21512be19ba7e19a71a803309e2a88","url":"https:\/\/secure.gravatar.com\/avatar\/a6ca5a533fc9f143a0a7428037ff652aa0633d66bf27e76ae89b955ae72a0f2d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a6ca5a533fc9f143a0a7428037ff652aa0633d66bf27e76ae89b955ae72a0f2d?s=96&d=mm&r=g","caption":"Harpreet Sahota"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/theartistsofdatasciencegmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8161"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8161\/revisions"}],"predecessor-version":[{"id":15451,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8161\/revisions\/15451"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8161"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}