0

🧠 Tutorial 5.1: In-Memory Conversation Agent

awesome-llm-apps5_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.

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

  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 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:

πŸ’‘ 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)