Skip to content
Blog

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 Runner you have run at least once.
  • Basic familiarity with session.state and EventActions.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”:

PrefixScopeLifetimeReadable without a session?
(none)Current session onlyThis conversation threadNo
user:All sessions of one user within an appAcross that user’s sessionsYes — get_user_state (except Vertex AI)
app:All users and all sessions of the appApp lifetimeNo
temp:Current invocation onlyDiscarded after the invocationNo

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 the user: prefix stripped, so you can read them before a session exists. VertexAiSessionService is the exception: it always raises NotImplementedError because the Agent Runtime API does not expose user state independently of a session — enumerate with list_sessions and call get_session on 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 SequentialAgent passes the same InvocationContext to 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 recognizes user:, app:, and temp: as special; anything else (including planner: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:

  1. output_key on an LlmAgent — the agent’s final text is written into the event’s state_delta automatically (as in the pipeline above).
  2. Explicit EventActions(state_delta={...}) on an event you append via session_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 onlytemp: key — remember it’s chain-wideYes, within the invocation
Private for this conversationSession key, agent_name: conventionYes, if siblings are told to read it
Shared across all a user’s sessionsuser: key — never for scratchEveryone, always
App-wide configurationapp: keyEveryone, always
Passed only between workflow nodesWorkflow node output (not state at all)Only the successor node
Truly secret from other agentsKeep it out of state — local to the sub-agent’s own executionNo

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:, or temp:.
  • 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: and app: prefixes are never used for sub-agent scratch data.
  • State writes go through output_key or EventActions.state_delta, never a fetched Session.
  • Mutations on the Context’s state object are used where convenient — the framework tracks them.
  • Cross-node-only values use workflow output, not state.
  • Genuinely private data stays out of state entirely.
  • get_user_state is not relied on with VertexAiSessionService (it raises NotImplementedError).

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.

References