0

πŸ—„οΈ Tutorial 5.2: Persistent Conversation Agent

awesome-llm-apps5_2_persistent_conversation_agent

Welcome to persistent session management! This tutorial teaches you how to create an AI agent that can remember conversations across multiple sessions using DatabaseSessionService with SQLite.

Sign in to save downloads to your library and vote.

Preview

πŸ—„οΈ Tutorial 5.2: Persistent Conversation Agent

Welcome to persistent session management! This tutorial teaches you how to create an AI agent that can remember conversations across multiple sessions using DatabaseSessionService with SQLite.

🎯 What You'll Learn

  • DatabaseSessionService: Persistent session storage with SQLite
  • Cross-Session Memory: Remembering conversations across program restarts
  • Database Management: Setting up and managing session databases
  • Data Persistence: Long-term storage of conversation history
  • Session Recovery: Retrieving previous conversations

🧠 Core Concept: Persistent Sessions

DatabaseSessionService stores session data in a SQLite database file. This means:

  • βœ… Persistent storage - Data survives program restarts
  • βœ… Cross-session memory - Remember conversations across sessions
  • βœ… Data integrity - ACID compliance with SQLite
  • βœ… Scalable - Can handle multiple users and sessions
  • ❌ Setup required - Need to initialize database
  • ❌ File-based - Limited to single machine

Perfect for:

  • Production applications
  • Multi-user systems
  • Long-term conversation history
  • Data analysis and insights

πŸ”§ Key Components

1. DatabaseSessionService

from google.adk.sessions import DatabaseSessionService

2. Database Structure

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    SQLITE DATABASE                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚  β”‚   SESSIONS  β”‚  β”‚    STATE    β”‚  β”‚   EVENTS    β”‚         β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€         β”‚
β”‚  β”‚ session_id  β”‚  β”‚ session_id  β”‚  β”‚ event_id    β”‚         β”‚
β”‚  β”‚ user_id     β”‚  β”‚ state_data  β”‚  β”‚ session_id  β”‚         β”‚
β”‚  β”‚ app_name    β”‚  β”‚ updated_at  β”‚  β”‚ event_type  β”‚         β”‚
β”‚  β”‚ created_at  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚ content     β”‚         β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β”‚ timestamp   β”‚         β”‚
β”‚                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3. Session Lifecycle with Persistence

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   CREATE    │───▢│   USE       │───▢│   CLOSE     β”‚
β”‚  SESSION    β”‚    β”‚  SESSION    β”‚    β”‚  SESSION    β”‚
β”‚  (DB)       β”‚    β”‚  (DB)       β”‚    β”‚  (DB)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                   β”‚                   β”‚
       β–Ό                   β–Ό                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Database   β”‚    β”‚  Database   β”‚    β”‚  Database   β”‚
β”‚  Created    β”‚    β”‚  Updated    β”‚    β”‚  Archived   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Tutorial Overview

In this tutorial, we'll create a Simple Persistent Agent that:

  • Remembers conversations across program restarts
  • Uses SQLite database for persistent storage
  • Demonstrates basic cross-session memory
  • Shows the difference from in-memory sessions

πŸ“ Project Structure

5_2_persistent_conversation/
β”œβ”€β”€ README.md              # This file - concept explanation
β”œβ”€β”€ requirements.txt       # Dependencies
β”œβ”€β”€ agent.py              # Main agent with database session management
β”œβ”€β”€ app.py                # Streamlit web interface
└── sessions.db           # SQLite database (created automatically)

🎯 Learning Objectives

By the end of this tutorial, you'll understand:

  • βœ… How to set up DatabaseSessionService with SQLite
  • βœ… How to create persistent sessions
  • βœ… How to retrieve conversation history across sessions
  • βœ… How to manage database connections and transactions
  • βœ… How to build agents that remember long-term

πŸš€ Getting Started

  1. Install dependencies:

    pip install -r requirements.txt
    
  2. Set up your environment:

    # Create a .env file with your Google AI API key
    echo "GOOGLE_API_KEY=your_api_key_here" > .env
    
  3. Run the agent:

    # Start the Streamlit app
    streamlit run app.py
    
  4. Test persistence:

    • Have a conversation with the agent
    • Close the browser/app
    • Restart the app
    • Continue the conversation - it will remember!

πŸ” Code Walkthrough

Key Database Session Management Code:

# 1. Create database session service
session_service = DatabaseSessionService(
    db_url="sqlite:///sessions.db"
)

# 2. Initialize database (creates tables)
await session_service.initialize()

# 3. Create or retrieve session
session = await session_service.get_session(
    app_name="demo",
    user_id="user123",
    session_id="session_user123"
)

# 4. Use with Runner for agent execution
async for event in runner.run_async(
    user_id=user_id,
    session_id=session_id,
    new_message=user_content
):
    # Handle response

🎯 Testing Your Agent

Try these persistence tests:

Test 1: Cross-Session Memory

Session 1:
User: "My name is Bob"
Agent: "Nice to meet you, Bob!"

Session 2 (after restart):
User: "What's my name?"
Agent: "Your name is Bob!"

Test 2: Interest Memory

Session 1:
User: "I love coding"
Agent: "That's great! Coding is a wonderful skill."

Session 2 (after restart):
User: "What do I love?"
Agent: "You love coding!"

Test 3: Database Verification

1. Have a conversation
2. Check for sessions.db file in project directory
3. Restart the app
4. Continue conversation - it remembers!

πŸ”— Next Steps

After completing this tutorial, you'll be ready for:

  • Tutorial 5.3: Cloud Memory - Learn cloud-based session storage
  • Advanced Database Patterns - Multi-user session management
  • Data Analytics - Analyzing conversation patterns

πŸ’‘ Pro Tips

  • Database Location: The SQLite file is created in your project directory
  • Backup Strategy: Consider backing up the sessions.db file
  • Performance: SQLite is fast for small to medium applications
  • Scaling: For large applications, consider PostgreSQL or cloud databases

🚨 Important Notes

  • Database File: A sessions.db file will be created in your project directory
  • Data Persistence: Conversations survive program restarts
  • File Permissions: Ensure write permissions in the project directory
  • Backup: The database file contains all conversation data - back it up!

Ingestion metadata

Source catalog
awesome-llm-apps
Repository
Shubhamsaboo/awesome-llm-apps Β· main
File path
ai_agent_framework_crash_course/google_adk_crash_course/5_memory_agent/5_2_persistent_conversation_agent/README.md
Last refreshed
7/24/2026, 3:00:13 AM (53m ago)
Refresh schedule
Daily Β· 03:00 UTC
Dedupe status
Unique Β· deduped by (source, url)