Bird
Raised Fist0
LangChainframework~10 mins

Chat history management in LangChain - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Chat history management
User sends message
↓
Add message to chat history
↓
Pass chat history to language model
↓
Model generates response
↓
Add response to chat history
↓
Return response to user
↓
Wait for next user message or end
This flow shows how each user message and model response is saved in chat history, which is then used to generate context-aware replies.
Execution Sample
LangChain
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain_community.llms.fake import FakeListLLM

llm = FakeListLLM(responses=["Hi! How can I help you today?"])
memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)

response = chain.predict(input="Hello!")
This code creates a conversation chain with memory to store chat history and generates a response to 'Hello!'.
Execution Table
StepActionChat History BeforeInput/OutputChat History After
1User sends message[]Hello![{'role': 'user', 'content': 'Hello!'}]
2Pass history to model[{'role': 'user', 'content': 'Hello!'}]Model generates response[{'role': 'user', 'content': 'Hello!'}]
3Add model response[{'role': 'user', 'content': 'Hello!'}]Hi! How can I help you today?[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
4Return response[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]Hi! How can I help you today?[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
5Wait for next message[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]Waiting...[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
💡 Execution stops waiting for next user input or conversation end.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
chat_history[][{'role': 'user', 'content': 'Hello!'}][{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}][{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
input_messageNone"Hello!""Hello!""Hello!"
model_responseNoneNone"Hi! How can I help you today?""Hi! How can I help you today?"
Key Moments - 2 Insights
Why does the chat history include both user and assistant messages?
Because the model needs the full conversation context to generate relevant responses, as shown in steps 1 and 3 of the execution_table.
What happens if we don't update the chat history after the model response?
The conversation loses context for future messages, so the model can't remember past exchanges, breaking the flow (see step 3 vs step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the chat history after step 1?
A[{'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
B[]
C[{'role': 'user', 'content': 'Hello!'}]
D[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi! How can I help you today?'}]
💡 Hint
Check the 'Chat History After' column for step 1 in the execution_table.
At which step does the model response get added to chat history?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look for the step where chat history grows to include the assistant's message.
If the user sends a new message, how will the chat history change?
AIt will reset to empty.
BIt will add the new user message to the existing history.
CIt will only keep the last user message.
DIt will remove the assistant messages.
💡 Hint
Refer to how chat_history updates after user input in variable_tracker.
Concept Snapshot
Chat history management stores all user and assistant messages.
Each new message is added to history.
History is passed to the model for context.
Model response is appended to history.
This keeps conversation context for better replies.
Full Transcript
Chat history management in langchain means saving every user message and model reply in a list. When a user sends a message, it is added to the chat history. Then the whole history is sent to the language model to generate a response. The model's response is also added to the history. This way, the conversation remembers what was said before. The example code shows creating a ConversationChain with ConversationBufferMemory to handle this automatically. The execution table traces each step: user message added, model generates reply, reply added, and response returned. Variables like chat_history update after each step. Key points include why both user and assistant messages are stored and what happens if history is not updated. The quiz checks understanding of chat history state at different steps and how it changes with new messages. This helps beginners see how chat history keeps conversations connected.

Practice

(1/5)
1. What is the main purpose of ConversationBufferMemory in Langchain chat applications?
easy
A. To store and manage past chat messages for context
B. To generate new chat messages automatically
C. To connect to external databases
D. To format chat messages for display

Solution

  1. Step 1: Understand the role of chat history

    Chat history helps keep track of previous messages to maintain context in conversations.
  2. Step 2: Identify what ConversationBufferMemory does

    This class stores and retrieves past messages so the chat app remembers what was said.
  3. Final Answer:

    To store and manage past chat messages for context -> Option A
  4. Quick Check:

    Chat history storage = A [OK]
Hint: Remember: ConversationBufferMemory saves past messages [OK]
Common Mistakes:
  • Thinking it generates new messages
  • Confusing it with database connectors
  • Assuming it formats messages for UI
2. Which of the following is the correct way to initialize a ConversationBufferMemory instance in Langchain?
easy
A. memory = ConversationBufferMemory.create()
B. memory = ConversationBufferMemory()
C. memory = ConversationBufferMemory.new()
D. memory = ConversationBufferMemory.load()

Solution

  1. Step 1: Recall the standard initialization pattern

    In Langchain, classes are usually instantiated by calling the class name with parentheses.
  2. Step 2: Check the options for correct syntax

    Only ConversationBufferMemory() follows this pattern; others are invalid or do not exist.
  3. Final Answer:

    memory = ConversationBufferMemory() -> Option B
  4. Quick Check:

    Standard class instantiation = D [OK]
Hint: Use parentheses to create instances: ClassName() [OK]
Common Mistakes:
  • Using .load(), .new(), or .create() which are not valid here
  • Forgetting parentheses when creating an instance
  • Confusing with static methods
3. Given this code snippet, what will print(memory.load_memory_variables({})) output?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message('Hello')
memory.chat_memory.add_ai_message('Hi there!')
medium
A. {'history': 'Human: Hello\nAI: Hi there!\n'}
B. {'history': 'Hi there!\nHello\n'}
C. {'history': ''}
D. Error: method not found

Solution

  1. Step 1: Understand how messages are added

    User and AI messages are added in order: 'Hello' then 'Hi there!'.
  2. Step 2: Check how load_memory_variables returns history

    It returns a dictionary with key 'history' and a string showing messages prefixed by 'Human:' and 'AI:' in order.
  3. Final Answer:

    {'history': 'Human: Hello\nAI: Hi there!\n'} -> Option A
  4. Quick Check:

    History shows messages in order with labels = C [OK]
Hint: History shows messages in order with 'Human:' and 'AI:' labels [OK]
Common Mistakes:
  • Mixing message order
  • Expecting empty history
  • Assuming method does not exist
4. What is wrong with this code snippet that tries to add chat history?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.add_user_message('Hello')
medium
A. You must import a different class for adding messages
B. You need to call memory.save() before adding messages
C. The message text must be a list, not a string
D. ConversationBufferMemory has no method add_user_message directly

Solution

  1. Step 1: Check method availability on ConversationBufferMemory

    The method add_user_message is not directly on ConversationBufferMemory but on its chat_memory attribute.
  2. Step 2: Identify correct usage

    Correct usage is memory.chat_memory.add_user_message(), not memory.add_user_message().
  3. Final Answer:

    ConversationBufferMemory has no method add_user_message directly -> Option D
  4. Quick Check:

    Use chat_memory to add messages = B [OK]
Hint: Add messages via memory.chat_memory, not memory directly [OK]
Common Mistakes:
  • Calling add_user_message on memory instead of chat_memory
  • Thinking message must be a list
  • Assuming save() is required before adding
5. You want to build a chat app that remembers only the last 3 messages. Which approach using ConversationBufferMemory is best to achieve this?
hard
A. Use ConversationBufferMemory with a custom buffer size of 3
B. Use ConversationBufferMemory without changes; it stores all messages
C. Manually delete older messages from chat_memory after each addition
D. Create a new ConversationBufferMemory instance for every message

Solution

  1. Step 1: Understand ConversationBufferMemory limitations

    ConversationBufferMemory stores all past messages without a built-in parameter to limit the number of messages (unlike ConversationBufferWindowMemory).
  2. Step 2: Identify the best approach among options

    Manually deleting older messages from memory.chat_memory.messages after each addition (e.g., memory.chat_memory.messages = memory.chat_memory.messages[-3:]) keeps only the last 3.
  3. Final Answer:

    Manually delete older messages from chat_memory after each addition -> Option C
  4. Quick Check:

    Manual management for custom limits = B [OK]
Hint: Manually prune memory.chat_memory.messages to last 3 after adds [OK]
Common Mistakes:
  • ConversationBufferMemory lacks message count limit parameter (use WindowMemory for that)
  • Not limiting buffer and storing unlimited history
  • Recreating instance loses all prior context
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard