0
0
Agentic AIml~5 mins

Short-term memory (conversation context) in Agentic AI

Choose your learning style9 modes available
Introduction

Short-term memory helps AI remember recent parts of a conversation. This makes the AI respond in a way that feels natural and connected.

When chatting with a virtual assistant that needs to recall what you just said.
In customer support bots that must keep track of recent questions.
During interactive storytelling where the AI remembers story details.
In tutoring systems that recall recent student answers.
When building chatbots that maintain context over multiple messages.
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
This keeps the last two messages to form the context.
Agentic AI
conversation_memory = []
conversation_memory.append('Hello!')
conversation_memory.append('How are you?')
context = ' '.join(conversation_memory[-2:])
Now the context includes three recent messages for better understanding.
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)
OutputSuccess
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.