Managing Workflow State: Scoped Node Updates and Immutable State Deltas
How ADK workflow state really works — Event(state=...) writes, ctx.state, key prefixes, committed deltas, and how to avoid the mutation bugs that parallel branches create.
Published on • December 2, 2026
AI Assistant

Every workflow needs a memory. The draft has to survive from the writer node to the reviewer; an attempt counter has to survive from one loop iteration to the next; a user’s preference has to survive across sessions. The tempting shortcut is to bolt everything onto session state because it is always available — until two parallel branches overwrite each other’s keys, a 4 MB document quietly lives in a key-value store meant for flags, and nobody can say which node wrote what.
ADK’s state model is more deliberate than a shared dictionary. State writes ride along with the node’s events as deltas, keys carry prefixes that define their lifetime, and the docs are explicit about what state is not for. In this tutorial you will learn how to write and read state in graph workflows, what the four key scopes mean, why state and node output are different tools, and how to structure updates so concurrent nodes cannot clobber each other.
Prerequisites
- ADK 2.0 (
pip install google-adk) - Comfort with graph workflows:
Workflow, nodes, andEvent(see graph routes) - Read the companion piece on node output: data handling docs
1. Writing state: Event(state=...) and ctx.state
There are two ways to change state from a node, and they end up in the same place.
Yield a state delta with your event. The state field of an Event carries the values you want persisted:
from google.adk import Context, Event, Workflow
async def init_state_node(attempts: int = 0):
yield Event(state={"attempts": attempts})
async def task_attempt_node(node_input, attempts: int):
yield Event(state={"attempts": attempts + 1})
async def read_state_node(ctx: Context):
print(f"attempts state: {ctx.state['attempts']}") # attempts state: 1
root_agent = Workflow(
name="root_agent",
edges=[("START", init_state_node, task_attempt_node, read_state_node)],
)
Mutate ctx.state directly. The Context object exposes a delta-aware state object — per the ADK source, “for any state change, you can mutate this object directly, e.g. ctx.state['foo'] = 'bar'”:
async def start_run(ctx: Context, node_input: str):
ctx.state["topic"] = node_input.strip()
ctx.state["temp:started_at"] = "2026-12-02T08:27:03+07:00"
ctx.state["attempts"] = 0
Both forms record the change as a delta against the session state, and that delta is committed together with the writing node’s events. The practical consequences:
- A write is visible to every later node in the same run.
- The write is attached to this node’s events, so the event stream tells you which node changed what — useful when you are debugging a session by reading it top to bottom.
- Downstream nodes read the accumulated state, not a snapshot frozen at graph-build time.
Reading is just as direct: take a Context parameter in your node and index into ctx.state.
2. State key prefixes: four lifetimes, one store
Not all keys should live as long as the session. ADK gives every key an optional prefix that selects its scope:
| Prefix | Scope |
|---|---|
app: | Shared across all users and sessions for the app |
user: | Tied to the user, shared across their sessions |
temp: | Discarded after the current invocation ends |
| (none) | Persists for the lifetime of the session |
This table is the whole design. A retry counter that only matters during this run belongs behind temp: — it evaporates when the invocation ends, so a resumed or restarted run cannot inherit a half-finished count by accident. A per-user preference uses user: so it follows the person across new sessions. A global feature flag uses app:. Plain keys are the default and persist for the session’s life.
Choosing the scope is a correctness decision, not a naming convention. State written without a prefix to track “which attempt am I on” is exactly the kind of value that leaks into the next invocation and causes the bug you will spend an afternoon reproducing.
3. State versus output: one reader or many
The most common state mistake is using it as a conveyor belt. The docs draw the line crisply:
When only the next node needs a value, pass it along the edge as node
output. Use state when a value must outlive the run, or be read by a tool, a callback, or{key}instruction templating.
So:
- Next node needs it →
return Event(output=...). Typed, validated, one consumer, no residue. - A tool, callback, or instruction template needs it → state. Tools and callbacks do not sit on the edge; they reach into the session.
- A value must outlive this invocation → state — with the correct prefix.
- It is large → neither. See the next section.
Output flows down the graph. State flows sideways into everything else that shares the session. Using state for the former produces workflows where you cannot tell what each node consumed, because everything reads the same bag.
4. The data-size limitation
The docs carry an explicit caution: session state “should not be used to persist large amounts of data between nodes.” State is a lightweight key-value store. File contents, binary blobs, and full API responses belong in artifacts or external storage (database tools), not in a session record that gets loaded, checkpointed, and replayed as part of every invocation.
Two related rules complete the picture:
- For large but temporary working data inside one run, prefer passing it as node
output— it travels the edge, not the session. - If a tool or an instruction template genuinely must see the value, store a reference (an artifact ID, a row key), not the payload itself.
A practical test: if your state value would make a JSON dump of the session hard to read, it is too big.
5. Avoiding mutation bugs
Here is where “delta-aware” earns its keep — and where the failure modes hide.
Parallel branches and read-modify-write. Suppose two fan-out branches both increment attempts:
async def branch_worker(ctx: Context, node_input: str):
current = int(ctx.state.get("attempts", 0)) # both read 0
ctx.state["attempts"] = current + 1 # both write 1
Each branch computes its delta from the state it saw. Last writer wins; the increment vanishes. The fix is structural: give each branch its own key (attempts_a, attempts_b) and merge them in a JoinNode, or do the counting in a single node after the join. Never use one shared counter as a synchronization primitive across parallel paths.
Mutating shared objects in place. If you read a list from state, append to it, and write it back, you are doing read-modify-write with the same race. Build a new value from what you read and yield it as the delta. Deltas are committed per write; treat each write as an immutable replacement rather than an in-place edit that other in-flight nodes might be observing.
Scope leaks. Scratch values (temp:) die with the invocation; unprefixed keys do not. If a node writes started_at without the prefix to measure its own latency, the next invocation inherits a timestamp from the last one. Match the prefix to the value’s true lifetime.
Stale reads after resume. After a human-in-the-loop pause, state is what survived the pause — not what was in a local variable. Re-read state on resume instead of assuming in-memory values, and prefer temp: for anything whose meaning does not survive the interruption.
6. Checklist
Before you ship a state-heavy workflow:
- Every key has a deliberate prefix —
app:,user:,temp:, or an justified plain key. - Single-consumer values travel as
output; state is reserved for cross-node, cross-tool, or cross-session needs. - No large payloads in state; references to artifacts or database rows instead.
- Parallel branches never read-modify-write the same key.
- State writes are new values yielded as deltas, not in-place mutations of shared objects.
- The event stream alone is enough to tell you which node wrote which key.
State in ADK is not a global variable with a nicer name. It is a scoped, event-committed record of what each node decided to remember — and the moment you treat it as one, debugging a workflow becomes reading its event history instead of guessing at hidden writes.