Short-term memory helps AI remember recent parts of a conversation. This makes the AI respond in a way that feels natural and connected.
Short-term memory (conversation context) in Agentic AI
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Agentic AI
conversation_memory = [] # Add new user input conversation_memory.append(user_input) # Use recent memory for response context = ' '.join(conversation_memory[-n:]) response = model.generate(context)
The variable conversation_memory stores recent messages.
Using the last n messages helps keep context manageable.
Examples
Agentic AI
conversation_memory = [] conversation_memory.append('Hello!') conversation_memory.append('How are you?') context = ' '.join(conversation_memory[-2:])
Agentic AI
conversation_memory = ['Hi', 'What is AI?'] conversation_memory.append('Tell me more about machine learning.') context = ' '.join(conversation_memory[-3:])
Sample Model
This simple AI remembers the last 3 messages and uses them to form a response. It shows how short-term memory keeps conversation context.
Agentic AI
class SimpleMemoryAI: def __init__(self, memory_size=3): self.memory = [] self.memory_size = memory_size def remember(self, message): self.memory.append(message) if len(self.memory) > self.memory_size: self.memory.pop(0) def respond(self, message): self.remember(message) context = ' | '.join(self.memory) # Simple response: echo last message with context info return f"I remember: {context}. You said: '{message}'" # Example usage bot = SimpleMemoryAI(memory_size=3) inputs = [ "Hello!", "What is AI?", "Can you explain machine learning?", "Thanks!" ] for msg in inputs: reply = bot.respond(msg) print(reply)
Important Notes
Short-term memory usually holds only a few recent messages to keep the AI focused.
Too much memory can slow down response and confuse the AI.
Memory can be cleared or reset when starting a new conversation.
Summary
Short-term memory helps AI keep track of recent conversation parts.
It stores a small number of recent messages to build context.
This makes AI responses feel connected and natural.
Practice
1. What is the main purpose of short-term memory in an AI conversation?
easy
Solution
Step 1: Understand short-term memory role
Short-term memory stores recent conversation parts to keep context.Step 2: Compare options with this role
Only To remember recent messages and keep the conversation connected matches this purpose; others describe different or incorrect functions.Final Answer:
To remember recent messages and keep the conversation connected -> Option AQuick Check:
Short-term memory = recent context [OK]
Hint: Short-term memory = recent messages stored [OK]
Common Mistakes:
- Confusing short-term with long-term memory
- Thinking it stores all past conversations
- Believing it deletes messages immediately
2. Which of the following is the correct way to represent short-term memory storing the last 3 messages in Python?
easy
Solution
Step 1: Understand Python list slicing for last 3 items
Usingmessages[-3:]gets the last 3 messages from the list.Step 2: Check other options
messages[:3]gets first 3,messages[3:]gets from 4th to end,messages[0]gets only first message.Final Answer:
short_term_memory = messages[-3:]-> Option DQuick Check:
Last 3 messages slice = messages[-3:] [OK]
Hint: Negative slice gets last items in list [OK]
Common Mistakes:
- Using positive slice for last items
- Selecting only one message instead of three
- Confusing start and end indices
3. Given the code below, what will be the output of
print(short_term_memory)?
messages = ['Hi', 'How are you?', 'I am fine', 'What about you?', 'Good!'] short_term_memory = messages[-2:] print(short_term_memory)
medium
Solution
Step 1: Understand list slicing with negative indices
messages[-2:]selects the last two items from the list.Step 2: Identify last two messages
The last two messages are 'What about you?' and 'Good!'.Final Answer:
['What about you?', 'Good!'] -> Option CQuick Check:
messages[-2:] = last two messages [OK]
Hint: Negative slice picks last elements [OK]
Common Mistakes:
- Selecting wrong slice range
- Confusing order of messages
- Printing only one message instead of two
4. The following code is intended to keep only the last 3 messages in short-term memory, but it has a bug. What is the bug?
messages = ['Hello', 'What is AI?', 'Tell me more', 'Thanks'] short_term_memory = messages[3:] print(short_term_memory)
medium
Solution
Step 1: Analyze the slice messages[3:]
This slice starts at index 3 and goes to the end, so it keeps only the last message 'Thanks'.Step 2: Compare with intended behavior
The goal was to keep last 3 messages, but this code keeps only one message.Final Answer:
It keeps only the last message instead of last three -> Option BQuick Check:
messages[3:] = last message only [OK]
Hint: Check slice start index carefully [OK]
Common Mistakes:
- Assuming slice keeps last 3 messages
- Expecting an error when none occurs
- Confusing slice start and end
5. You want an AI agent to remember the last 4 messages in a conversation to keep context. The conversation messages are stored in a list called
chat_history. Which code snippet correctly updates the short-term memory to always hold the last 4 messages after adding a new message new_msg?hard
Solution
Step 1: Add new message to chat_history first
Appendingnew_msgtochat_historyupdates the conversation.Step 2: Slice last 4 messages for short-term memory
Usingchat_history[-4:]gets the last 4 messages including the new one.Final Answer:
chat_history.append(new_msg) short_term_memory = chat_history[-4:]-> Option AQuick Check:
Append then slice last 4 messages [OK]
Hint: Append first, then slice last 4 [OK]
Common Mistakes:
- Slicing before appending new message
- Assigning new message alone as memory
- Slicing first 4 messages instead of last 4
