Orchestrating Workflows with LangChain

Adding Memory

Learning Outcome

4

Apply this understanding to choose the right memory type

3

Recognize the difference between Memory and the Context Window.

2

Identify the four types of memory

1

Understand what Memory in LangChain is ?

Recall: Core Components of LangChain

Prompts : A Prompt provides instructions.

LLM : The LLM generates a response.

Output Parser : An Output Parser formats the result.

Imagine you are talking to your personal assistant about the weekly tasks planned for the month.

You tell your assistant about different tasks, such as:

Week 1: Hold a meeting with stakeholders.

Week 2: Prepare the monthly report.

Week 3: Review the project progress with the team.

Week 4: Conduct a meeting with managers.

Your personal assistant notes all these tasks in a notebook.

After a few days, you ask your PA:

What task was planned for the 4th week?

Your assistant refers to the notebook, finds the information, and tells you:

The meeting with managers was planned for the 4th week.”

Just like the assistant uses the notebook to remember previous information, LangChain Memory stores conversation information and provides it when needed.

Memory in LangChain is a mechanism that enables an AI application to remember information from previous interactions so it can provide more natural, personalised, and context-aware responses during a conversation. Memory allows conversations to flow naturally, just as they do between humans.

What is Memory in LangChain?

Without Memory vs With Memory

Without Memory

With Memory

My name is Rahul

Nice to meet you

What is my name

I don't know

My name is Rahul

Nice to meet you Rahul

What is my name

Your Name is Rahul

How Memory Works

01

02

03

04

05

User sends a message

LangChain stores the conversation

User asks another question

LangChain retrieves previous context

LLM generates a context-aware response

How Memory Works

01

02

03

04

05

User sends a message

LangChain stores the conversation

User asks another question

LangChain retrieves previous context

LLM generates a context-aware response

Types of Memory in LangChain

01

Conversation Buffer Memory

The simplest type; stores the entire conversation from start to finish, allowing the AI to reference anything said earlier. Easy to implement but consumes more tokens as conversations grow longer.

ConversationBufferMemory()

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

Types of Memory in LangChain

01

Conversation Buffer Memory

The simplest type; stores the entire conversation from start to finish, allowing the AI to reference anything said earlier. Easy to implement but consumes more tokens as conversations grow longer.

ConversationBufferMemory()

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

Types of Memory in LangChain

02

Conversation Buffer Memory

The simplest type; stores the entire conversation from start to finish, allowing the AI to reference anything said earlier. Easy to implement but consumes more tokens as conversations grow longer.

ConversationBufferMemory()

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

02

Conversation Buffer Window Memory

Only the last few interactions are remembered; older messages are discarded, reducing token usage while still maintaining recent context.

ConversationBufferWindow
Memory( k=3 )

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

K = 3

Types of Memory in LangChain

02

Conversation Buffer Memory

The simplest type; stores the entire conversation from start to finish, allowing the AI to reference anything said earlier. Easy to implement but consumes more tokens as conversations grow longer.

ConversationBufferMemory()

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

02

Conversation Buffer Window Memory

Only the last few interactions are remembered; older messages are discarded, reducing token usage while still maintaining recent context.

ConversationBufferWindow
Memory( k=3 )

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

K = 3

03

Conversation Summary

Instead of storing every individual message, LangChain creates a running summary of the conversation (e.g. "The user is learning Python and Machine Learning and preparing for interviews"). This saves tokens and works well for long conversations.

ConversationBufferMemory()

msg 1

msg 2

msg 3

  • User: “I’m building a food delivery app.”
  • Assistant: “Great! What technology are you using?”
  • User: “I’m using React.”
  • User: “The app is called FoodHub.”
  • User: “I want a simple and modern design.”

“User is building a food delivery app called FoodHub using React, with a simple and modern design.”

Types of Memory in LangChain

02

Conversation Buffer Memory

The simplest type; stores the entire conversation from start to finish, allowing the AI to reference anything said earlier. Easy to implement but consumes more tokens as conversations grow longer.

ConversationBufferMemory()

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

02

Conversation Buffer Window Memory

Only the last few interactions are remembered; older messages are discarded, reducing token usage while still maintaining recent context.

ConversationBufferWindow
Memory( k=3 )

Stores every message

msg 1

msg 2

msg 3

msg 4

msg 5

K = 3

03

Conversation Summary

Instead of storing every individual message, LangChain creates a running summary of the conversation (e.g. "The user is learning Python and Machine Learning and preparing for interviews"). This saves tokens and works well for long conversations.

ConversationBufferMemory()

msg 1

msg 2

msg 3

  • User: “I’m building a food delivery app.”
  • Assistant: “Great! What technology are you using?”
  • User: “I’m using React.”
  • User: “The app is called FoodHub.”
  • User: “I want a simple and modern design.”

“User is building a food delivery app called FoodHub using React, with a simple and modern design.”

04

Vector Store Memory

Important conversation pieces are converted into embeddings and stored in a vector database; the system retrieves the most relevant memories using similarity search. Especially useful for long-term memory, enterprise chatbots, and personalised AI assistants.

Vector space · similarity search

Memory vs Context Window

msg 5

Memory in RAG Applications

msg 5

A user asks "Explain Machine Learning." Later, they ask "Explain it with an example."

Scenario

The LLM combines the conversation memory, the retrieved documents, and the latest question to produce a complete and context-aware response.

At the same time, the RAG system retrieves relevant documents about Machine Learning examples.

Memory remembers that "it" refers to Machine Learning from the earlier turn.

Memory

Rag

LLM

Summary

4

Memory and RAG combine history with knowledge.

3

Memory stores history; context limits processing capacity.

2

Four memory types suit different conversation needs.

Memory helps AI remember previous interactions.

1

Quiz

What is the main purpose of LangChain Memory?

A. Increase model creativity

B. Store previous conversation information

C. Reduce response length

D. Generate random responses  

What is the main purpose of LangChain Memory?

A. Increase model creativity

B. Store previous conversation information

C. Reduce response length

D. Generate random responses  

Quiz-Answer