ποΈ 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.
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
-
Install dependencies:
pip install -r requirements.txt -
Set up your environment:
# Create a .env file with your Google AI API key echo "GOOGLE_API_KEY=your_api_key_here" > .env -
Run the agent:
# Start the Streamlit app streamlit run app.py -
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.dbfile 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)