Lab 10: Building a "Memory" Agent with Stateful Tools
Goalโ
In this lab, you will build an agent that can remember a user's name across multiple turns. You will learn how to use the ToolContext to read from and write to the session state.
Step 1: Prepare the Projectโ
Environment setup:
- 1-Click Launch (Browser): Open in GitHub CodespacesOpen in Google Cloud Shell
- Local machine: If you're continuing the course in sequence,
cdinto theadk-trainingproject you created in Module 2 and skip the block below -- your environment and dependencies already carry forward. If you're starting fresh at this module, create that project first:uv init adk-training --python 3.10 && cd adk-training
uv add "google-adk>=2.1.0" python-dotenv- DevContainer / Codespaces (Alternative): If you opened this repository in a DevContainer or GitHub Codespaces, Python 3.11,
uv,gcloud, and all core ADK dependencies are pre-installed in your container environment.
-
Create the agent project:
uv run adk create memory_agent
cd memory_agent -
Create the tools package:
mkdir tools
touch tools/__init__.py
touch tools/memory.py
Step 2: Implement Stateful & Sensitive Toolsโ
Exercise: Open tools/memory.py and implement these three functions:
# In tools/memory.py
from google.adk.tools import ToolContext
def store_name(name: str, tool_context: ToolContext) -> str:
"""
Saves the user's name to the session memory.
Use this tool when the user tells you their name.
"""
# TODO: Save 'name' to tool_context.state under key "user_name"
pass
def recall_name(tool_context: ToolContext) -> str:
"""
Retrieves the user's name from the session memory.
Use this tool if the user asks who they are or what their name is.
"""
# TODO: Get "user_name" from tool_context.state.
# Return the name, or "Stranger" if not found.
pass
def clear_memory(tool_context: ToolContext) -> str:
"""
Permanently erases the user's stored name and session memory.
Use this tool when the user asks to forget them or reset memory.
"""
# TODO: Remove "user_name" from tool_context.state if present.
# Return a confirmation string.
pass
Step 3: Configure the Agent with Tool Confirmationโ
Exercise: Configure agent.py. Because clear_memory is a destructive action, wrap it inside FunctionTool with Human-in-the-Loop confirmation as covered in the Theory section.
# In agent.py
from google.adk.agents import Agent
from google.adk.tools import FunctionTool
from .tools.memory import store_name, recall_name, clear_memory
# TODO: Wrap clear_memory using FunctionTool to enforce human confirmation
guarded_clear_tool = ... # Configure FunctionTool with confirmation parameter
root_agent = Agent(
name="memory_agent",
model="gemini-3.8-flash",
description="An agent node that remembers users with guarded memory controls.",
instruction="""
You are a friendly assistant.
Use 'store_name' if the user introduces themselves.
Use 'recall_name' if they ask for their name.
Use 'clear_memory' if they ask you to forget them or erase their data.
""",
tools=[store_name, recall_name, guarded_clear_tool]
)
Step 4: Test Memory and Tool Confirmationโ
- Run:
uv run adk run .(run this from inside thememory_agentagent directory created in Step 1) - Test Memory:
- "Hi, I'm Mario." -> Should call
store_name. - "What is my name?" -> Should call
recall_nameand respond "Mario".
- "Hi, I'm Mario." -> Should call
- Test Human-in-the-Loop Confirmation:
- "Please forget my name." -> Should trigger
clear_memoryand request explicit confirmation before executing.
- "Please forget my name." -> Should trigger
- Inspect: Open the Dev UI (
uv run adk web) and check the State tab to observe state mutations.
Lab Summaryโ
You have successfully built an agent with programmable memory and safety governance! You have learned:
- How to use
ToolContextto access the ADK's session management. - How to read and write to
tool_context.state. - How to create a Store and Recall pattern to maintain context across multiple turns.
- How to enforce
require_confirmationon sensitive actions withFunctionTool.
Self-Reflection Questionsโ
- Why is it more reliable to store data in the session state rather than just relying on the LLM's chat history?
- What would happen if you used the same key (e.g., "user_name") for two different users? (Hint: The ADK isolates sessions automatically).
- When should you use
require_confirmation=Truevs a conditional predicate function?
๐ Reference Solution Vaultโ
Official Solution Vault โ Module 10
Locked Challenge โ Active Pedagogical Socratic Gate
Step 1/3: Question 1 of 1