Module 38: Best Practices & Production Patterns

Theoryβ
From Prototype to Productionβ
Building a working agent is the first step. Building a production-ready agent requires a focus on architecture, performance, security, and reliability. This module summarizes the essential best practices for taking your agent from a prototype to a robust, scalable, and maintainable application, aligned with the ADK 2.0 Workflow Runtime.
Figure: Defense-in-depth architecture covering input validation, execution resilience, state integrity, and observability.
1. Architectural Best Practices: Thinking in Graphsβ
In ADK 2.0, every component is a Node in a Workflow Graph. Choosing the right structure is your most critical design decision.
| Pattern | When to use it | Key Characteristics |
|---|---|---|
Agent | Natural language reasoning and dynamic tool calling. | Non-deterministic, flexible. Best for high-level decision making. |
Workflow (Static) | Fixed, predictable pipelines (Sequential/Parallel). | Deterministic. Reduced latency and cost by avoiding LLM routing where logic is known. |
@node (Dynamic) | Complex, code-based orchestration (Loops, If/Else). | Maximum control. Allows arbitrary Python logic between agent executions. |
- Small, Focused Tools: Design your tools to follow the single-responsibility principle. A tool should do one thing well.
- Progressive Disclosure with Skills: Don't overload an agent with 50 tools. Group related domain workflows and instructions into Skills (
SkillToolset, covered in depth in Module 39.5) and only activate them on demand when needed. - Deterministic Routing: If you know that Step B always follows Step A, don't ask an LLM to decide. Use a
Workflowor a Dynamic@node.
2. Resilience: Framework-Level Error Handlingβ
ADK 2.0 changes the way we handle failures. Instead of writing complex try...except logic inside every tool, we now leverage the Workflow Runtime.
The "Let it Fail" Patternβ
In ADK 1.x, you were taught to catch all exceptions in tools. In ADK 2.0, you should allow standard exceptions to propagate out of your tools and workflow nodes.
Why?
- Automatic Retries: If a node or tool fails with an exception, the ADK framework can automatically retry the execution based on a
RetryConfig. - Human-in-the-Loop (HITL) Interrupts: When a workflow pauses for human input, ADK unwinds the node stack using
NodeInterruptedError. In ADK 2.14,NodeInterruptedErrorinherits directly from Python'sBaseException(rather thanException) specifically so normalexcept Exception:blocks won't accidentally catch itβhowever, never write a bareexcept:orexcept BaseException:without re-raising, as swallowingNodeInterruptedErrorsilently breaks workflow pause and resume!
Configuring Retriesβ
You configure retry logic per-node, on the node itself (either as a constructor argument like Agent(..., retry_config=...), or via @node(retry_config=...) for function nodes):
from google.adk.workflow import RetryConfig
# Framework handles retries for you!
my_agent_node = Agent(
...,
retry_config=RetryConfig(max_attempts=3, initial_delay=2.0)
)
Important: retry_config does not cascade from a container to the nodes inside it. Setting retry_config on a Workflow(...) only governs retries of that Workflow as a node (relevant if it's nested inside a parent graph) β it has no effect on the nodes in its own edges. If you want a specific node inside a Workflow to retry, put retry_config directly on that node.
3. Performance & Token Cost Optimizationβ
- Model Selection Hierarchy: Use
gemini-3.8-flashfor routing, classification, and high-throughput tool calling (validating quality parity withagents-cli eval compare), and reservegemini-3.8-profor deep multi-step reasoning. - Context Caching & History Compaction (
AppLevel): For agents with large static system instructions, domain rubrics, or tool schemas (>= 4,096tokens), enableContextCacheConfig(min_tokens=4096, ttl_seconds=1800)onApp(...)and bound multi-turn prompt growth withEventsCompactionConfig(compaction_interval=10, overlap_size=2)βcovered hands-on in Module 30.4 (Cloud Telemetry, BigQuery Analytics & Cost Control). - Deterministic Short-Circuit Caching: Use
before_agent_callbackor keyed@nodecaches on normalized queries to bypass redundant LLM or backend calls when an identical read-only lookup was recently resolved. - Async Everything: Always use
asynctool and node definitions to prevent blocking the event loop during I/O-bound tasks (API calls, DB queries).
4. Security & Safetyβ
- Fail-Closed Validation: Use Pydantic schemas for input and output contracts. If validation fails, the entry node raises immediately, preventing prompt injection or malformed data from propagating downstream.
- Secrets Management: Use Google Secret Manager or
.envfiles. Never log sensitive data or return it in an agent response. - HITL for Side Effects: Any action that performs a destructive or irreversible side effect (deleting data, executing a financial transfer) must pause for human approval using the right primitive for your layer:
- Agent Tools (
FunctionTool): Wrap standard Python tool functions withFunctionTool(my_fn, require_confirmation=True)(Module 13) so ADK automatically emits a confirmationRequestInputevent before executing the tool. - Workflow Nodes (
@node): Inside a generator@nodefunction, explicitlyyield RequestInput(...)(fromgoogle.adk.events) to pause the workflow graph until the user supplies confirmation or structured input.
- Agent Tools (
Key Takeawaysβ
- Think in Nodes: Break logic into discrete, testable nodes orchestrated by a Workflow, and pass validated domain payloads cleanly from node to node.
- Framework Resilience: Propagate exceptions to enable ADK 2.0 automatic retries, and never swallow
BaseException/NodeInterruptedError. - Model Efficiency: Match the model capability to the task complexity, and use Skills (Module 39.5) for progressive context disclosure.
- Strict Schemas & HITL: Use Pydantic to enforce "fail-closed" security at node boundaries and
FunctionTool(..., require_confirmation=True)or@nodeyield RequestInput(...)for destructive side effects.