State Keys and Scope: Isolating Private Sub-Agent Data from Global Session State
State prefixes define scope — and the sub-agent chain shares one InvocationContext, so temp: is not private either. A tutorial on keeping sub-agent scratch isolated from user-, app-, and session-global state.
Published on • December 3, 2026
AI Assistant

A SequentialAgent runs a planner, then a reviewer, then a writer — and all three read the same session.state dictionary. That is by design (it is how the reviewer sees the planner’s output), but it creates a trap: every key you write is potentially visible to every other agent in the chain, and keys you thought were temporary turn out to leak further than you intended. A reviewer’s half-finished critique sitting in state is one {review_notes} placeholder away from being injected into the writer’s prompt.
The fix is not a new API — ADK already gives you everything you need in key prefixes and a clear rule about what the InvocationContext shares. This tutorial maps each scope, shows exactly what a sub-agent can and cannot isolate, and gives you a concrete isolation pattern.
Prerequisites
- ADK installed and a
Runneryou have run at least once. - Basic familiarity with
session.stateandEventActions.state_delta. - A multi-agent setup —
SequentialAgent,ParallelAgent, or a parent with sub-agents — to make the isolation problem real.
1. The four scopes, and what they isolate
Prefixes on state keys are the API for scope. Each answers “who can see this, and for how long”:
| Prefix | Scope | Lifetime | Readable without a session? |
|---|---|---|---|
| (none) | Current session only | This conversation thread | No |
user: | All sessions of one user within an app | Across that user’s sessions | Yes — get_user_state (except Vertex AI) |
app: | All users and all sessions of the app | App lifetime | No |
temp: | Current invocation only | Discarded after the invocation | No |
Two details matter for isolation:
user:reads without a session. In Python,await session_service.get_user_state(app_name=..., user_id=...)returns user-scoped keys with theuser:prefix stripped, so you can read them before a session exists.VertexAiSessionServiceis the exception: it always raisesNotImplementedErrorbecause the Agent Runtime API does not expose user state independently of a session — enumerate withlist_sessionsand callget_sessionon each instead.temp:is shorter-lived than you think, but wider than you hope. It is discarded when the invocation completes, yet the whole parent→sub-agent chain shares one invocation — which brings us to the core problem.
2. What the sub-agent chain actually shares
When a parent agent calls a sub-agent (via SequentialAgent, ParallelAgent, or any delegation), it passes its own InvocationContext. The sub-agent inherits the same invocation ID, and therefore the same temp: scope:
The
SequentialAgentpasses the sameInvocationContextto each of its sub-agents. This means they all share the same session state, including the temporary (temp:) namespace, making it easy to pass data between steps within a single turn.
The practical consequence: temp: is not sub-agent-private. It is invocation-wide. A temp:scratchpad key written by the planner is readable by the reviewer and the writer. If you expected temp: to be your isolation boundary, it is the wrong tool — it is a lifetime boundary (this turn only), not an audience boundary (this agent only).
Your agent code sees one combined session.state; the SessionService merges values from the correct underlying storage by prefix. There is no per-sub-agent namespace handed to you for free.
3. The isolation pattern: session scope + explicit namespacing
When sub-agent data must be private from its siblings within the same invocation, give it a session-scoped key owned by that sub-agent and never a shared prefix:
from google.adk.agents import LlmAgent
from google.adk.agents.sequential_agent import SequentialAgent
planner = LlmAgent(
name="PlannerAgent",
model="gemini-2.5-flash",
instruction="Break the request into steps. Store them in state['planner:plan'].",
output_key="planner:plan", # session scope, owned by the planner
)
reviewer = LlmAgent(
name="ReviewerAgent",
model="gemini-2.5-flash",
instruction=(
"Review {planner:plan} and write your critique to "
"state['reviewer:notes'] — do not modify the plan itself."
),
output_key="reviewer:notes",
)
pipeline = SequentialAgent(
name="PlanReviewPipeline",
sub_agents=[planner, reviewer],
)
Why this isolates:
- Session scope means the data lives for this conversation only — it never reaches other sessions or users.
- The
agent_name:prefix is a naming convention you own, not an ADK scope marker. ADK recognizesuser:,app:, andtemp:as special; anything else (includingplanner:plan) behaves as session state. The convention makes ownership legible to humans and prevents accidental{...}collisions between agents. - Read and write are asymmetric by instruction: the reviewer reads the planner’s key but is told to write only its own. Isolation here is enforced by convention and prompt, not by a sandbox.
For data that must not outlive the invocation at all, temp: is still correct — just assume every agent in the chain can read it:
changes = {
"temp:validated_payload": payload, # shared across the whole chain, this turn only
"reviewer:notes": critique, # private to this session, owned by ReviewerAgent
}
event = Event(invocation_id=..., author="system",
actions=EventActions(state_delta=changes))
await session_service.append_event(session, event)
4. Writing state the right way
Every isolation rule above assumes writes go through an event. Two supported routes:
output_keyon anLlmAgent— the agent’s final text is written into the event’sstate_deltaautomatically (as in the pipeline above).- Explicit
EventActions(state_delta={...})on an event you append viasession_service.append_event— for multi-key, non-string, or prefixed updates.
Mutating the state object on the Context you were handed (callback_context.state[...] = ...) is also safe — the framework tracks it. What is never safe: mutating a Session fetched from the SessionService directly (my_session.state[...] = ...). That bypasses event tracking and can lose data — and in a sub-agent chain, lost writes are exactly the kind of silent cross-agent corruption that isolation conventions are meant to prevent.
5. Isolation decision table
| You need sub-agent data that is… | Put it in… | Visible to siblings? |
|---|---|---|
| Private for this turn only | temp: key — remember it’s chain-wide | Yes, within the invocation |
| Private for this conversation | Session key, agent_name: convention | Yes, if siblings are told to read it |
| Shared across all a user’s sessions | user: key — never for scratch | Everyone, always |
| App-wide configuration | app: key | Everyone, always |
| Passed only between workflow nodes | Workflow node output (not state at all) | Only the successor node |
| Truly secret from other agents | Keep it out of state — local to the sub-agent’s own execution | No |
The last two rows are the escape hatches. If a value only needs to travel from one node to its immediate successor, it does not belong in state at all — use the node’s output mechanism, which never enters the shared dictionary. If it must not be readable by any sibling under any circumstance, keep it in local execution context rather than in the session.
Checklist
- Every state key has an intentional scope: session,
user:,app:, ortemp:. -
temp:is treated as invocation-wide (chain-shared), not sub-agent-private. - Sub-agent-owned session keys follow an
agent_name:naming convention for legibility. -
user:andapp:prefixes are never used for sub-agent scratch data. - State writes go through
output_keyorEventActions.state_delta, never a fetchedSession. - Mutations on the
Context’sstateobject are used where convenient — the framework tracks them. - Cross-node-only values use workflow
output, not state. - Genuinely private data stays out of
stateentirely. -
get_user_stateis not relied on withVertexAiSessionService(it raisesNotImplementedError).
Conclusion
Scope in ADK is a property of the key prefix, not of the agent that wrote it — and the sub-agent chain shares one InvocationContext, so temp: buys you lifetime isolation, not audience isolation. The working pattern is deliberate: session-scoped keys with an agent_name: convention for per-agent data, temp: for turn-wide scratch you want the chain to share, node output for edge handoffs, and local execution context for anything that must never touch the shared dictionary. Isolate by design, and the reviewer’s critique stops leaking into the writer’s prompt.