|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""DynamicNode Pattern: Runtime Agent Selection |
| 3 | +
|
| 4 | +Motivation (Mixture-of-Experts) |
| 5 | +-------------------------------- |
| 6 | +Shazeer et al. (2017) "Outrageously Large Neural Networks: The Sparsely-Gated |
| 7 | +Mixture-of-Experts Layer" showed that routing inputs to specialised experts |
| 8 | +beats a single monolithic model while keeping per-token compute fixed. |
| 9 | +
|
| 10 | +The same principle applies to agentic workflows: a *router* classifies the |
| 11 | +complexity/type of each task, then a *gating function* selects the cheapest |
| 12 | +adequate specialist—a fast flash-model for simple tasks, a slower pro-model |
| 13 | +for hard tasks. |
| 14 | +
|
| 15 | +Pattern: DynamicNode |
| 16 | +-------------------- |
| 17 | +DynamicNode is the first-class API for this pattern. The `agent_selector` |
| 18 | +callable runs at runtime, reads the current GraphState, and returns the |
| 19 | +appropriate BaseAgent. |
| 20 | +
|
| 21 | +Compare to the function-node alternative |
| 22 | +----------------------------------------- |
| 23 | +Without DynamicNode you need a function node that manually dispatches: |
| 24 | +
|
| 25 | + async def dispatch(state, ctx): |
| 26 | + agent = complex_agent if "hard" in state.data else simple_agent |
| 27 | + node_ctx = ctx.model_copy(update={...}) |
| 28 | + output = "" |
| 29 | + async for event in agent.run_async(node_ctx): |
| 30 | + if event.content and event.content.parts: |
| 31 | + output = event.content.parts[0].text or "" |
| 32 | + return output |
| 33 | +
|
| 34 | +DynamicNode gives you: |
| 35 | + ✅ Metadata auto-tracking: which agent was selected (observability) |
| 36 | + ✅ Built-in fallback_agent when selector returns None |
| 37 | + ✅ Selection logic decoupled from execution boilerplate |
| 38 | +
|
| 39 | +Architecture |
| 40 | +------------ |
| 41 | + classify ──► route (DynamicNode) ──► end |
| 42 | + │ |
| 43 | + ├─ selector returns simple_agent (flash, cheap) |
| 44 | + └─ selector returns detailed_agent (pro, thorough) |
| 45 | +""" |
| 46 | + |
| 47 | +import asyncio |
| 48 | +import os |
| 49 | + |
| 50 | +from google.adk.agents import LlmAgent |
| 51 | +from google.adk.agents.graph import DynamicNode |
| 52 | +from google.adk.agents.graph import GraphAgent |
| 53 | +from google.adk.agents.graph import GraphState |
| 54 | +from google.adk.runners import Runner |
| 55 | +from google.adk.sessions import InMemorySessionService |
| 56 | +from google.genai import types |
| 57 | + |
| 58 | +_MODEL = os.getenv("LLM_MODEL_NAME", "gemini-2.5-flash") |
| 59 | + |
| 60 | +# --------------------------------------------------------------------------- |
| 61 | +# Step 1: Classifier — assigns complexity label from the user's request |
| 62 | +# --------------------------------------------------------------------------- |
| 63 | +classifier = LlmAgent( |
| 64 | + name="classifier", |
| 65 | + model=_MODEL, |
| 66 | + instruction=""" |
| 67 | +You are a task complexity classifier. |
| 68 | +
|
| 69 | +Read the user's request and reply with EXACTLY one word: |
| 70 | + SIMPLE – if the task is a quick factual lookup or short question |
| 71 | + COMPLEX – if the task requires multi-step reasoning, analysis, or code |
| 72 | +
|
| 73 | +Reply with only the word, nothing else. |
| 74 | +""", |
| 75 | +) |
| 76 | + |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | +# Step 2: Specialists — cheap flash model vs thorough pro model |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | +simple_agent = LlmAgent( |
| 81 | + name="simple_responder", |
| 82 | + model=_MODEL, |
| 83 | + instruction=""" |
| 84 | +You are a concise assistant. Answer the user's question briefly (1-3 sentences). |
| 85 | +""", |
| 86 | +) |
| 87 | + |
| 88 | +detailed_agent = LlmAgent( |
| 89 | + name="detailed_responder", |
| 90 | + model=_MODEL, |
| 91 | + instruction=""" |
| 92 | +You are a thorough analyst. Work through the problem step by step, show your |
| 93 | +reasoning, and provide a complete, well-structured answer. |
| 94 | +""", |
| 95 | +) |
| 96 | + |
| 97 | + |
| 98 | +# --------------------------------------------------------------------------- |
| 99 | +# Step 3: Agent selector — called at runtime with current GraphState |
| 100 | +# --------------------------------------------------------------------------- |
| 101 | +def select_responder(state: GraphState) -> LlmAgent: |
| 102 | + """Route to simple_agent for SIMPLE tasks, detailed_agent otherwise. |
| 103 | +
|
| 104 | + The classifier stored its output in state.data["classify"] via the |
| 105 | + default output_mapper (OVERWRITE reducer, key = node name). |
| 106 | + """ |
| 107 | + classification = state.data.get("classify", "").upper() |
| 108 | + if "SIMPLE" in classification: |
| 109 | + return simple_agent |
| 110 | + return detailed_agent |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- |
| 114 | +# Build the graph |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | +def build_graph() -> GraphAgent: |
| 117 | + graph = GraphAgent( |
| 118 | + name="dynamic_routing", |
| 119 | + description="Routes each query to the cheapest adequate specialist", |
| 120 | + ) |
| 121 | + |
| 122 | + # Node 1: classify complexity |
| 123 | + graph.add_node("classify", agent=classifier) |
| 124 | + |
| 125 | + # Node 2: DynamicNode selects the right specialist at runtime |
| 126 | + graph.add_node( |
| 127 | + DynamicNode( |
| 128 | + name="respond", |
| 129 | + agent_selector=select_responder, |
| 130 | + fallback_agent=simple_agent, # safety net if selector returns None |
| 131 | + ) |
| 132 | + ) |
| 133 | + |
| 134 | + graph.add_edge("classify", "respond") |
| 135 | + graph.set_start("classify") |
| 136 | + graph.set_end("respond") |
| 137 | + return graph |
| 138 | + |
| 139 | + |
| 140 | +# --------------------------------------------------------------------------- |
| 141 | +# Runner helper |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +_graph = build_graph() |
| 144 | + |
| 145 | + |
| 146 | +async def run(question: str) -> str: |
| 147 | + graph = _graph |
| 148 | + svc = InMemorySessionService() |
| 149 | + runner = Runner( |
| 150 | + app_name="dynamic_node_example", agent=graph, session_service=svc |
| 151 | + ) |
| 152 | + await svc.create_session( |
| 153 | + app_name="dynamic_node_example", user_id="user", session_id="s1" |
| 154 | + ) |
| 155 | + final = "" |
| 156 | + async for event in runner.run_async( |
| 157 | + user_id="user", |
| 158 | + session_id="s1", |
| 159 | + new_message=types.Content(role="user", parts=[types.Part(text=question)]), |
| 160 | + ): |
| 161 | + if event.content and event.content.parts: |
| 162 | + text = event.content.parts[0].text or "" |
| 163 | + if text and not text.startswith("[GraphMetadata]"): |
| 164 | + final = text |
| 165 | + return final |
| 166 | + |
| 167 | + |
| 168 | +# --------------------------------------------------------------------------- |
| 169 | +# Demo |
| 170 | +# --------------------------------------------------------------------------- |
| 171 | + |
| 172 | + |
| 173 | +async def main(): |
| 174 | + questions = [ |
| 175 | + "What is the capital of France?", # SIMPLE → flash model |
| 176 | + ( # COMPLEX → pro model |
| 177 | + "Explain how transformer attention scales with sequence length " |
| 178 | + "and what architectural changes help address this." |
| 179 | + ), |
| 180 | + ] |
| 181 | + for q in questions: |
| 182 | + print(f"\nQ: {q}") |
| 183 | + answer = await run(q) |
| 184 | + print(f"A: {answer}") |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + asyncio.run(main()) |
0 commit comments