Lab 18: Building a Smart Support Router with Dynamic Workflows
Goalโ
In this lab, you will build a conditional multi-step orchestration system using a Dynamic Workflow โ doing something Module 17's fixed static edges cannot do:
- Run
classifierto detect the customer's sentiment ("angry","neutral", or"happy"). - If the customer is not angry, simply call
ai_supportand return its technical answer. - If the customer is
"angry", execute a dynamic 2-step chain:- First, call
ai_supportto draft a quick technical troubleshooting note. - Second, pass both the customer's original message and that technical draft into
human_escalation, so the escalation team responds with empathy and an immediate technical fix!
- First, call
This shows the real superpower of @node + ctx.run_node(): chaining and combining multiple agent outputs in plain Python variables.
Step 1: Create the Project Structureโ
Environment setup:
- 1-Click Launch (Browser): Open in GitHub CodespacesOpen in Google Cloud Shell
- Local machine: If you're continuing the course in sequence,
cdinto theadk-trainingproject you created in Module 2 and skip the block below -- your environment and dependencies already carry forward. If you're starting fresh at this module, create that project first:uv init adk-training --python 3.10 && cd adk-training
uv add "google-adk>=2.1.0" python-dotenv- DevContainer / Codespaces (Alternative): If you opened this repository in a DevContainer or GitHub Codespaces, Python 3.11,
uv,gcloud, and all core ADK dependencies are pre-installed in your container environment.
-
Create a new project:
uv run adk create support_router_v2 -
Navigate into the new directory:
cd support_router_v2
Step 2: Implement the Dynamic Routerโ
Exercise: Open agent.py. Complete the TODO placeholders to define the two specialist agents (ai_support and human_escalation), the classifier agent, and the support_router_workflow function using @node and ctx.run_node().
# In agent.py (Starter Code)
from __future__ import annotations
from pydantic import BaseModel
from google.adk import Agent, Workflow, Context
from google.adk.workflow import node
from typing import Literal
# ===== Specialist Agent Nodes =====
# TODO: Define ai_support (name="ai_support_bot") and human_escalation (name="human_escalation_team") agents.
# Hint: Set model="gemini-3.8-flash" and rerun_on_resume=True on EVERY child Agent called via ctx.run_node()!
# One answers technical questions clearly; the other apologizes to frustrated customers and includes any provided technical steps.
ai_support = ...
human_escalation = ...
# ===== 1. Define Sentiment Schema =====
# Define a Pydantic model for structured classification
class SentimentClassification(BaseModel):
sentiment: Literal["angry", "neutral", "happy"]
# TODO: Create the classifier agent node using the schema above (remember rerun_on_resume=True!).
classifier = ...
# ===== 2. Build the Dynamic Workflow =====
# TODO: Implement the orchestrator node
# 1. Use the @node(rerun_on_resume=True) decorator.
# 2. Accept 'ctx: Context' and 'node_input: str' as arguments.
@node(rerun_on_resume=True)
async def support_router_workflow(ctx: Context, node_input: str):
# Step 2a: Run the classifier node asynchronously using ctx.run_node().
# Note: Even though the classifier's output_schema is the SentimentClassification
# Pydantic model, run_node() returns it as a plain dict at runtime --
# access fields with result["sentiment"], not result.sentiment.
classification = None
# Step 2b: Dynamic Multi-Step Orchestration.
# - If classification["sentiment"] == "angry":
# 1. Call `ai_support` via ctx.run_node() to draft a preliminary technical fix.
# 2. Combine `node_input` and that technical draft into a prompt, then
# call `human_escalation` via ctx.run_node() and return its response.
# - Otherwise:
# Call `ai_support` directly via ctx.run_node() and return its response.
return None
# ===== 3. Register the System =====
# TODO: Create a Workflow named "SupportSystem"
# and link the "START" edge to your support_router_workflow.
root_agent = Workflow(
name="SupportSystem",
edges=[("START", ...)]
)
Step 3: Run and Test the Routerโ
- Return to the parent directory and start the Dev UI:
(Select
cd ..
uv run adk websupport_router_v2from the agent dropdown in the UI). - Test the dynamic orchestration:
- Input: "My internet is down, help!" -> Runs
classifier-> callsai_support_botdirectly. - Input: "THIS IS DISGUSTING! MY ROUTER IS STILL BROKEN!" -> Runs
classifier-> callsai_support_botfor a preliminary technical fix -> passes both the complaint and the fix tohuman_escalation_team!
- Input: "My internet is down, help!" -> Runs
- Inspect the Workflow Graph: In the Dev UI, open the Graph View. You will see the visual representation of your dynamic execution: the flow from the router node to the specific specialist agent.
Lab Summaryโ
By completing this lab, you have mastered the fundamental orchestration pattern of ADK 2.0:
- Using
@nodeto turn standard Python functions into workflow components. - Leveraging
ctx.run_node()to execute agents and retrieve structured results directly. - Implementing Programmable Routing that combines AI classification with deterministic business rules.
Self-Reflection Questionsโ
- How does passing data with
ctx.run_node()in a Dynamic Workflow differ from passing data withoutput_keyin Static Edges (Module 17)? - Why is it important to set
rerun_on_resume=Truefor the orchestrator node? - Can a dynamic workflow call another dynamic workflow? (Hint: Yes, every workflow is just a node!)
๐ Reference Solution Vaultโ
Official Solution Vault โ Module 18
Locked Challenge โ Active Pedagogical Socratic Gate