Agent Platform Memory Bank gives agents that you build with the Agent Development Kit (ADK) long-term memory across sessions. In this quickstart, you run an agent that learns a user's preferred temperature in one session and recalls the preference in the next session.
This quickstart guides you through the following steps:
- Set up your environment.
- Create a Memory Bank instance.
- Run a memory-enabled ADK agent locally.
- Verify that the agent recalls the preference in a new session.
To call Memory Bank directly without ADK, see the Memory Bank API quickstart. To choose other memory tools, callbacks, or runtimes, see Use Memory Bank with ADK.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- Set up a Python development environment.
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
Required roles
To get the permissions that you need to create a Memory Bank instance and generate and retrieve memories, ask your administrator to grant you the following IAM roles on your project:
- Create or update a Memory Bank instance: Agent Platform User (`roles/aiplatform.user`)
- Read, write, or generate memories: Memory Bank User (`roles/aiplatform.memoryUser`)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Set up your environment
In your terminal, install ADK and the Agent Platform SDK:
pip install --upgrade "google-adk[gcp]" "google-cloud-agentplatform>=2.0.1"Set the environment variables that the scripts in this quickstart read:
export GOOGLE_GENAI_USE_ENTERPRISE=TRUE export GOOGLE_CLOUD_PROJECT=PROJECT_ID export GOOGLE_CLOUD_LOCATION=globalReplace PROJECT_ID with your Google Cloud project ID.
This quickstart uses the global endpoint. To use a regional or multi-regional location instead, replace
globalwith one of the supported locations. If you need customer-managed encryption keys (CMEK), use a regional or multi-regional location, because CMEK isn't available with the global endpoint.Run the rest of this quickstart in the same terminal session and directory so that the scripts can read these variables.
Create a Memory Bank instance
Create a file named
create_memory_bank.pywith the following code:import os import agentplatform client = agentplatform.Client( project=os.environ["GOOGLE_CLOUD_PROJECT"], location=os.environ["GOOGLE_CLOUD_LOCATION"], ) memory_bank = client.memory_banks.create( config={"display_name": "Memory Bank quickstart"} ) # Print only the instance ID, for example 1234567890. print(memory_bank.name.split("/")[-1])Run the script and save the instance ID in the
MEMORY_BANK_IDenvironment variable:export MEMORY_BANK_ID=$(python create_memory_bank.py)If this is your first Memory Bank instance, creation can take 1-2 minutes.
Confirm that the variable contains the instance ID:
echo $MEMORY_BANK_IDThe output is an ID such as
1234567890. If the output is empty or returns a401or403error, check your required roles and rungcloud auth application-default login. If a newglobalinstance returns a transient403error on first use, wait 30 seconds and run the script again.
Run a memory-enabled agent
The script in this section runs two sessions for the same user. In the first session, the user states a temperature preference. In the second session, the agent recalls that preference from Memory Bank.
Complete the following steps:
Create a directory named
memory_agent:mkdir memory_agentCreate a file named
memory_agent/agent.pywith the following code. The code reads your project, location, and instance ID from the variables you set in Set up your environment and Create a Memory Bank instance. You don't need to edit the code.import asyncio import os import agentplatform from google import adk from google.adk.agents.callback_context import CallbackContext from google.adk.memory import VertexAiMemoryBankService from google.adk.sessions import InMemorySessionService from google.adk.tools.preload_memory_tool import PreloadMemoryTool from google.genai import types PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] LOCATION = os.environ["GOOGLE_CLOUD_LOCATION"] MEMORY_BANK_ID = os.environ["MEMORY_BANK_ID"] # Memory Bank scopes memories to this app name and user ID. APP_NAME = "memory_agent" USER_ID = "quickstart_user" async def generate_memories_callback( callback_context: CallbackContext, ) -> None: # Send the latest turn to Memory Bank and wait until memory generation # finishes, so that the next session can retrieve the new memory. await callback_context.add_events_to_memory( events=callback_context.session.events[-5:], custom_metadata={"wait_for_completion": True}, ) return None root_agent = adk.Agent( model="gemini-3.5-flash", name="memory_agent", instruction=( "You are a vehicle voice agent that controls in-vehicle settings. " "Use what you know about the user's preferences. " "If you don't know the user's preferred temperature, ask for it. " "Limit responses to under 30 words." ), tools=[PreloadMemoryTool()], after_agent_callback=generate_memories_callback, ) memory_service = VertexAiMemoryBankService( project=PROJECT_ID, location=LOCATION, agent_engine_id=MEMORY_BANK_ID, ) session_service = InMemorySessionService() runner = adk.Runner( agent=root_agent, app_name=APP_NAME, session_service=session_service, memory_service=memory_service, ) async def send_message(session_id: str, text: str) -> None: print(f"User: {text}") message = types.Content(role="user", parts=[types.Part(text=text)]) async for event in runner.run_async( user_id=USER_ID, session_id=session_id, new_message=message ): if event.is_final_response() and event.content: print(f"Agent: {event.content.parts[0].text}") async def main() -> None: print("--- Session 1: share a preference ---") session = await session_service.create_session( app_name=APP_NAME, user_id=USER_ID ) await send_message(session.id, "Can you fix the temperature?") await send_message(session.id, "I'm comfortable at 71 degrees.") print("--- Session 2: recall the preference ---") session = await session_service.create_session( app_name=APP_NAME, user_id=USER_ID ) await send_message(session.id, "Fix the temperature.") print("--- Memories stored in Memory Bank ---") client = agentplatform.Client(project=PROJECT_ID, location=LOCATION) memory_bank_name = ( f"projects/{PROJECT_ID}/locations/{LOCATION}" f"/reasoningEngines/{MEMORY_BANK_ID}" ) results = client.memory_banks.memories.retrieve( name=memory_bank_name, scope={"app_name": APP_NAME, "user_id": USER_ID}, ) for retrieved_memory in results.page: print(f"* {retrieved_memory.memory.fact}") if __name__ == "__main__": asyncio.run(main())Run the script:
python memory_agent/agent.pyThe callback waits for memory generation to finish after each turn, so the memory is available when the second session starts.
Verify that the agent remembers
When the script finishes, it prints both conversations and the stored memory to your terminal. Compare that output with the following examples to confirm that the agent recalled the preference in a new session.
In the first session, the agent doesn't have any memories for the user, so it asks for a temperature:
--- Session 1: share a preference ---
User: Can you fix the temperature?
Agent: Sure. What temperature would you like?
User: I'm comfortable at 71 degrees.
Agent: Okay, I've set the temperature to 71 degrees.
In the second session, the agent retrieves the memory from Memory Bank and sets the temperature to 71 degrees without asking. The stored memory appears at the end of the output:
--- Session 2: recall the preference ---
User: Fix the temperature.
Agent: Okay, I've set the temperature to 71 degrees, as you prefer.
--- Memories stored in Memory Bank ---
* I like the temperature to be 71 degrees.
The wording of agent responses and memories varies depending on the models that the agent and Memory Bank use.
Optional: Chat with your agent in ADK Web
ADK Web is a browser-based interface for testing agents during development.
In the same terminal session, from the directory that contains the
memory_agentdirectory, run the following command:adk web --memory_service_uri=agentengine://$MEMORY_BANK_ID memory_agentIn your browser, open
http://localhost:8000?userId=quickstart_user.The
userIdquery parameter matches the user ID in the script, so the agent can retrieve the memory that the script created.Send the message
Fix the temperature.and confirm that the agent sets the temperature to 71 degrees without asking. The agent uses the preference that the script stored in Run a memory-enabled agent.To stop ADK Web, press Control+C in the terminal.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, you can delete the Google Cloud project. Otherwise, delete the individual resources:
Create a file named
clean_up.pywith the following code:import os import agentplatform project = os.environ["GOOGLE_CLOUD_PROJECT"] location = os.environ["GOOGLE_CLOUD_LOCATION"] memory_bank_id = os.environ["MEMORY_BANK_ID"] client = agentplatform.Client(project=project, location=location) client.memory_banks.delete( name=f"projects/{project}/locations/{location}/reasoningEngines/{memory_bank_id}", force=True, )Run the script to delete the Memory Bank instance:
python clean_up.pyDelete the local files that you created:
rm -r memory_agent create_memory_bank.py clean_up.py
What's next
Memory Bank API quickstart
Get started with the Memory Bank API to manage long-term memories.