Get started with Vertex AI Memory Bank - ADK

Source notebook

Repo path: gemini/agent-engine/memory/get_started_with_memory_bank_adk.ipynb · Open on GitHub · intermediate

Builds an ADK agent with Vertex AI Memory Bank for long-term user memory across sessions.

Summary

This notebook teaches how to create a Vertex AI Agent Engine instance, configure ADK with Vertex AI session and memory services, and run a Gemini-powered assistant with long-term memory. It demonstrates an information-gathering session, explicitly stores the completed session in Memory Bank, then starts a second session for the same user to recall facts from the first conversation.

Key code patterns

Project and Vertex setup

PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "TRUE"
os.environ["GOOGLE_CLOUD_PROJECT"] = PROJECT_ID
os.environ["GOOGLE_CLOUD_LOCATION"] = LOCATION
_ = vertexai.Client(project=PROJECT_ID, location=LOCATION)

Configures ADK and GenAI calls to use Vertex AI in the selected Google Cloud project and region.

Create Agent Engine

agent_engine = agent_engines.create()
print(f"Created Agent Engine: {agent_engine.resource_name}")

Creates the managed Agent Engine resource required for Vertex AI Memory Bank.

ADK agent with memory preload

agent = adk.Agent(
    model="gemini-2.5-flash",
    name="helpful_assistant",
    instruction="""You are a helpful assistant with perfect memory.""",
    tools=[adk.tools.preload_memory_tool.PreloadMemoryTool()],
)

Gives the agent a Gemini model and the built-in tool used to retrieve relevant memories.

Runner with session and memory services

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,
)

Connects the ADK runner to Vertex AI-backed session state and long-term memory.

Persist a completed session

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)

Explicitly adds conversation history to Memory Bank so memories can be generated and recalled later.

Models & APIs used

  • Models: gemini-2.5-flash
  • APIs / services: Vertex AI, Vertex AI API, Vertex AI Agent Engine, Vertex AI Memory Bank
  • SDKs / libraries: google-cloud-aiplatform, google-adk, vertexai, google-genai

When to use this

Use this pattern when building ADK agents that need personalized long-term memory across multiple user sessions.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Vertex AI API enabled.
  • Colab users may need to run notebook authentication before using Google Cloud resources.
  • PROJECT_ID must be supplied directly or through GOOGLE_CLOUD_PROJECT.
  • LOCATION defaults to us-central1 unless GOOGLE_CLOUD_REGION is set.
  • ADK is configured through GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION environment variables.
  • The notebook explicitly adds the completed session to memory; production apps may need a background process or hooks.
  • The second recall session uses the same USER_ID so memories are associated with the same user.
  • The Agent Engine is deleted at cleanup to avoid charges.

Best practices

  • Use a unique USER_ID to scope memories to a particular user.
  • Use PreloadMemoryTool so the agent can retrieve relevant user context.
  • Prompt the agent to personalize responses and naturally reference past conversations when relevant.
  • Retrieve the completed session before adding it to Memory Bank.
  • Delete the Agent Engine after experimentation to avoid charges.