Before you work with Vertex AI Agent Engine Memory Bank, you must set up your environment. Note that although Memory Bank is part of Agent Engine, you don't need to deploy your code to Agent Engine Runtime to use Memory Bank.
Set up your Google Cloud project
Every project can be identified in two ways: the project number or the project
ID. The PROJECT_NUMBER
is automatically created when you
create the project, whereas the PROJECT_ID
is created by you,
or whoever created the project. To set up a project:
- 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Get the required roles
To get the permissions that
you need to use Vertex AI Agent Engine,
ask your administrator to grant you the
Vertex AI User (roles/aiplatform.user
)
IAM role on your project.
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.
If you're making requests to Memory Bank from an agent deployed on Google Kubernetes Engine or Cloud Run, make sure that your service account has the necessary permissions. The Reasoning Engine Service Agent already has the necessary permissions to read and write memories, so outbound requests from Agent Engine Runtime should already have permission to access Memory Bank.
Set up your environment
This section assumes that you have set up a Python development environment, or are using a runtime with a Python development environment (such as Colab).
Install libraries
Install the Vertex AI SDK:
pip install google-cloud-aiplatform>=1.104.0
Authentication
Authentication instructions depend on whether you're using Vertex AI in express mode:
If you're not using Vertex AI in express mode, follow the instructions at Authenticate to Vertex AI.
If you're using Vertex AI in express mode, set up authentication by setting the API key in the environment:
os.environ["GOOGLE_API_KEY"] = "API_KEY"
Set up a Vertex AI SDK client
Run the following code to set up a Vertex AI SDK client:
import vertexai
client = vertexai.Client(
project="PROJECT_ID",
location="LOCATION",
)
where
PROJECT_ID
is your project ID.LOCATION
is one of the supported regions for Memory Bank.
Configure your Agent Engine instance for Memory Bank
To get started with Memory Bank, you first need an Agent Engine instance.
You can do one of the following:
Create or update an Agent Engine instance: When you create or update an instance, you can override Agent Engine's defaults to make the following modifications to the instance:
Set the configuration for how Memory Bank generates and manages memories.
Deploy your agent to Agent Engine Runtime.
Use an existing instance
If you don't need to modify an existing Agent Engine instance, run the following to configure the instance for Memory Bank:
agent_engine = client.agent_engines.get(name="AGENT_ENGINE_NAME")
Replace the following:
- AGENT_ENGINE_NAME: The name of the Agent Engine. It should be in the format
projects/.../locations/.../reasoningEngines/...
. See the supported regions for Memory Bank.
You can use the instance in any environment, including Google Kubernetes Engine and Cloud Run. To get started, you need the Agent Engine name that identifies the Memory Bank and sufficient permission to call Memory Bank.
Create or update an instance
Create
Memory Bank is enabled by default when you create an Agent Engine instance. Creating a new Agent Engine without a Runtime should only take a few seconds.
agent_engine = client.agent_engines.create()
You can also override Agent Engine's defaults when creating an Agent Engine instance to make the following modifications:
Set the configuration for how Memory Bank generates and manages memories.
Deploy your agent to Agent Engine Runtime.
agent_engine = client.agent_engines.create( # Optional. Set this argument if you want to deploy to Agent Engine Runtime. agent_engine=..., # Optional. Set this argument if you want to change the Memory Bank configuration. config=... )
New instances are empty until you create or generate memories.
You need the name of your Agent Engine to read or write memories:
agent_engine_name = agent_engine.api_resource.name
Update
You can update an existing Agent Engine instance if you want to update the Agent Engine while still persisting the memories that were stored in the instance. You can make updates like changing the Memory Bank configuration or deploying your agent to Agent Engine Runtime.
agent_engine = client.agent_engines.update(
# If you have an existing AgentEngine, you can access the name using `agent_engine.api_resource.name`.
name="AGENT_ENGINE_NAME",
# Optional. Set this argument if you want to deploy to Agent Engine Runtime.
agent_engine=...,
# Optional. Set this argument if you want to change the Memory Bank configuration.
config=...
)
Replace the following:
- AGENT_ENGINE_NAME: The name of the Agent Engine. It should be in the format
projects/.../locations/.../reasoningEngines/...
. See the supported regions for Memory Bank.
Set your Memory Bank configuration
You can configure your Memory Bank to customize how memories are generated and managed. If the configuration is not provided, then Memory Bank uses the default settings for each type of configuration.
The Memory Bank configuration is set when creating or updating your Agent Engine instance:
client.agent_engines.create(
...,
config={
"context_spec": {
"memory_bank_config": memory_bank_config
}
}
)
# Alternatively, update an existing Agent Engine's Memory Bank config.
agent_engine = client.agent_engines.update(
name=agent_engine.api_resource.name,
config={
"context_spec": {
"memory_bank_config": memory_bank_config
}
}
)
You can configure the following settings for your instance:
- Similarity search configuration: Configures which embedding model is used for similarity search. Defaults to
text-embedding-005
. - Generation configuration: Configures which LLM is used for memory generation. Defaults to
gemini-2.0-flash-001
. - TTL configuration: Configures how TTL is automatically set for created or updated memories. Defaults to no TTL.
Similarity search configuration
The similarity search configuration controls which embedding model is used by your instance for similarity search. Similarity search is used for identifying which memories should be candidates for consolidation and for similarity search-based memory retrieval. If this configuration is not provided, Memory Bank uses text-embedding-005
as the default model.
If you expect user conversations to be in non-English languages, use a model that supports multiple languages, such as gemini-embedding-001
or text-multilingual-embedding-002
, to improve retrieval quality.
Dictionary
memory_bank_config = {
"similarity_search_config": {
"embedding_model": "EMBEDDING_MODEL",
}
}
Class-based
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfig as MemoryBankConfig
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfigSimilaritySearchConfig as SimilaritySearchConfig
memory_bank_config = MemoryBankConfig(
similarity_search_config=SimilaritySearchConfig(
embedding_model="EMBEDDING_MODEL"
)
)
Replace the following:
- EMBEDDING_MODEL: The Google text embedding model to use for similarity search, in the format
projects/{project}/locations/{location}/publishers/google/models/{model}
.
Generation configuration
The generation configuration controls which LLM is used for generating memories, including extracting memories and consolidating new memories with existing memories.
Memory Bank uses gemini-2.0-flash-001
as the default model.
Dictionary
memory_bank_config = {
"generation_config": {
"model": "LLM_MODEL",
}
}
Class-based
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfig as MemoryBankConfig
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfigGenerationConfig as GenerationConfig
memory_bank_config = MemoryBankConfig(
generation_config=GenerationConfig(
model="LLM_MODEL"
)
)
Replace the following:
- LLM_MODEL: The Google LLM model to use for extracting and consolidating memories, in the format
projects/{project}/locations/{location}/publishers/google/models/{model}
.
Time to live (TTL) configuration
The TTL configuration controls how Memory Bank should dynamically set memories' expiration time. After their expiration time elapses, memories won't be available for retrieval and will be deleted.
If the configuration is not provided, expiration time won't be dynamically set for created or updated memories, so memories won't expire unless their expiration time is manually set.
There are two options for the TTL configuration:
Default TTL: The TTL will be applied to all operations that create or update a memory, including
UpdateMemory
,CreateMemory
, andGenerateMemories
.Dictionary
memory_bank_config = { "ttl_config": { "default_ttl": f"TTLs" } }
Class-based
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfig as MemoryBankConfig from vertexai.types import ReasoningEngineContextSpecMemoryBankConfigTtlConfig as TtlConfig memory_bank_config = MemoryBankConfig( ttl_config=TtlConfig( default_ttl=f"TTLs" ) )
Replace the following:
- TTL: The duration in seconds for the TTL. For updated memories, the newly calculated expiration time (now + TTL) will overwrite the Memory's previous expiration time.
Granular (per-operation) TTL: The TTL is calculated based on which operation created or updated the Memory. If not set for a given operation, then the operation won't update the Memory's expiration time.
Dictionary
memory_bank_config = { "ttl_config": { "granular_ttl": { "create_ttl": f"CREATE_TTLs", "generate_created_ttl": f"GENERATE_CREATED_TTLs", "generate_updated_ttl": f"GENERATE_UPDATED_TTLs" } } }
Class-based
from vertexai.types import ReasoningEngineContextSpecMemoryBankConfig as MemoryBankConfig from vertexai.types import ReasoningEngineContextSpecMemoryBankConfigTtlConfig as TtlConfig from vertexai.types import ReasoningEngineContextSpecMemoryBankConfigTtlConfigGranularTtlConfig as GranularTtlConfig memory_bank_config = MemoryBankConfig( ttl_config=TtlConfig( granular_ttl_config=GranularTtlConfig( create_ttl=f"CREATE_TTLs", generate_created_ttl=f"GENERATE_CREATED_TTLs", generate_updated_ttl=f"GENERATE_UPDATED_TTLs", ) ) )
Replace the following:
- CREATE_TTL: The duration in seconds for the TTL for memories created using
CreateMemory
. - GENERATE_CREATED_TTL: The duration in seconds for the TTL for memories created using
GeneratedMemories
. - GENERATE_UPDATED_TTL: The duration in seconds for the TTL for memories updated using
GeneratedMemories
. The newly calculated expiration time (now + TTL) will overwrite the Memory's previous expiration time.
- CREATE_TTL: The duration in seconds for the TTL for memories created using
Deploy your agent with memory to Agent Engine
Although Memory Bank can be used in any runtime, you can also use Memory Bank with Agent Engine Runtime to read and write memories from your deployed agent.
To deploy an agent with Memory Bank on Vertex AI Agent Engine Runtime, first set up your environment for Agent Engine runtime. Then, prepare your agent to be deployed on Agent Engine Runtime with memory integration. Your deployed agent should make calls to read and write memories as needed.
AdkApp
If you're using the Agent Engine Agent Development Kit template, the agent uses the VertexAiMemoryBankService
by default when deployed to Agent Engine Runtime. This means that the ADK Memory tools read memories from Memory Bank.
from google.adk.agents import Agent
from vertexai.preview.reasoning_engines import AdkApp
# Develop an agent using the ADK template.
agent = Agent(...)
adk_app = AdkApp(
agent=adk_agent,
...
)
# Deploy the agent to Agent Engine Runtime.
agent_engine = client.agent_engines.create(
agent_engine=adk_app,
config={
"staging_bucket": "STAGING_BUCKET",
"requirements": ["google-cloud-aiplatform[agent_engines,adk]"],
# Optional.
**context_spec
}
)
# Update an existing Agent Engine to add or modify the Runtime.
agent_engine = client.agent_engines.update(
name=agent_engine.api_resource.name,
agent_engine=adk_app,
config={
"staging_bucket": "STAGING_BUCKET",
"requirements": ["google-cloud-aiplatform[agent_engines,adk]"],
# Optional.
**context_spec
}
)
Replace the following:
- STAGING_BUCKET: Your Cloud Storage bucket to use for staging your Agent Engine Runtime.
For more information about using Memory Bank with ADK, refer to the Quickstart with Agent Development Kit.
Custom agent
You can use Memory Bank with your custom agent deployed on Agent Engine Runtime. In this case, your agent should orchestrate calls to Memory Bank to trigger memory generation and memory retrieval calls.
If you want to use the same Agent Engine instance for both Memory Bank and the Agent Engine Runtime, you can read the environment variables GOOGLE_CLOUD_PROJECT
, GOOGLE_CLOUD_LOCATION
,GOOGLE_CLOUD_AGENT_ENGINE_ID
to infer the Agent Engine name from the environment:
project = os.environ.get("GOOGLE_CLOUD_PROJECT")
location = os.environ.get("GOOGLE_CLOUD_LOCATION")
agent_engine_id = os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID")
agent_engine_name = f"projects/{project}/locations/{location}/reasoningEngines/{agent_engine_id}"