Bird
Raised Fist0
Agentic AIml~20 mins

Memory persistence and storage in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Memory persistence and storage
Problem:You have built an AI agent that learns from conversations but loses all memory after each session. This means it cannot remember past interactions, limiting its usefulness.
Current Metrics:Memory retention: 0% after session ends; agent responses lack context from previous sessions.
Issue:The AI agent does not persist memory between sessions, causing it to forget all learned information and user preferences.
Your Task
Implement memory persistence so the AI agent retains learned information and user context across sessions, improving continuity and relevance of responses.
You must use a simple file-based or database storage solution for memory persistence.
Do not change the core AI model architecture.
Ensure memory loading and saving does not significantly slow down the agent's response time.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
import json
import os

class MemoryAgent:
    def __init__(self, memory_file='memory.json'):
        self.memory_file = memory_file
        self.memory = self.load_memory()

    def load_memory(self):
        if os.path.exists(self.memory_file):
            with open(self.memory_file, 'r') as f:
                return json.load(f)
        else:
            return {}

    def save_memory(self):
        with open(self.memory_file, 'w') as f:
            json.dump(self.memory, f)

    def remember(self, key, value):
        self.memory[key] = value
        self.save_memory()

    def recall(self, key):
        return self.memory.get(key, None)

    def respond(self, user_input):
        # Simple example: remember user's name if mentioned
        if 'my name is' in user_input.lower():
            name = user_input.lower().split('my name is')[-1].strip()
            self.remember('user_name', name)
            return f'Nice to meet you, {name}!'
        elif 'what is my name' in user_input.lower():
            name = self.recall('user_name')
            if name:
                return f'Your name is {name}.'
            else:
                return 'I do not know your name yet.'
        else:
            return 'Tell me your name by saying "My name is ..."'

# Example usage
agent = MemoryAgent()
print(agent.respond('My name is Alice'))  # Stores name
print(agent.respond('What is my name?'))  # Recalls name

# After restarting the program, the agent will still remember Alice
Added a MemoryAgent class to handle loading and saving memory to a JSON file.
Implemented remember and recall methods to store and retrieve information.
Modified respond method to use persistent memory for user name.
Ensured memory is saved after each update to persist across sessions.
Results Interpretation

Before: Memory retention was 0%. The agent forgot all information after each session, resulting in generic responses.

After: Memory retention is 100% for stored data. The agent remembers user names and can recall them in later sessions, improving interaction quality.

Persisting memory outside the AI model allows agents to maintain context and user information across sessions, making interactions more natural and useful.
Bonus Experiment
Try implementing memory persistence using a lightweight database like SQLite instead of JSON files.
💡 Hint
Use Python's sqlite3 module to create a simple table for key-value pairs and update the load and save methods accordingly.

Practice

(1/5)
1. What is the main purpose of memory persistence in agentic AI systems?
easy
A. To keep important information available over time
B. To speed up the AI's calculations
C. To reduce the size of the AI model
D. To improve the AI's visual recognition

Solution

  1. Step 1: Understand memory persistence concept

    Memory persistence means saving data so it stays available even after the AI stops running.
  2. Step 2: Identify the purpose in AI context

    This helps AI remember important info across sessions, not just during one run.
  3. Final Answer:

    To keep important information available over time -> Option A
  4. Quick Check:

    Memory persistence = keep info over time [OK]
Hint: Memory persistence means saving info to use later [OK]
Common Mistakes:
  • Confusing persistence with faster processing
  • Thinking it reduces model size
  • Mixing it with unrelated AI tasks
2. Which of the following is the correct way to save data in a JSON file for memory persistence?
easy
A. open('memory.json', 'a') and load data with json.load()
B. open('memory.json', 'r') and write data
C. open('memory.json', 'x') and read data
D. open('memory.json', 'w') and dump data with json.dump()

Solution

  1. Step 1: Identify file mode for writing JSON

    To save data, we open the file in write mode ('w').
  2. Step 2: Use json.dump() to write data

    json.dump() writes Python data to the file in JSON format.
  3. Final Answer:

    open('memory.json', 'w') and dump data with json.dump() -> Option D
  4. Quick Check:

    Write mode + json.dump() = save JSON [OK]
Hint: Use 'w' mode and json.dump() to save JSON data [OK]
Common Mistakes:
  • Using 'r' mode to write data
  • Confusing json.load() with saving
  • Using 'x' mode incorrectly for reading
3. Given this code snippet for loading memory data, what will be the output if the file contains {'key': 'value'}?
import json
with open('memory.json', 'r') as f:
    data = json.load(f)
print(data['key'])
medium
A. key
B. value
C. None
D. Error: KeyError

Solution

  1. Step 1: Understand json.load() output

    json.load() reads JSON and converts it to a Python dictionary.
  2. Step 2: Access dictionary value by key

    data['key'] accesses the value 'value' stored under 'key'.
  3. Final Answer:

    value -> Option B
  4. Quick Check:

    data['key'] = 'value' [OK]
Hint: json.load() returns dict; access keys normally [OK]
Common Mistakes:
  • Expecting the key name as output
  • Confusing key with value
  • Assuming None or error without checking file content
4. This code tries to save data but causes an error. What is the problem?
import json
data = {'name': 'AI Agent'}
file = open('memory.json', 'r')
json.dump(data, file)
file.close()
medium
A. Missing import statement for json
B. json.dump() requires a string, not dict
C. File opened in read mode, cannot write
D. File not closed before writing

Solution

  1. Step 1: Check file open mode

    The file is opened with 'r' (read) mode, which does not allow writing.
  2. Step 2: Understand json.dump() needs writable file

    json.dump() writes data, so the file must be opened in 'w' or 'a' mode.
  3. Final Answer:

    File opened in read mode, cannot write -> Option C
  4. Quick Check:

    Write requires 'w' mode, not 'r' [OK]
Hint: Open file with 'w' to write JSON data [OK]
Common Mistakes:
  • Using 'r' mode when writing
  • Forgetting to close the file
  • Misunderstanding json.dump() input
5. You want your AI agent to remember user preferences across sessions using JSON storage. Which approach best ensures data is saved and loaded correctly?
hard
A. Save preferences with json.dump() in 'w' mode; load with json.load() in 'r' mode
B. Save preferences by appending text; load by reading lines manually
C. Save preferences in a plain text file without JSON; load by parsing strings
D. Save preferences only in memory variables without writing to file

Solution

  1. Step 1: Choose reliable save method

    json.dump() with 'w' mode writes structured data safely to file.
  2. Step 2: Choose matching load method

    json.load() with 'r' mode reads the structured data back correctly.
  3. Step 3: Avoid unreliable or volatile methods

    Appending text or plain text parsing risks errors; memory-only loses data after session.
  4. Final Answer:

    Save preferences with json.dump() in 'w' mode; load with json.load() in 'r' mode -> Option A
  5. Quick Check:

    Use json.dump/load with correct modes for persistence [OK]
Hint: Use json.dump/load with 'w' and 'r' modes for safe persistence [OK]
Common Mistakes:
  • Appending text without JSON format
  • Not saving data to file at all
  • Parsing plain text manually risking errors