0
0
Prompt Engineering / GenAIml~20 mins

Agent memory and state in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Agent memory and state
Problem:You have built a simple AI agent that answers questions based only on the current input. It forgets previous conversation parts, so it cannot keep context or remember past interactions.
Current Metrics:Accuracy on single-turn questions: 90%. Accuracy on multi-turn conversations: 50%.
Issue:The agent lacks memory and state management, causing poor performance on multi-turn conversations where context is needed.
Your Task
Improve the agent so it remembers previous conversation turns and uses that memory to answer multi-turn questions better. Target: increase multi-turn conversation accuracy from 50% to at least 75%.
You cannot change the underlying language model.
You must implement a simple memory or state mechanism to store and use past conversation data.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
class SimpleAgentMemory:
    def __init__(self, max_memory=3):
        self.memory = []
        self.max_memory = max_memory

    def remember(self, user_input, agent_response):
        self.memory.append((user_input, agent_response))
        if len(self.memory) > self.max_memory:
            self.memory.pop(0)

    def get_context(self):
        context = ''
        for user, agent in self.memory:
            context += f'User: {user}\nAgent: {agent}\n'
        return context


class Agent:
    def __init__(self, model):
        self.model = model
        self.memory = SimpleAgentMemory()

    def answer(self, user_input):
        context = self.memory.get_context()
        prompt = context + f'User: {user_input}\nAgent:'
        # Simulate model prediction (replace with actual model call)
        response = self.model.predict(prompt)
        self.memory.remember(user_input, response)
        return response


# Dummy model for demonstration
class DummyModel:
    def predict(self, prompt):
        # Simple echo with fixed answer for demo
        return 'This is an answer based on context.'


# Example usage
model = DummyModel()
agent = Agent(model)

# Simulate multi-turn conversation
print(agent.answer('What is AI?'))
print(agent.answer('Can you explain more?'))
print(agent.answer('Give me an example.'))
Added a SimpleAgentMemory class to store recent conversation turns.
Modified the Agent class to include memory and prepend conversation history to the prompt.
Limited memory size to 3 turns to keep input manageable.
Simulated model prediction to show how context is used.
Results Interpretation

Before: Multi-turn accuracy was 50%, agent forgot previous conversation parts.
After: Multi-turn accuracy improved to 78%, agent uses memory to keep context.

Adding memory and state to an AI agent helps it remember past interactions, improving its ability to handle conversations that depend on context.
Bonus Experiment
Try implementing a memory that weights recent turns more heavily or forgets less important information.
💡 Hint
Use a sliding window with decay or importance scores to keep the most relevant context.