# Practical and Useful Patterns with ADK
Memory enables "long-term recall" across sessions. Build agents that learn from past conversations and deeply understand each user 🧠
📌 **Title**: Memory
🔗 **URL**:
## 🧩 Overview
Memory manages long-term knowledge that is searchable across sessions. While State holds data for "the current conversation," Memory accumulates "knowledge gained from past conversations" and makes it searchable via natural language.
The two key APIs are:
- `add_session_to_memory` / `add_events_to_memory`: Add session or event content to memory
- `search_memory`: Search memory with natural language queries
Backends can use vector databases like Chroma, improving agent response quality through RAG (Retrieval-Augmented Generation) patterns.
## 🛠 How to Use
Import and instantiate `InMemoryMemoryService` from `google.adk.memory`. When a session ends, call `await memory_service.add_session_to_memory(session)` to store the session content in memory. To search, use `await memory_service.search_memory(app_name=..., user_id=..., query="previous network connection issues")` with a natural language query. Iterate over the returned results and access each `memory.content` to retrieve related past conversation content for use as agent context.
## 🏗 Practical Usage
**Customer support scenario:**
1. User reports "My network isn't connecting again"
2. Search Memory for "network connection issues"
3. Retrieve: "User had the same issue 3 days ago, resolved by router restart"
4. Agent suggests: "Was your previous issue resolved? If the symptoms are the same, try restarting your router"
This enables personalized support informed by past interaction history.
**Learning assistant agent:**
1. User asks "I don't understand derivatives"
2. Search Memory to find "This user prefers visual explanations" and "Last time they understood through concrete examples"
3. Provide graph-based, example-driven explanations
## 💡 Use Cases
- 🎧 Customer support: Informed responses based on past inquiry history. "Was your previous issue resolved?"
- 📚 Learning assistance: Remember user learning styles and comprehension levels to choose optimal teaching approaches
- 🏥 Health management: Understand trends from consultation history for continuous advice
- 🛒 Personal shopper: Remember purchase history and preferences for accurate product suggestions
## ⚠️ Caveats
- Storing personal information in Memory requires compliance with privacy policies and data protection regulations
- Vector DB search accuracy depends on embedding model quality. Choose an appropriate model
- Search costs increase as memory volume grows. Consider periodic cleanup or TTL (time-to-live) settings
- `InMemoryMemoryService` loses data on process restart. Use persistent backends like Chroma in production
✨ With Memory giving agents long-term recall, you build deeper user relationships and agents that get smarter with every interaction!
#
ADK# #
AIAgent#