Welcome to your first step into session management! This tutorial teaches you how to create an AI agent that can remember conversations within a single session using InMemorySessionService.
Sign in to save downloads to your library and vote.
Preview
π§ Tutorial 5.1: In-Memory Conversation Agent
Welcome to your first step into session management! This tutorial teaches you how to create an AI agent that can remember conversations within a single session using InMemorySessionService.
π― What You'll Learn
- InMemorySessionService: Basic session management for temporary conversations
- Session Creation: How to create and manage conversation sessions
- State Management: Storing and retrieving conversation context
- Event Tracking: Recording conversation history
- Multi-turn Conversations: Building agents that remember context
π§ Core Concept: In-Memory Sessions
InMemorySessionService stores session data in your computer's RAM (memory). This means:
- β Fast access - No database queries needed
- β Simple setup - No external dependencies
- β Temporary storage - Data is lost when the program stops
- β No persistence - Can't remember across program restarts
Perfect for:
- Development and testing
- Temporary conversations
- Prototyping memory features
- Single-session applications
π§ Key Components
1. InMemorySessionService
from google.adk.sessions import InMemorySessionService
2. Session Lifecycle
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β CREATE βββββΆβ USE βββββΆβ CLOSE β
β SESSION β β SESSION β β SESSION β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
3. Session Data Structure
{
"session_id": "unique_session_id",
"user_id": "user_identifier",
"state": {
"conversation_history": [...],
"user_preferences": {...},
"current_context": "..."
},
"events": [
{"type": "user_input", "content": "...", "timestamp": "..."},
{"type": "agent_response", "content": "...", "timestamp": "..."}
]
}
π Tutorial Overview
In this tutorial, we'll create a Personal Assistant Agent that:
- Remembers your name and preferences
- Tracks conversation history
- Provides personalized responses
- Demonstrates basic session management
π Project Structure
5_1_in_memory_conversation/
βββ README.md # This file - concept explanation
βββ requirements.txt # Dependencies
βββ agent.py # Main agent with session management
βββ app.py # Streamlit web interface
π― Learning Objectives
By the end of this tutorial, you'll understand:
- β How to create and manage sessions
- β How to store and retrieve conversation state
- β How to track conversation events
- β How to build multi-turn conversations
- β Basic session lifecycle management
π 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 the memory:
- Tell the agent your name: "My name is John"
- Ask about your preferences: "What do you know about me?"
- Have a conversation and see how it remembers context
π Code Walkthrough
Key Session Management Code:
# 1. Create session service
session_service = InMemorySessionService()
# 2. Create a new session
session = await session_service.create_session(
app_name="personal_assistant",
user_id="user123"
)
# 3. Update session state
await session_service.update_session_state(
session_id=session.session_id,
state={"user_name": "John", "preferences": ["travel", "music"]}
)
# 4. Add events to track conversation
await session_service.add_event(
session_id=session.session_id,
event_type="user_input",
content="My name is John"
)
π― Testing Your Agent
Try these conversation flows to test memory:
Flow 1: Personal Information
User: "My name is Alice"
Agent: "Nice to meet you, Alice! How can I help you today?"
User: "What's my name?"
Agent: "Your name is Alice! I remember you told me that."
Flow 2: Preferences
User: "I love pizza and hiking"
Agent: "Great! I'll remember that you love pizza and hiking."
User: "What are my interests?"
Agent: "Based on our conversation, you love pizza and hiking!"
Flow 3: Context Continuity
User: "I'm planning a trip"
Agent: "That sounds exciting! Since you mentioned hiking, would you like recommendations for hiking destinations?"
User: "Yes, where should I go?"
Agent: "Given your love for hiking, I'd recommend..."
π Next Steps
After completing this tutorial, you'll be ready for:
- Tutorial 5.2: Persistent Conversation - Learn database-based session storage
- Tutorial 5.3: Cloud Memory - Explore cloud-based memory solutions
π‘ Pro Tips
- Test Multi-turn Conversations: Have extended conversations to see memory in action
- Monitor Session State: Use the web interface to inspect what the agent remembers
- Experiment with State: Try storing different types of data in the session state
- Understand Limitations: Remember that in-memory sessions are temporary
π¨ Important Notes
- Data Loss: In-memory sessions are lost when you restart the application
- Single Process: Sessions only work within the same Python process
- Memory Usage: Large conversation histories will consume RAM
- Development Only: Use in-memory sessions for development, not production
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_1_in_memory_conversation_agent/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (51m ago)
- Refresh schedule
- Daily Β· 03:00 UTC
- Dedupe status
- Unique Β· deduped by (source, url)