Bird
0
0

You want to build a chat app that remembers only the last 3 messages. Which approach using ConversationBufferMemory is best to achieve this?

hard📝 Application Q15 of 15
LangChain - Conversational RAG
You want to build a chat app that remembers only the last 3 messages. Which approach using ConversationBufferMemory is best to achieve this?
AUse ConversationBufferMemory with a custom buffer size of 3
BUse ConversationBufferMemory without changes; it stores all messages
CManually delete older messages from chat_memory after each addition
DCreate a new ConversationBufferMemory instance for every message
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes