How to Refactor Monolithic Agents with Google ADK
Learn how to transition monolithic prompt scripts into production-ready multi-agent pipelines using Google's Agent Development Kit and the Agent2Agent protocol.
Google Developers recently published Production-Ready AI Agents: 5 Lessons from Refactoring a Monolith, detailing how they rebuilt a fragile sales research prototype named Titanium. The release coincides with major updates to the Agent Development Kit (ADK) and the launch of the Gemini Enterprise Agent Platform. You can use these new ADK features to transition monolithic scripts into modular, multi-agent architectures.
The Titanium refactoring highlights a shift away from linear scripts toward orchestrated nodes. The original system suffered from silent failures and high token costs. By adopting ADK’s specialized classes and native protocols, you can address these architectural bottlenecks directly.
Orchestrating Sub-Agents
The standard approach of looping through prompts in a single script often creates brittle logic. The Titanium project replaced its linear iteration loops with an orchestrated pipeline using ADK’s SequentialAgent.
You can split heavy workloads into specialized nodes. In the Titanium refactoring, developers divided the workload into a Company Researcher, Search Planner, Case Study Researcher, Selector, and Email Drafter. The official ADK documentation provides complete class references for implementing the SequentialAgent to manage this handoff. Breaking tasks into distinct roles makes it easier to implement multi-agent coordination patterns and isolate failures.
Enforcing Structured Outputs with Pydantic
Relying on schema formatting instructions within your prompt string leads to unpredictable parsing errors. The updated ADK replaces brittle JSON parsing with native Pydantic integration.
You can define strict Pydantic objects within your ADK configuration. This enforces structural integrity at the runtime level, ensuring that the model output matches your required schema before the next step in the pipeline executes. If a sub-agent returns malformed data, the native Pydantic enforcement triggers a validation error rather than passing bad data downstream.
Using the Agent2Agent Protocol
Interoperability across different languages is necessary for larger enterprise deployments. The Agent2Agent (A2A) protocol is a standardized communication layer that allows ADK agents to interact regardless of the underlying language. The protocol supports Python, Java, Go, and TypeScript.
To establish communication between heterogeneous agents, configure an AgentCard identity system. The AgentCard defines the agent’s capabilities and endpoints. Agents communicate via JSON-RPC REST endpoints over the A2A protocol, allowing them to delegate tasks across different frameworks.
Managing Tokens and Context
High token usage is a common issue with monolithic prompts. The new SkillToolset feature introduces progressive disclosure for your agents. Instead of loading all potential instructions into the initial context, agents load domain expertise on demand. Google reports that progressive disclosure can reduce token usage by up to 90 percent compared to traditional monolithic prompts. This approach fundamentally shifts how you handle managing context windows.
Additionally, the release of ADK for Java 1.0.0 brings automated event compaction to help manage context windows over long-running sessions. The Java update also introduces Google Maps grounding and Human-in-the-Loop (HITL) confirmation workflows. A dynamic Retrieval-Augmented Generation (RAG) system replaces hardcoded prompt contexts to scale the agent’s knowledge base securely.
Error Handling and Observability
Runaway token burn during infinite retry loops can quickly escalate API costs. The refactored system utilizes ADK’s native exponential backoffs and timeout boundaries as circuit breakers. You can set these boundaries in your agent configuration to halt execution when a node gets stuck, directly aiding in reducing API costs in production.
For real-time telemetry, integrate the system with OpenTelemetry. The Titanium project paired this with a custom Server-Sent Events (SSE) streaming application to monitor live diagnostics. This infrastructure helps you evaluate and test AI agents as they transition from prototypes to production environments.
Deployment Options
ADK agents are deployment-agnostic. You can deploy the refactored nodes across several Google Cloud environments depending on your scalability requirements. Supported targets include Cloud Run, Google Kubernetes Engine (GKE), and the Vertex AI Agent Engine Runtime. The Gemini Enterprise Agent Platform also includes the Agents CLI to build, scale, and govern fleets of agents from the terminal.
Review the ADK implementation guides for your specific programming language to map out the required AgentCard identities before refactoring your monolithic scripts.
Get Insanely Good at AI
The book for developers who want to understand how AI actually works. LLMs, prompt engineering, RAG, AI agents, and production systems.
Keep Reading
Unit 42 Reveals Zealot Multi-Agent AI for Cloud Attacks
Palo Alto Networks has demonstrated Zealot, an autonomous multi-agent AI system capable of executing end-to-end cloud infrastructure exploits in minutes.
Cursor Agents Boost CUDA Kernel Speed by 38% on NVIDIA Blackwell
A new multi-agent system from Cursor achieves massive performance gains on NVIDIA Blackwell GPUs by autonomously optimizing complex CUDA kernels.
Claude Cowork Reimagines the Enterprise as an Agentic Workspace
Anthropic debuts Claude Cowork, introducing multi-agent coordination, persistent team memory, and VPC deployment options for secure corporate collaboration.
Build Autonomous Tools 10x Faster via Claude Managed Agents
Anthropic debuts Claude Managed Agents, a cloud-hosted API suite that handles infrastructure, sandboxing, and persistent state for production AI agents.
Rocket AI Automates Strategic Consulting Reports for Startups
Indian startup Rocket launches a multi-agent AI platform to deliver high-level McKinsey-style management consulting at a fraction of traditional costs.