AI 101Intermediate15 min read

Build an AI Memory System for Chatbots with Gemini in 30 Minutes

Create a persistent memory layer for AI chatbots using Gemini, so conversations stay context-aware across sessions.

Build an AI Memory System for Chatbots with Gemini in 30 Minutes

Overview

Ever frustrated when your AI chatbot forgets everything after a conversation ends? Today you'll build a memory system for Gemini that persists key details between chats - perfect for customer support bots, personal assistants, or creative collaborators. We'll use Google's Gemini API to create a simple but powerful context-preserving layer.


What You Will Be Able To Do

  • Save important details from AI conversations automatically
  • Recall previous context when starting new chats
  • Build a foundation for long-term AI assistants
  • Implement this in any Gemini-powered application
  • Why This Matters

    Imagine you're a freelance designer using Gemini for client consultations. Today, you spend 10 minutes re-explaining the client's brand colors and preferences every new chat. With this system, Gemini will remember "Client X prefers minimalist designs in navy blue" automatically.

    For customer support teams, this means no more asking customers to repeat order numbers. For personal use, your AI journaling companion remembers your important life events. Context persistence is what separates toy chatbots from truly useful AI agents.

    Getting Started


    Install the required packages by running this in your terminal:

    
    pip install google-generativeai python-dotenv
    

    Step-by-Step Guide

    Step 1: Get Your Gemini API Key

    1. Go to https://aistudio.google.com/app/apikey
    2. Click "Create API key"
    3. Copy the key and store it securely
    4. Create a .env file in your project folder with:

    
    GEMINI_API_KEY=your_key_here
    

    Step 2: Set Up the Memory Database

    Create a new Python file called memory_bot.py with:

    python
    import json
    import os
    from pathlib import Path

    MEMORY_FILE = Path("ai_memory.json")

    def load_memory():
    if not MEMORY_FILE.exists():
    return {}
    with open(MEMORY_FILE, 'r') as f:
    return json.load(f)

    def save_memory(memory):
    with open(MEMORY_FILE, 'w') as f:
    json.dump(memory, f, indent=2)

    Step 3: Connect to Gemini with Memory

    Add this to your file:

    python
    import google.generativeai as genai
    from dotenv import load_dotenv

    load_dotenv()

    genai.configure(api_key=os.getenv('GEMINI_API_KEY'))

    model = genai.GenerativeModel('gemini-pro')

    def chat_with_memory(user_id, message):
    memory = load_memory()
    user_memory = memory.get(user_id, {"history": []})

    # Build context from memory
    context = ""
    if user_memory["history"]:
    context = "Previous conversation:\n" + "\n".join(
    f"{msg['role']}: {msg['content']}"
    for msg in user_memory["history"][-3:]
    ) + "\n\n"

    response = model.generate_content(
    context + f"User: {message}\nAI:"
    )

    # Update memory
    user_memory["history"].extend([
    {"role": "user", "content": message},
    {"role": "AI", "content": response.text}
    ])
    memory[user_id] = user_memory
    save_memory(memory)

    return response.text

    Step 4: Test Your Memory Bot

    Add this test code:

    python
    if __name__ == "__main__":
        print("Memory Bot activated! Type 'quit' to exit.")
        user_id = input("Enter your user ID: ").strip()
        
        while True:
            message = input("You: ").strip()
            if message.lower() == 'quit':
                break
            print("AI:", chat_with_memory(user_id, message))
    

    Step 5: Run and Experience Persistent Memory

    1. Run python memory_bot.py
    2. Enter a test user ID
    3. Have a conversation
    4. Quit and restart - your history will remain!

    Real-World Example

    Sarah, a nutrition coach, uses this to track client progress. When Client123 asks "What was my calorie target?", Gemini recalls from their last session: "Based on our 3/15 chat, your target is 1800kcal with 30% protein." Sarah saves 15 minutes per client by eliminating repetitive explanations while maintaining personalized service.

    Common Mistakes to Avoid


  • [Not sanitizing user IDs]: Always validate user IDs to prevent path traversal attacks

  • [Storing sensitive data]: Never store passwords, payment info, or PII in plain text

  • [Memory bloat]: Limit history length (we store last 3 messages) to avoid performance issues
  • Pro Tips

  • [Add memory summaries]: Periodically have Gemini summarize key facts to store instead of full history
  • [Implement forgetting]: Add a command like "/forget" to let users erase specific memories
  • [Multi-modal memory]: Extend this to store images or files using Gemini Pro Vision
  • Your Challenge


    Summary

    You've built what most AI tools lack - persistent memory. This foundation lets you create truly personalized AI experiences that evolve over time. Next, try connecting this to a Discord bot or adding automatic memory categorization.

    Learn AI, after work

    Track your progress, earn XP, and unlock more free tutorials in the AfterWork Bytes app.

    Open this tutorial in the app