Module 18: Dynamic Workflows - Programmable Graphs with @node
๐งญ Where We Are in Part 3: In Module 17, you built fixed pipelines (
A โ B, parallel fan-out +JoinNode) where every branch always ran. What if your workflow needs runtime decisions โ like "inspect an article draft first; if it passes length and quality checks, send it to the publisher; otherwise stop early"? Welcome to Dynamic Workflows (@node), where standard Python code (if/else, variables, functions) becomes your orchestrator!
Figure: Fully programmable multi-agent workflows executing dynamic routing and state evaluation with the @node decorator.
Theoryโ
Static edges (Module 17) are unbeatable when every step in your pipeline is fixed ahead of time. When execution depends on runtime conditions, Dynamic Workflows let you write an ordinary Python async def function that calls agents just like regular Python functions!
Building Your First @node Orchestratorโ
Figure: Programmatic node invocation lifecycle with context state inspection, adaptive node dispatch, and result aggregation.
ADK 2.0 introduces two simple primitives for programmable orchestration:
@node(fromgoogle.adk.workflow): A decorator that turns any Python function into a graph node that aWorkflowcan run.await ctx.run_node(target_node, input_data): Executes anAgent(or another node) and returns its output directly into a Python variable!
from google.adk import Agent, Workflow, Context
from google.adk.workflow import node
# 1. Define specialist agents as usual
# IMPORTANT: Every child Agent invoked dynamically via ctx.run_node() inside
# a @node(rerun_on_resume=True) orchestrator MUST also set rerun_on_resume=True!
researcher = Agent(
name="researcher",
model="gemini-3.8-flash",
rerun_on_resume=True,
instruction="Research key facts about the user's topic.",
)
writer = Agent(
name="writer",
model="gemini-3.8-flash",
rerun_on_resume=True,
instruction="Turn the provided research notes into a short, engaging article.",
)
# 2. Write a Python orchestrator decorated with @node
@node(rerun_on_resume=True)
async def newsletter_workflow(ctx: Context, node_input: str):
# Step A: Run researcher and store its answer in a normal Python variable
research_notes = await ctx.run_node(researcher, node_input)
# Step B: Use standard Python if/else or string formatting freely!
if len(research_notes) < 50:
return "Not enough public information found on this topic."
# Step C: Pass research_notes directly into writer and return its final article
final_article = await ctx.run_node(writer, research_notes)
return final_article
# 3. Register the orchestrator as the Workflow's entry point
root_agent = Workflow(
name="NewsletterSystem",
edges=[("START", newsletter_workflow)],
)
Line-by-Line Breakdown of the New Primitivesโ
@node(rerun_on_resume=True)& ChildAgent(..., rerun_on_resume=True): Registersnewsletter_workflowas a workflow node. In ADK 2.0, both the@node(rerun_on_resume=True)orchestrator and every childAgentinvoked dynamically viaawait ctx.run_node(child, ...)must setrerun_on_resume=Trueso multi-turn conversations re-execute cleanly on subsequent user messages without raisingValueError: A node must have rerun_on_resume=True.async def newsletter_workflow(ctx: Context, node_input: str)::ctx: Contextis ADK's runtime control object (giving youctx.run_node(...)andctx.session.state).node_input: stris the incoming user message (because"START"connects to this function).
await ctx.run_node(researcher, node_input): Runsresearcherwithnode_inputas its prompt and returns the result directly:- If the agent returns plain text, you get a
str. - If the agent has a Pydantic
output_schema, ADK automatically parses the JSON and returns a plain Pythondict(e.g.,result["approved"]).
- If the agent returns plain text, you get a
return final_article: Whatever your@nodefunction returns is emitted as the workflow's final response to the user!
When to Choose Dynamic @node Workflowsโ
- Multi-Step Conditional Chains: Run Agent A, inspect or combine its result in Python, and conditionally pass enriched context to Agent B.
- Direct Variable Passing: No need for
output_keyor{placeholder}templates โ outputs live in normal Python variables. - Full Trace Visibility: Even though routing happens in Python, ADK's Workflow Runtime still records every
ctx.run_node()call in the Dev UI Trace tab!
Bonus: Using a @node as an Agent Toolโ
Because every @node function is a first-class ADK component, you can also pass a @node directly into an Agent's tools=[...] list without any extra wrapper:
from google.adk import Agent, Context
from google.adk.workflow import node
@node
async def calculate_vip_discount(ctx: Context, tier: str) -> str:
"""Calculates the discount rate for a given customer tier."""
return "20% VIP discount" if "VIP" in tier.upper() else "5% standard discount"
root_agent = Agent(
model="gemini-3.8-flash",
name="pricing_agent",
instruction="Help customers with discounts using the calculate_vip_discount tool.",
tools=[calculate_vip_discount],
)
When `await ctx.run_node(topic_checker, node_input)` executes an `Agent` configured with a Pydantic `output_schema`, what Python type does `run_node()` return at runtime?
Key Takeawaysโ
@node(rerun_on_resume=True)turns a standardasync defPython function into a programmable workflow orchestrator.await ctx.run_node(node, input)executes any agent and returns its output directly (str, ordictwhenoutput_schemais set).- Use Module 17 for fixed pipelines, and Module 18 when orchestration requires conditional multi-step Python logic!