Skip to content

Memory for ADK Agent

ADK agents use session services and memory services to manage conversational context. These services work together to maintain conversation state and enable long-term memory storage, allowing agents to remember previous interactions and persist context across sessions.

ADK Sessions documentation

Memory/Session configuration in ADK Agent form

Overview

ADK provides two complementary services for managing agent context:

Session Service

Manages the lifecycle of Session objects, which represent individual conversations. A session contains:

  • Events: Chronological sequence of messages and actions during the interaction
  • State: Temporary data relevant only during the current conversation (e.g., shopping cart items, user preferences)

Memory Service

Manages Memory, a long-term knowledge store that spans multiple sessions. Memory acts as a searchable knowledge base that the agent can query to recall information beyond the immediate conversation.

Supported Session Services: In Memory, Vertex AI, Database Supported Memory Services: In Memory, Vertex AI

Setup

During agent creation:

  1. Navigate to the Memory step
  2. The available options depend on your selected agent framework
  3. Configure your Session Service (required for conversation state):
  4. Choose from: In Memory, Vertex AI, or Database
  5. Fill in connection details if required
  6. Configure your Memory Service (optional, for long-term memory):
  7. Choose from: In Memory or Vertex AI
  8. Fill in connection details if required
  9. Click Next to continue

Success

Your ADK agent has memory enabled. Session state and messages will be persisted, and long-term memory will be available for your agent to improve its responses.




Session Service Options

Session services manage conversation state and events for individual sessions.

In Memory Session Service

The InMemorySessionService stores session data in the application's memory.

Characteristics:

  • Persistence: Data is lost when the application restarts
  • Performance: Fastest option, no I/O overhead
  • Use Cases: Development, testing, stateless workflows, quick prototyping

Configuration: No additional configuration required.

Vertex AI Session Service

The VertexAiSessionService utilizes Google Cloud's Vertex AI infrastructure for session management.

Characteristics:

  • Persistence: Cloud-native, persistent storage
  • Scalability: Handles high-volume, distributed deployments
  • Integration: Seamless integration with other Google Cloud services
  • Use Cases: Production deployments on Google Cloud, scalable applications

Configuration: Requires:

  • project_id: Google Cloud project ID
  • location: GCP region (e.g., us-central1)
  • reasoning_engine_app_name: Vertex AI Reasoning Engine application name

Database Session Service

The DatabaseSessionService connects to a relational database (PostgreSQL, MySQL, etc.) for persistent session storage using SQLAlchemy.

Characteristics:

  • Persistence: Robust SQL-based storage
  • Scalability: Supports multi-instance deployments
  • Reliability: Production-grade database features (transactions, backups)
  • Use Cases: Production deployments requiring SQL persistence, multi-instance setups

Configuration: Requires:

  • db_url: Database connection string (e.g., postgresql://user:pass@localhost:5432/dbname)

Memory Service Options

Memory services manage long-term knowledge storage that persists across multiple sessions.

In Memory Memory Service

The InMemoryMemoryService provides ephemeral memory storage.

Characteristics:

  • Persistence: Data is lost when the application restarts
  • Performance: Fast access, no external dependencies
  • Use Cases: Development, testing, scenarios where long-term memory isn't required

Configuration: No additional configuration required.

Vertex AI Memory Service

The VertexAiMemoryService provides cloud-backed memory with long-term storage using Vertex AI Memory Banks.

Characteristics:

  • Persistence: Long-term, persistent storage across sessions
  • Scalability: Cloud-native, handles large knowledge bases
  • Search: Semantic search capabilities for retrieving relevant memories
  • Use Cases: Production deployments requiring long-term memory, knowledge-intensive applications

Configuration: Requires:

  • project_id: Google Cloud project ID
  • location: GCP region
  • memory_bank_resource_id: Vertex AI Memory Bank resource ID

Best Practices

Session Service Best Practices

  • Use In Memory for local development - Simple and fast for testing, no setup required
  • Use Database for production - Reliable SQL-based persistence, supports multi-instance deployments
  • Use Vertex AI for Google Cloud production - Cloud-native, scalable, integrates with other GCP services
  • Configure session isolation - Ensure each conversation has a unique session ID to prevent state leakage

Memory Service Best Practices

  • Use In Memory for development - Fast iteration, no external dependencies
  • Use Vertex AI for production - Long-term persistence, semantic search capabilities
  • Ingest session data into memory - Periodically move important information from sessions to long-term memory
  • Use semantic search - Leverage Vertex AI's search capabilities to retrieve relevant memories

General Best Practices

  • Separate concerns - Use session service for conversation state, memory service for long-term knowledge
  • Monitor storage usage - Long-running sessions and large memory stores can consume significant resources
  • Backup strategies - Implement regular backups for production database and Vertex AI configurations

Troubleshooting

Session Service Troubleshooting

  1. Database connection errors:
  2. Verify the connection string format and credentials
  3. Test the connection independently using a database client
  4. Ensure the database server is accessible from the agent

  5. Vertex AI authentication:

  6. Verify Google Cloud credentials are properly configured
  7. Check that the service account has necessary permissions
  8. Ensure the project ID and location are correct

  9. Session not persisting:

  10. Verify the session service is properly initialized
  11. Check that session IDs are being used consistently
  12. Review session service logs for errors

Memory Service Troubleshooting

  1. Vertex AI Memory Bank:
  2. Verify the Memory Bank resource ID is correct
  3. Check that the Memory Bank exists in the specified project and location
  4. Ensure proper IAM permissions for accessing Memory Banks

  5. Memory not accessible:

  6. Verify the memory service is properly initialized
  7. Check memory service configuration matches your setup
  8. Review memory service logs for errors

General Troubleshooting

  1. Review logs: Look for session and memory-related errors in agent logs
  2. Check permissions: Ensure the agent has appropriate access to all storage resources
  3. Verify configuration: Double-check all connection strings, credentials, and resource IDs

Next Steps