ADK Cross-Session Memory Bank Integration
Overview
This skill wires up and directly operates Vertex AI Memory Bank: the
managed service that lets an ADK agent remember user preferences,
terminology, and facts across separate conversations instead of forgetting
everything when a session ends. It covers both layers real integrations
need — the ADK tool layer (PreloadMemoryTool, LoadMemoryTool,
after_agent_callback) an agent uses automatically, and the underlying
Memory Bank API (vertexai.Client().agent_engines.memories.*) this skill's
own scripts call directly to seed or inspect memories outside of a live
agent turn.
Grounded in the real cross-session-memory ADK sample
(github.com/google/adk-samples/tree/main/core/python/cross-session-memory)
and the Agent Platform Memory Bank docs — not invented from memory. Every
API call this skill's scripts make traces to a documented example; where a
detail (like semantic-similarity retrieval's exact parameter) wasn't
directly confirmed, the reference files say so explicitly rather than
guessing.
Prerequisites
For real (non-dry-run) use: pip install google-adk google-cloud-aiplatform[agent-engines] and gcloud auth application-default login, plus a GCP project with Agent Engine access.
A --memory-bank-name (an Agent Engine resource:
projects/P/locations/L/reasoningEngines/R) — either an existing
instance, or one the user needs to create first
(client.agent_engines.create(), see
references/vertex_memory_bank_api.md).
None of the above is required for --dry-run — use it to validate seed
files and preview requests with no GCP access at all.
Data classification: Confidential. Memories may contain personal
preferences, identity facts, or internal terminology; restrict scopes to
authorized users and do not expose retrieved output outside its intended
audience.
Workflow
Step 1: Decide ADK-tool wiring vs. direct API scripting
If the goal is "make my ADK agent remember things across sessions" (the
common case): wire PreloadMemoryTool() into the agent's tools and
after_agent_callback=generate_memories_callback per
references/vertex_memory_bank_api.md's PreloadMemoryTool section — this
is two lines in any existing ADK agent, no coupling to this skill's
scripts required.
If the goal is "seed memories before the first conversation" or "check
what's currently stored" (testing, onboarding, debugging recall): use this
skill's scripts/preload_memory.py / scripts/load_memory.py directly
(Steps 2-3 below).
Step 2: Seed memories with preload_memory.py
Build a facts file matching assets/memory_bank_schema.json (see
assets/sample_knowledge_base.md for a worked example spanning all four
managed topics plus a custom-topic pattern), then:
scripts/preload_memory.py --facts-file seed.json --dry-run
Fix any validation errors, THEN run for real:
scripts/preload_memory.py --memory-bank-name projects/P/locations/L/reasoningEngines/R \
--facts-file seed.json --scope-json '{"user_id":"123"}' --confirm-write
Default mode is consolidation-aware (GenerateMemories); only pass
--no-consolidate when guaranteed-distinct records matter more than
avoiding duplicates (e.g. a disposable test fixture) — see the script's
own docstring for why this is the exception, not the default.
Never seed a real user's data this way without their knowledge — see
the closing note in assets/sample_knowledge_base.md.
--confirm-write is required for every real persistent write. It confirms
the operator reviewed a successful dry run; never add it on the user's
behalf or bypass it with a direct API call.
Step 3: Verify or inspect with load_memory.py
scripts/load_memory.py --memory-bank-name projects/P/locations/L/reasoningEngines/R \
--scope-json '{"user_id":"123"}' --as-prompt
--as-prompt renders exactly the <MEMORIES> block PreloadMemoryTool
would inject — use this to confirm a seed actually took effect, or to
debug "why doesn't my agent know X" by checking whether the fact is even
in the scope being queried. Use --dry-run --local-file seed.json to
sanity-check scope-matching and rendering before touching the real API.
Step 4: Read the domain references before answering "why" questions
references/vertex_memory_bank_api.md — PreloadMemoryTool vs.
LoadMemoryTool, the write path, session-state-vs-memory boundary, the
full API primitive table, memory topics, local-vs-real Memory Bank.
references/long_term_memory_patterns.md — scope design, Memory Bank
vs. RAG, perspective consistency, metadata merge strategies, and the
Cloud-Run-reuses-a-stale-instance gotcha that silently breaks topic
config changes.
Do not answer a "why isn't this working" question from general LLM
knowledge about memory systems — check these files first; several of the
gotchas here (async consolidation, Cloud Run engine reuse) are
non-obvious and specific to this API.
Examples
Example 1: New ADK agent, wire memory from scratch
Input: "Add cross-session memory to my ADK agent so it remembers user
preferences."
Expected output / behavior: add PreloadMemoryTool() to tools and
after_agent_callback=generate_memories_callback per Step 1 — no script
invocation needed for this case. Point to
references/vertex_memory_bank_api.md for the consolidation-is-async
gotcha so the user doesn't expect same-session recall of something just
said.
Example 2: Seed a team glossary before any real conversations happen
Input: "I want the support agent to already know our internal jargon
before it talks to anyone."
Expected output / behavior: build a facts file from
assets/sample_knowledge_base.md's glossary section, --dry-run it,
then run preload_memory.py for real with an appropriate scope (likely a
team_id-based scope per references/long_term_memory_patterns.md, not
per-user, since the glossary applies to everyone on the team). Verify with
load_memory.py --as-prompt.
Error Handling
preload_memory.py --facts-file fails schema validation: fix the
reported field(s) against assets/memory_bank_schema.json before
retrying — never bypass validation by hand-editing the request past
what the script checked.
A real GenerateMemories call returns 0 generated memories: this is
valid, not a bug — Memory Bank only persists information matching a
configured topic (see references/vertex_memory_bank_api.md); check the
target instance's topic configuration before assuming the call failed.
A seeded fact doesn't show up in load_memory.py right after seeding:
check you queried the same scope you seeded with (scope must match
exactly, not just conceptually overlap) before assuming seeding failed.
Memory Bank topic/config changes don't take effect after redeploying to
Cloud Run: see the "Cloud Run still needs an Agent Engine" /
reused-as-is gotcha in references/long_term_memory_patterns.md — a
pre-existing engine instance is not automatically reconfigured.
vertexai import fails when not using --dry-run: tell the user to
pip install google-cloud-aiplatform[agent-engines] — don't silently
fall back to dry-run behavior, since that would hide that nothing was
actually written.
A user asks for semantic/similarity-search retrieval by free-text query:
load_memory.py deliberately does not implement this (the exact API
parameter wasn't directly confirmed while building this skill) — point
them at the live "Fetch memories" doc rather than guessing a parameter
name that may not match the current API.
Reference Files
scripts/preload_memory.py: seeds Memory Bank from a facts file
(direct_memories_source) or a conversation transcript
(direct_contents_source); --dry-run validates and previews with no
GCP call — run in Step 2.
scripts/load_memory.py: scope-based retrieve, revision history, and
<MEMORIES>-block rendering; --dry-run --local-file for offline
testing — run in Step 3.
references/vertex_memory_bank_api.md: ADK tool layer vs. API layer,
PreloadMemoryTool/LoadMemoryTool, the write callback, session-state
boundary, full API primitive table, memory topics — read in Step 1 and
Step 4.
references/long_term_memory_patterns.md: scope design, Memory Bank
vs. RAG, perspective consistency, metadata merge strategy, deploy-path
gotchas — read in Step 4 and whenever a seeded fact isn't behaving as
expected.
assets/memory_bank_schema.json: canonical JSON Schema for facts-file
entries — both scripts validate against this; the single source of
truth for the seed-file shape.
assets/sample_knowledge_base.md: worked example spanning all four
managed topics plus a custom-topic pattern — copy from this into a real
facts file rather than inventing seed content structure from scratch.
Output Format
Return, in order: (1) which layer applies (ADK tool wiring vs. direct API
script) and why, (2) the exact command(s) run including --dry-run
validation before any real write, (3) for seeding tasks, the scope used and
a load_memory.py --as-prompt verification of what actually landed, and
(4) any applicable gotcha from references/long_term_memory_patterns.md
(async consolidation, scope mismatch, Cloud Run engine reuse) that could
explain unexpected behavior. Never claim a fact was successfully
remembered without a load_memory.py (or equivalent) verification step
showing it in the retrieved scope.
1---2name: adk-cross-session-knowledge-bank3description: Seeds, queries, and explains Vertex AI Memory Bank cross-session memory for ADK agents: PreloadMemoryTool/LoadMemoryTool recall, generate_memories_callback writes, managed/custom memory topics, and scope-based consolidation. TRIGGER when the user asks to "implement cross-session memory in ADK", "configure Vertex AI Memory Bank", "persist agent knowledge/preferences across conversations", "use PreloadMemoryTool or LoadMemoryTool", or seed/query Memory Bank facts by scope. DO NOT TRIGGER for short-term session state (state["key"]) with no cross-session need, generic RAG/document retrieval corpora, or non-Vertex memory stores (Redis, a custom vector DB) unrelated to Agent Engine Memory Bank.4license: Apache-2.05---67- ADK Cross-Session Memory Bank Integration89- Overview10This skill wires up and directly operates Vertex AI Memory Bank: the11managed service that lets an ADK agent remember user preferences,12terminology, and facts across separate conversations instead of forgetting13everything when a session ends. It covers both layers real integrations14need — the ADK tool layer (`PreloadMemoryTool`, `LoadMemoryTool`,15`after_agent_callback`) an agent uses automatically, and the underlying16Memory Bank API (`vertexai.Client().agent_engines.memories.*`) this skill's17own scripts call directly to seed or inspect memories outside of a live18agent turn.1920Grounded in the real `cross-session-memory` ADK sample21(`github.com/google/adk-samples/tree/main/core/python/cross-session-memory`)22and the Agent Platform Memory Bank docs — not invented from memory. Every23API call this skill's scripts make traces to a documented example; where a24detail (like semantic-similarity retrieval's exact parameter) wasn't25directly confirmed, the reference files say so explicitly rather than26guessing.2728- Prerequisites29- For real (non-dry-run) use: `pip install google-adk30 google-cloud-aiplatform[agent-engines]` and `gcloud auth31 application-default login`, plus a GCP project with Agent Engine access.32- A `--memory-bank-name` (an Agent Engine resource:33 `projects/P/locations/L/reasoningEngines/R`) — either an existing34 instance, or one the user needs to create first35 (`client.agent_engines.create()`, see36 `references/vertex_memory_bank_api.md`).37- None of the above is required for `--dry-run` — use it to validate seed38 files and preview requests with no GCP access at all.39- **Data classification:** Confidential. Memories may contain personal40 preferences, identity facts, or internal terminology; restrict scopes to41 authorized users and do not expose retrieved output outside its intended42 audience.4344- Workflow4546- Step 1: Decide ADK-tool wiring vs. direct API scripting47If the goal is "make my ADK agent remember things across sessions" (the48common case): wire `PreloadMemoryTool()` into the agent's `tools` and49`after_agent_callback=generate_memories_callback` per50`references/vertex_memory_bank_api.md`'s PreloadMemoryTool section — this51is two lines in any existing ADK agent, no coupling to this skill's52scripts required.53If the goal is "seed memories before the first conversation" or "check54what's currently stored" (testing, onboarding, debugging recall): use this55skill's `scripts/preload_memory.py` / `scripts/load_memory.py` directly56(Steps 2-3 below).5758- Step 2: Seed memories with `preload_memory.py`59Build a facts file matching `assets/memory_bank_schema.json` (see60`assets/sample_knowledge_base.md` for a worked example spanning all four61managed topics plus a custom-topic pattern), then:62```63scripts/preload_memory.py --facts-file seed.json --dry-run64```65Fix any validation errors, THEN run for real:66```67scripts/preload_memory.py --memory-bank-name projects/P/locations/L/reasoningEngines/R \68 --facts-file seed.json --scope-json '{"user_id":"123"}' --confirm-write69```70- Default mode is consolidation-aware (`GenerateMemories`); only pass71 `--no-consolidate` when guaranteed-distinct records matter more than72 avoiding duplicates (e.g. a disposable test fixture) — see the script's73 own docstring for why this is the exception, not the default.74- Never seed a real user's data this way without their knowledge — see75 the closing note in `assets/sample_knowledge_base.md`.76- `--confirm-write` is required for every real persistent write. It confirms77 the operator reviewed a successful dry run; never add it on the user's78 behalf or bypass it with a direct API call.7980- Step 3: Verify or inspect with `load_memory.py`81```82scripts/load_memory.py --memory-bank-name projects/P/locations/L/reasoningEngines/R \83 --scope-json '{"user_id":"123"}' --as-prompt84```85`--as-prompt` renders exactly the `<MEMORIES>` block `PreloadMemoryTool`86would inject — use this to confirm a seed actually took effect, or to87debug "why doesn't my agent know X" by checking whether the fact is even88in the scope being queried. Use `--dry-run --local-file seed.json` to89sanity-check scope-matching and rendering before touching the real API.9091- Step 4: Read the domain references before answering "why" questions92- `references/vertex_memory_bank_api.md` — PreloadMemoryTool vs.93 LoadMemoryTool, the write path, session-state-vs-memory boundary, the94 full API primitive table, memory topics, local-vs-real Memory Bank.95- `references/long_term_memory_patterns.md` — scope design, Memory Bank96 vs. RAG, perspective consistency, metadata merge strategies, and the97 Cloud-Run-reuses-a-stale-instance gotcha that silently breaks topic98 config changes.99Do not answer a "why isn't this working" question from general LLM100knowledge about memory systems — check these files first; several of the101gotchas here (async consolidation, Cloud Run engine reuse) are102non-obvious and specific to this API.103104- Examples105106- Example 1: New ADK agent, wire memory from scratch107Input: "Add cross-session memory to my ADK agent so it remembers user108preferences."109Expected output / behavior: add `PreloadMemoryTool()` to `tools` and110`after_agent_callback=generate_memories_callback` per Step 1 — no script111invocation needed for this case. Point to112`references/vertex_memory_bank_api.md` for the consolidation-is-async113gotcha so the user doesn't expect same-session recall of something just114said.115116- Example 2: Seed a team glossary before any real conversations happen117Input: "I want the support agent to already know our internal jargon118before it talks to anyone."119Expected output / behavior: build a facts file from120`assets/sample_knowledge_base.md`'s glossary section, `--dry-run` it,121then run `preload_memory.py` for real with an appropriate scope (likely a122`team_id`-based scope per `references/long_term_memory_patterns.md`, not123per-user, since the glossary applies to everyone on the team). Verify with124`load_memory.py --as-prompt`.125126- Error Handling127- `preload_memory.py --facts-file` fails schema validation: fix the128 reported field(s) against `assets/memory_bank_schema.json` before129 retrying — never bypass validation by hand-editing the request past130 what the script checked.131- A real `GenerateMemories` call returns 0 generated memories: this is132 valid, not a bug — Memory Bank only persists information matching a133 configured topic (see `references/vertex_memory_bank_api.md`); check the134 target instance's topic configuration before assuming the call failed.135- A seeded fact doesn't show up in `load_memory.py` right after seeding:136 check you queried the same `scope` you seeded with (scope must match137 exactly, not just conceptually overlap) before assuming seeding failed.138- Memory Bank topic/config changes don't take effect after redeploying to139 Cloud Run: see the "Cloud Run still needs an Agent Engine" /140 reused-as-is gotcha in `references/long_term_memory_patterns.md` — a141 pre-existing engine instance is not automatically reconfigured.142- `vertexai` import fails when not using `--dry-run`: tell the user to143 `pip install google-cloud-aiplatform[agent-engines]` — don't silently144 fall back to dry-run behavior, since that would hide that nothing was145 actually written.146- A user asks for semantic/similarity-search retrieval by free-text query:147 `load_memory.py` deliberately does not implement this (the exact API148 parameter wasn't directly confirmed while building this skill) — point149 them at the live "Fetch memories" doc rather than guessing a parameter150 name that may not match the current API.151152- Reference Files153- **scripts/preload_memory.py**: seeds Memory Bank from a facts file154 (`direct_memories_source`) or a conversation transcript155 (`direct_contents_source`); `--dry-run` validates and previews with no156 GCP call — run in Step 2.157- **scripts/load_memory.py**: scope-based retrieve, revision history, and158 `<MEMORIES>`-block rendering; `--dry-run --local-file` for offline159 testing — run in Step 3.160- **references/vertex_memory_bank_api.md**: ADK tool layer vs. API layer,161 PreloadMemoryTool/LoadMemoryTool, the write callback, session-state162 boundary, full API primitive table, memory topics — read in Step 1 and163 Step 4.164- **references/long_term_memory_patterns.md**: scope design, Memory Bank165 vs. RAG, perspective consistency, metadata merge strategy, deploy-path166 gotchas — read in Step 4 and whenever a seeded fact isn't behaving as167 expected.168- **assets/memory_bank_schema.json**: canonical JSON Schema for facts-file169 entries — both scripts validate against this; the single source of170 truth for the seed-file shape.171- **assets/sample_knowledge_base.md**: worked example spanning all four172 managed topics plus a custom-topic pattern — copy from this into a real173 facts file rather than inventing seed content structure from scratch.174175- Output Format176Return, in order: (1) which layer applies (ADK tool wiring vs. direct API177script) and why, (2) the exact command(s) run including `--dry-run`178validation before any real write, (3) for seeding tasks, the scope used and179a `load_memory.py --as-prompt` verification of what actually landed, and180(4) any applicable gotcha from `references/long_term_memory_patterns.md`181(async consolidation, scope mismatch, Cloud Run engine reuse) that could182explain unexpected behavior. Never claim a fact was successfully183remembered without a `load_memory.py` (or equivalent) verification step184showing it in the retrieved scope.