Skip to main content

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, cd into the adk-training project 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.
  1. Create the agent project:

    uv run adk create memory_agent
    cd memory_agent
  2. 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โ€‹

  1. Run: uv run adk run . (run this from inside the memory_agent agent directory created in Step 1)
  2. Test Memory:
    • "Hi, I'm Mario." -> Should call store_name.
    • "What is my name?" -> Should call recall_name and respond "Mario".
  3. Test Human-in-the-Loop Confirmation:
    • "Please forget my name." -> Should trigger clear_memory and request explicit confirmation before executing.
  4. 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 ToolContext to 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_confirmation on sensitive actions with FunctionTool.

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=True vs a conditional predicate function?

๐Ÿ” Reference Solution Vaultโ€‹

๐Ÿงช

10: Advanced Function Tools(Challenge Lab)

Part 2: Tools & Capabilities โ€ข Module 10

Overall Course Progress:
0%(0/98)
Next: 11: OpenAPI & REST Tools ๐Ÿ“– โ†’