Chapter 59
Get started with Vertex AI Memory Bank - ADK
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.Get started with Vertex AI Memory Bank - ADK
Share to:
| Author(s) |
|---|
| Kimberly Milam, Ivan Nardini |
Overview
This tutorial demonstrates how to build an agent with long-term memory using the Google Agent Development Kit (ADK) and Vertex AI Memory Bank.
Vertex AI Memory Bank enables you to dynamically generate and store long-term memories from user conversations. This allows an agent to access personalized information across multiple sessions for a particular user, leading to more contextual and continuous interactions.
What you'll learn
By the end of this tutorial, you will be able to:
- Understand how long-term memory enhances agent capabilities.
- Create a Vertex AI Agent Engine instance to use Memory Bank.
- Build an ADK agent that uses the
PreloadMemoryTooltool to retrieve information. - See how the agent stores conversation history and recalls relevant facts from past sessions.
- Build more sophisticated, context-aware Agents.
Why this is important
Traditional LLM-based agents often lack the ability to recall information from previous interactions, treating each conversation as a new one. This "amnesia" prevents them from maintaining context over time. Long-term memory, like that provided by Vertex AI Memory Bank, allows agents to:
- Maintain context over extended periods.
- Personalize interactions based on user history.
- Build a deeper understanding of user preferences and needs.
Google's ADK provides a robust framework for building agents, and by extending it with services like Memory Bank, you can unlock a new level of sophistication.
Get started
Install Google Gen AI SDK and other required packages
%pip install --upgrade --quiet "google-cloud-aiplatform>=1.100.0" "google-adk>=1.5.0"Authenticate your notebook environment (Colab only)
If you're running this notebook on Google Colab, run the cell below to authenticate your environment.
# import sys
# if "google.colab" in sys.modules:
# from google.colab import auth
# auth.authenticate_user()Set Google Cloud project information
To get started using Vertex AI, you must have an existing Google Cloud project and enable the Vertex AI API.
Learn more about setting up a project and a development environment.
# Use the environment variable if the user doesn't provide Project ID.
import os
import uuid
import vertexai
# Project configuration
PROJECT_ID = "[your-project-id]" # @param {type: "string", placeholder: "[your-project-id]", isTemplate: true}
if not PROJECT_ID or PROJECT_ID == "[your-project-id]":
PROJECT_ID = str(os.environ.get("GOOGLE_CLOUD_PROJECT"))
if not PROJECT_ID:
raise ValueError("Project ID not found. Please set it in the form above or as the GOOGLE_CLOUD_PROJECT environment variable.")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
# Set environment variables required for ADK
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "TRUE"
os.environ["GOOGLE_CLOUD_PROJECT"] = PROJECT_ID
os.environ["GOOGLE_CLOUD_LOCATION"] = LOCATION
# Agent configuration
MODEL_NAME = "gemini-2.5-flash"
USER_ID = f"user_{uuid.uuid4()}"
print(f"Project: {PROJECT_ID}")
print(f"Location: {LOCATION}")
print(f"Session User ID: {USER_ID}")
# Initialize Vertex AI client
_ = vertexai.Client(
project=PROJECT_ID,
location=LOCATION,
)Import libraries
Import the Python libraries required for this tutorial, including components from the ADK, Vertex AI SDK, and standard libraries.
import uuid
import vertexai
from vertexai import agent_engines
from google import adk
from google.adk.memory import VertexAiMemoryBankService
from google.adk.sessions import VertexAiSessionService
from google.genai import typesHelper functions
We will define a helper function to simplify running the agent and capturing its response. This will make our interaction loop cleaner and easier to read.
def run_single_turn(query, session, user_id):
"""Run a single conversation turn."""
content = types.Content(role="user", parts=[types.Part(text=query)])
events = runner.run(user_id=user_id, session_id=session, new_message=content)
response_content = None
for event in events:
if event.is_final_response():
response_content = event.content.parts[0].text
return response_content
def chat_loop(session, user_id) -> None:
"""Main chat interface loop."""
print("\nStarting chat. Type 'exit' or 'quit' to end.")
print("Every message will be automatically stored in memory.\n")
while True:
user_input = input("\nYou: ")
if user_input.lower() in ["quit", "exit", "bye"]:
print("\nAssistant: Thank you for chatting. Have a great day!")
break
response = run_single_turn(user_input, session, user_id)
if response:
print(f"\nAssistant: {response}")Creating the Agent Engine with Memory Bank
Vertex AI Memory Bank is a component of the Vertex AI Agent Engine, a managed service that allows developers to deploy, manage, and scale AI agents.
To use Memory Bank, you first need to create or get an existing Agent Engine instance. This provides the necessary APIs to store and retrieve memories associated with specific users.
agent_engine = agent_engines.create()
print(f"Created Agent Engine: {agent_engine.resource_name}")Building an Agent with Memory Bank using ADK
Now we will construct the core components of our memory-enabled agent.
Define the Agent
Define the ADK agent with a name, instructions (prompt), and specify the load_memory tool. This built-in ADK tool enables the agent to call our VertexAiMemoryBankService to search for information. The agent's prompt is crucial, as it instructs the LLM when and how to use this tool to recall user-specific context.
agent = adk.Agent(
model=MODEL_NAME,
name="helpful_assistant",
instruction="""You are a helpful assistant with perfect memory.
Instructions:
- Use the context to personalize responses
- Naturally reference past conversations when relevant
- Build upon previous knowledge about the user
- If using semantic search, the memories shown are the most relevant to the current query""",
tools=[adk.tools.preload_memory_tool.PreloadMemoryTool()],
)Configure the ADK Runner
The Runner orchestrates the interaction between the user, the agent, and the various services. We will configure it with our agent and the VertexAiSessionService and VertexAiMemoryBankService to manage session state and long-term memory.
Notice that the
VertexAiMemoryBankServicereleased in ADK, you can also use a local session service.
app_name = "my_agent_" + str(uuid.uuid4())[:6]
agent_engine_id = agent_engine.name
memory_bank_service = VertexAiMemoryBankService(
project=PROJECT_ID, location=LOCATION, agent_engine_id=agent_engine_id
)
session_service = VertexAiSessionService(
project=PROJECT_ID, location=LOCATION, agent_engine_id=agent_engine_id
)
runner = adk.Runner(
agent=agent,
app_name=app_name,
session_service=session_service,
memory_service=memory_bank_service,
)Interacting with the Agent - Information gathering session
Let's begin our first conversation. In this session, we will provide the agent with specific pieces of information that we expect it to remember later.
The process is as follows:
- Create a new ADK session for the user.
- Send a series of messages to the agent.
- After the conversation, retrieve the completed session data.
- Explicitly add the session to our Memory Bank, which triggers the memory generation process.
In a production application, adding the session to memory might be handled by a background process or through hooks in a custom Runner or SessionService. For this tutorial, we perform this step explicitly to clearly illustrate the process.
session1 = await runner.session_service.create_session(
app_name=app_name,
user_id=USER_ID,
)Try these example phrases:
You: Hi, I work as an agent engineer
You: I love hiking and have a dog named Max
You: I'm working on a recommendation system project
You: Byechat_loop(session1.id, USER_ID)Add the session to memory.
completed_session = await runner.session_service.get_session(
app_name=app_name, user_id=USER_ID, session_id=session1.id
)
await memory_bank_service.add_session_to_memory(completed_session)Interacting with the Agent - Memory Recall session
Now, we will start a new session with the same user. This time, we will ask questions that require the agent to recall information from the first session. This is where the load_memory tool and our VertexAIMemoryBankService.search_memory() method will be invoked.
session2 = await runner.session_service.create_session(
app_name=agent_engine.name,
user_id=USER_ID,
)Try these example phrases:
You: What do you remember about me?
You: What is my dog's name?
You: Byechat_loop(session2.id, USER_ID)Cleaning up
To avoid charges, delete the agent engine when you're done experimenting.
delete_engine = True
if delete_engine:
agent_engines.delete(resource_name=agent_engine.resource_name, force=True)