Get started with Vertex AI Memory Bank - CrewAI

Source notebook

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

Integrates Vertex AI Memory Bank with CrewAI agents for persistent long-term conversational memory.

Summary

This notebook teaches how to provision a Vertex AI Agent Engine and use its Memory Bank as external long-term memory for CrewAI. It implements a custom CrewAI Storage class that saves events with generate_memories and retrieves user-scoped facts with retrieve_memories. The workflow configures Gemini 2.5 Flash, creates a personal assistant agent, runs a chat loop, verifies stored memories, and deletes the Agent Engine resource.

Key code patterns

Initialize Vertex AI and CrewAI environment

PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
os.environ["DEFAULT_VERTEXAI_PROJECT"] = PROJECT_ID
os.environ["DEFAULT_VERTEXAI_LOCATION"] = LOCATION
MODEL_NAME = "gemini-2.5-flash"
client = vertexai.Client(project=PROJECT_ID, location=LOCATION)

Sets the Google Cloud project, region, model, and Vertex AI client used by Agent Engine and CrewAI.

Create Agent Engine backend

agent_engine = client.agent_engines.create()
print(agent_engine.api_resource.name)

Creates the Agent Engine instance that provides Memory Bank APIs.

Save memory through CrewAI storage

event = {
    "content": {
        "parts": [{"text": value}],
        "role": metadata.get("role", "user"),
    }
}
client.agent_engines.generate_memories(
    name=self.agent_engine_name,
    direct_contents_source={"events": [event]},
    scope={"user_id": user_id},
    config={"wait_for_completion": True},
)

Translates CrewAI memory saves into Vertex AI Memory Bank memory generation.

Search user-scoped memories

retrieved_memories = client.agent_engines.retrieve_memories(
    name=self.agent_engine_name,
    scope={"user_id": user_id},
    similarity_search_params={"search_query": query, "top_k": limit},
)

Uses semantic search over memories scoped to a specific user.

Attach external memory to a crew

storage = VertexAIMemoryBankStorage(
    project=PROJECT_ID,
    location=LOCATION,
    agent_engine_name=agent_engine.api_resource.name,
    default_user_id=USER_ID,
)
memory = ExternalMemory(storage=storage)
crew = Crew(agents=[agent], tasks=[task], external_memory=memory)

Connects the custom Memory Bank storage to CrewAI’s external memory interface.

Models & APIs used

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

When to use this

Use this pattern when CrewAI agents need persistent, user-scoped memory across multiple conversations.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Vertex AI API enabled.
  • The notebook expects PROJECT_ID from the form input or GOOGLE_CLOUD_PROJECT environment variable.
  • CrewAI LLM setup loads service account credentials from SERVICE_ACCOUNT_FILE and requires Vertex AI User permission.
  • Default location is us-central1 unless GOOGLE_CLOUD_REGION is set.
  • The notebook deletes the Agent Engine at cleanup only when delete_engine is true.

Best practices

  • Validate project, location, and Agent Engine name before using custom storage.
  • Scope memories by user_id to keep user memory separated.
  • Use wait_for_completion when generating memories before retrieval is expected.
  • Create a new Vertex AI client per storage operation for async event loop management.
  • Delete the Agent Engine resource after the tutorial to avoid ongoing charges.