Module 22: State and Memory - Persistent Agent Context

Theory
Giving Agents Memory: Sessions vs. Long-Term Memory
Welcome to Part 4: Production Readiness. In Part 3, you learned how to orchestrate multiple specialized agents into teams and workflows. To make those systems truly production-ready, an agent cannot treat every turn like a stranger walking in for the first time—it needs context continuity.
Google ADK separates conversational continuity into two distinct, complementary subsystems:
| Dimension | Session (SessionService) | Long-Term Memory (MemoryService) |
|---|---|---|
| Mental Model | The Active Workbench (current conversation thread & scratchpad) | The Searchable Archive (filing cabinet of past knowledge) |
| Data Stored | Ordered events ledger + structured key-value state | Distilled facts, semantic memories, or indexed past transcripts |
| How Data Is Read | Automatically loaded on every turn by (app_name, user_id, session_id) | Queried via search_memory(app_name=..., user_id=..., query=...) |
| Primary Use Case | Multi-turn dialogue history, active workflow progress, user preferences | Recalling unstructured insights discussed weeks ago across threads |
| Built-in Backends | InMemorySessionService, DatabaseSessionService, VertexAiSessionService (+ CLI SqliteSessionService) | InMemoryMemoryService, SqliteMemoryService, VertexAiMemoryBankService, VertexAiRagMemoryService |
Anatomy of an ADK Session
Every conversation in ADK is represented by a google.adk.sessions.Session Pydantic model and uniquely addressed by a 3-part composite coordinate:
(app_name, user_id, session_id)
When a Runner loads or creates a Session, it populates six core attributes:
from google.adk.sessions import Session
# Conceptual inspection of a live Session instance:
# session.id -> str: Unique thread ID (e.g., "session_math_1")
# session.app_name -> str: Application namespace (e.g., "personal_tutor")
# session.user_id -> str: Caller identity (e.g., "student_42")
# session.state -> dict[str, Any]: Merged key-value state dictionary
# session.events -> list[Event]: Chronological append-only turn ledger
# session.last_update_time -> float: Unix timestamp of the most recent event
Understanding the 3-part coordinate (app_name, user_id, session_id) makes State Scoping intuitive—each prefix simply binds a key to a broader or narrower slice of that coordinate!
Session State (session.state) & Scoped Prefixes
session.state is a structured key-value dictionary used to personalize behavior, track multi-step workflow progress, and pass structured outputs between agents. ADK routes keys to different persistence scopes based on their prefix:
| Prefix | Bound Coordinate | Persistence | Example Use Case |
|---|---|---|---|
| (None) | (app_name, user_id, session_id) | Current session only | state['current_topic'] = 'asyncio' — Active lesson progress |
user: | (app_name, user_id) | Shared across all sessions for that user | state['user:language'] = 'es' — Persistent user preferences |
app: | (app_name) | Shared across all users and sessions | state['app:course_version'] = '2.1' — Global configuration |
temp: | Current invocation_id only | Never persisted to storage | state['temp:quiz_percentage'] = 92.5 — Single-turn scratch math |
Note (
temp:State Visibility): Althoughtemp:keys are stripped before storage commit and never survive into the next user turn, they remain visible inside the liveEventstream and the Dev UI Trace View during the current turn—makingtemp:ideal for passing transient calculations between tools or nodes within a single invocation without polluting persistent state.
Security & Multi-Tenant Hygiene:
- Never store secrets, API keys, or OAuth tokens in
session.state(even undertemp:): persistent scopes are written as JSON in database tables, andtemp:deltas are emitted in live event traces. Store credentials in Secret Manager, environment variables, or ADKAuthCredentialstores.- Mind
app:blast radius &user:retention:app:keys are merged into every user's session—never write user- or tenant-specific data intoapp:*. Conversely,delete_session()removes session events while preservinguser:*profile keys, so handle GDPR erasure requests explicitly on user state.
Figure: Scoped session state lifecycle and persistence pipeline from ephemeral turn scratchpad to durable storage tables.
How Event-Sourced State Mutation Works (EventActions.state_delta)
A critical architectural rule in ADK is that state is event-sourced. Calling await session_service.get_session(...) returns a detached snapshot of the session. Mutating session.state["key"] = "value" directly on that snapshot in external Python code will not persist to the database!
Instead, every durable state change is recorded as a state_delta inside EventActions and committed atomically through session_service.append_event(session, event):
The 4 Safe Ways to Write State
- Inside a Function Tool (
ToolContext):def set_language(lang: str, tool_context: ToolContext) -> dict:
tool_context.state["user:language"] = lang.strip().lower()[:10]
return {"status": "saved"} - Inside a Lifecycle Callback (
CallbackContext):def track_turn(callback_context: CallbackContext):
callback_context.state["turn_count"] = callback_context.state.get("turn_count", 0) + 1 - Automatically via Agent
output_key:summarizer = Agent(
name="summarizer",
model="gemini-3.8-flash",
instruction="Summarize the lesson in 2 bullet points.",
output_key="last_lesson_summary", # Emits state_delta={"last_lesson_summary": ...}
) - Seeding Initial State (
create_session) or External System Updates (append_event):from google.adk.events import Event, EventActions
# A. Seed initial state when creating a session:
session = await session_service.create_session(
app_name="personal_tutor",
user_id="student_42",
session_id="session_1",
state={"app:course_version": "2.1"},
)
# B. Commit an out-of-band system state update on an existing session:
await session_service.append_event(
session,
Event(
author="system",
invocation_id="sys_sync_1",
actions=EventActions(state_delta={"user:tier": "pro"}),
),
)
Accessing State in Instructions: {key} vs. {key?}
ADK automatically interpolates session state values into an agent's instruction string right before calling the model:
{current_topic}(Required Key): Replaces the placeholder withsession.state["current_topic"]. If the key is missing fromstate, ADK raises aKeyError—helpful in deterministic multi-agent pipelines (Modules 17–18) to catch broken upstream handoffs early.{app:course_version?}(Optional Key — trailing?): Replaces the placeholder with the value if present, or cleanly substitutes an empty string""if the key has not been initialized yet. (Tip: when interpolating user-supplied state like{user:language?}, validate/allowlist values inside your tool first so untrusted text cannot inject instructions.)
from google.adk.agents import Agent
tutor = Agent(
name="personal_tutor",
model="gemini-3.8-flash",
instruction=(
"You are a helpful tutor (Catalog {app:course_version?}). "
"Adapt explanations to the user's preferred language: {user:language?}."
),
)
SessionService Lifecycle & Storage Backends
Every ADK Runner delegates session persistence to an implementation of BaseSessionService, exposing four asynchronous keyword-only CRUD operations:
# 1. Create a new session (with optional custom session_id and initial state)
session = await session_service.create_session(
app_name="tutor_app",
user_id="student_42",
session_id="session_1",
state={"app:course_version": "2.1"},
)
# 2. Retrieve an existing session snapshot (returns None if not found)
loaded = await session_service.get_session(
app_name="tutor_app",
user_id="student_42",
session_id="session_1",
)
# 3. List all sessions for a user (or across the entire app if user_id is omitted)
response = await session_service.list_sessions(
app_name="tutor_app",
user_id="student_42",
)
print([s.id for s in response.sessions])
# 4. Delete a session (removes session-scoped events/state; preserves user: & app: state!)
await session_service.delete_session(
app_name="tutor_app",
user_id="student_42",
session_id="session_1",
)
Choosing the Right SessionService Backend
| Backend | Import / Extra | Concurrency & Best Fit |
|---|---|---|
InMemorySessionService | google.adk.sessions (core) | Volatile RAM. Lost when the Python process exits. Best for fast unit tests and InMemoryRunner. |
SqliteSessionService | google.adk.sessions.sqlite_session_service (core aiosqlite) | Local CLI SQLite (sqlite:///). Lightweight JSON schema used internally by adk web's default .adk/session.db. Single-process local dev only. |
DatabaseSessionService | google.adk.sessions (google-adk[db]) | SQLAlchemy 2.0 Async Relational Store. Use sqlite+aiosqlite:/// for single-instance local/CI testing, and postgresql+asyncpg:// (Cloud SQL / AlloyDB) or Cloud Spanner for multi-worker production with row-level SELECT ... FOR UPDATE locking. |
VertexAiSessionService | google.adk.sessions + Vertex AI | Fully Managed Cloud Service (agentengine://...). Serverless horizontal scaling on Cloud Run / GKE / Agent Engine with zero database ops. |
Custom BaseSessionService | Subclass BaseSessionService | NoSQL / Custom Stores (such as the Cloud Firestore provider you built in Module 13.5). |
Programmatic Example with DatabaseSessionService
Always manage DatabaseSessionService with async with in standalone scripts (or close it during FastAPI app lifespan shutdown). Under the hood, aiosqlite and asyncpg maintain background worker threads/socket pools—disposing the engine cleanly ensures asyncio.run(...) exits immediately without hanging:
from google.adk.runners import Runner
from google.adk.sessions import DatabaseSessionService
# Single-instance local dev: "sqlite+aiosqlite:///./tutor_sessions.db"
# Multi-replica Cloud Run / GKE: "postgresql+asyncpg://user:pass@host:5432/dbname"
async with DatabaseSessionService(
db_url="sqlite+aiosqlite:///./tutor_sessions.db"
) as session_service:
runner = Runner(
app_name="tutor_app",
agent=root_agent,
session_service=session_service,
)
# Any user: or app: state written during runner.run_async() is now persisted on disk!
Important — How
uv run adk webPersists Sessions & CLI Flags: In ADK 2.0+,adk web(intended strictly for local development on127.0.0.1) enables--use_local_storageby default, writing sessions to<agent_folder>/.adk/session.dbviaSqliteSessionService! You can switch backends from the CLI (note:--session_service_uricannot be combined with--[no_]use_local_storage):
- Default local
.adk/SQLite:uv run adk web- Pure ephemeral RAM (reset on restart):
uv run adk web --no_use_local_storage(or--session_service_uri="memory://")- SQLite (
SqliteSessionServiceschema):uv run adk web --session_service_uri="sqlite:///./local_dev.db"- SQLAlchemy (
DatabaseSessionService, requiresgoogle-adk[db]+ async driver):uv run adk web --session_service_uri="sqlite+aiosqlite:///./tutor_sessions.db"or"postgresql+asyncpg://user:pass@host:5432/dbname"
Managing Long Sessions: Rewind, Caching & Compaction
1. Session Rewind (runner.rewind_async)
ADK supports rewinding an active session to a point prior to a specific invocation_id—useful when a user cancels an action or wants to retry a quiz from an earlier turn:
- Restored: Session-scoped state keys (keys created after the rewind point are reset to
None) and session artifacts. - Preserved (NOT rewound): Persistent
user:andapp:state, plus external side effects (such as emails already sent).
await runner.rewind_async(
user_id="student_42",
session_id="session_1",
rewind_before_invocation_id=target_invocation_id,
)
2. Context Caching (ContextCacheConfig)
When agents include large static system instructions or reference manuals, resending those tokens on every turn increases latency and cost. ContextCacheConfig caches immutable prompt prefixes on Gemini:
from google.adk.apps.app import App
from google.adk.agents.context_cache_config import ContextCacheConfig
app = App(
name="enterprise_analyst",
root_agent=root_agent,
context_cache_config=ContextCacheConfig(
min_tokens=2048, # Cache only if system context exceeds 2,048 tokens
ttl_seconds=1800, # Cache Time-To-Live in seconds (30 minutes)
cache_intervals=10, # Refresh cache every N invocations
),
)
3. Context Compaction (EventsCompactionConfig)
In long multi-turn tutoring or support sessions, raw session.events history grows continuously. Context Compaction applies sliding-window LLM summarization over older events while keeping recent turns intact:
from google.adk.apps.app import App, EventsCompactionConfig
from google.adk.apps.llm_event_summarizer import LlmEventSummarizer
from google.adk.models import LLMRegistry
app = App(
name="long_running_assistant",
root_agent=root_agent,
events_compaction_config=EventsCompactionConfig(
compaction_interval=20, # Trigger summarization every 20 invocations
overlap_size=3, # Keep the 3 most recent turns uncompressed
summarizer=LlmEventSummarizer(llm=LLMRegistry.new_llm("gemini-3.8-flash")),
),
)
Long-Term Recall with MemoryService
While user: state is ideal for structured key-value attributes (language, tier, completed topic IDs), MemoryService is designed for unstructured cross-session recall—answering open-ended questions like "What struggle did I have when we studied recursion last month?".
InMemoryMemoryService/SqliteMemoryService: Lightweight keyword-based recall over ingested sessions for local prototyping and CLI testing.VertexAiMemoryBankService: Managed Google Cloud service that uses LLM-powered Memory Extraction & Consolidation to turn raw chat logs into semantic user memories.
Standard 3-Step Workflow:
from google.adk.memory import InMemoryMemoryService
from google.adk.runners import Runner
from google.adk.tools import load_memory
memory_service = InMemoryMemoryService()
# 1. Ingest a completed Session into long-term memory
completed_session = await session_service.get_session(
app_name="tutor_app", user_id="student_42", session_id="session_1"
)
await memory_service.add_session_to_memory(completed_session)
# 2. Query memories programmatically across past sessions
results = await memory_service.search_memory(
app_name="tutor_app",
user_id="student_42",
query="recursion base cases",
)
# 3. Wire memory_service into Runner and equip your Agent with load_memory
tutor_with_recall = Agent(
name="tutor_with_recall",
model="gemini-3.8-flash",
instruction="Use load_memory whenever the student asks about past lessons.",
tools=[load_memory],
)
runner = Runner(
app_name="tutor_app",
agent=tutor_with_recall,
session_service=session_service,
memory_service=memory_service,
)
From Local Dev to Google Cloud (VertexAiSessionService + VertexAiMemoryBankService)
Because Agent definitions depend only on abstract Session state and memory tools—never on the underlying storage engine—promoting your prototype from local RAM/SQLite (InMemorySessionService / DatabaseSessionService + InMemoryMemoryService) to managed Vertex AI Agent Engine requires zero changes to your Agent code. You only swap the service instances passed into Runner:
from google.adk.memory import VertexAiMemoryBankService
from google.adk.runners import Runner
from google.adk.sessions import VertexAiSessionService
from google.adk.tools import load_memory, preload_memory
PROJECT_ID = "your-gcp-project-id"
LOCATION = "us-central1"
AGENT_ENGINE_ID = "1234567890123456789" # Provisioned Reasoning / Agent Engine resource ID
# Swap local services for managed Google Cloud services — tutor_with_recall stays 100% unchanged!
cloud_session_service = VertexAiSessionService(
project=PROJECT_ID,
location=LOCATION,
agent_engine_id=AGENT_ENGINE_ID,
)
cloud_memory_service = VertexAiMemoryBankService(
project=PROJECT_ID,
location=LOCATION,
agent_engine_id=AGENT_ENGINE_ID,
)
cloud_runner = Runner(
app_name="tutor_app",
agent=tutor_with_recall, # Exact same Agent definition!
session_service=cloud_session_service,
memory_service=cloud_memory_service,
)
Choosing Between load_memory and preload_memory
ADK ships two built-in memory retrieval tools in google.adk.tools that work identically across InMemoryMemoryService, SqliteMemoryService, and VertexAiMemoryBankService:
load_memory(On-Demand Tool Call): Registers a standard function tool that the LLM invokes only when it decides past context is needed (e.g., "What topic did I struggle with last week?"). This minimizes latency and token usage on routine turns that don't require historical recall.preload_memory(Automatic Turn-by-Turn Injection): Automatically queriesMemoryServicebefore every LLM call using the current user turn and injects matching memories straight into the system instructions—guaranteeing personalization even when the user doesn't explicitly ask the agent to "remember," at the cost of one memory search per turn.
Interactive Knowledge Checks
In ADK 2.0 State Management, which state prefix ensures a stored value is shared across all future sessions belonging to the same user rather than being restricted to a single session or turn?
Why does mutating session.state['topic'] = 'SQL' directly on a Session object returned by session_service.get_session() fail to persist in DatabaseSessionService?
Key Takeaways
- Composite Session Identity: Every
Sessionis uniquely addressed by(app_name, user_id, session_id)and holds an append-onlyeventslist plus a mergedstatedictionary. - Four State Scopes: Unprefixed keys stay in one
session_id;user:spans all sessions for auser_id;app:spans all users of anapp_name;temp:vanishes when the current turn ends. - Event-Sourced Writes: Always mutate state via
tool_context.state,callback_context.state,output_key,create_session(..., state=...), orappend_event()so changes persist throughEventActions.state_delta. - Dynamic Instructions: Use
{key}for required state interpolation and{key?}for optional keys that may not be initialized yet. - Pluggable
SessionServiceBackends:adk webpersists locally to.adk/SQLite by default; production deployments useDatabaseSessionService(postgresql+asyncpg://) orVertexAiSessionService. - Session vs. Memory: Use
Session(SessionService) for active thread history and structured state, andMemoryService(add_session_to_memory/search_memory) for searchable cross-session recall.