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
Slack Gains Shared Autonomous Agents With Claude Tag Beta
Anthropic has launched Claude Tag in beta, bringing autonomous, multi-agent AI directly into shared Slack channels for Enterprise and Team customers.
How to Build Graph-Based Workflows With Google ADK Go 2.0
Learn how to migrate from rigid DAGs to dynamic, stateful AI agent graphs using Google's new ADK Go 2.0 workflow engine and Human-in-the-Loop primitives.
A2A v2.0 Adds Zero-Knowledge Proofs to Multi-Agent Handoffs
Google Cloud's A2A Protocol v2.0 introduces decentralized discovery and zero-knowledge capability proofs, reducing multi-step workflow token consumption by 35%.
Flue Integration Opens Cloudflare Agents SDK to Custom Harnesses
Cloudflare has decoupled its Agents SDK into foundational platform primitives, allowing external frameworks like Flue to orchestrate stateful AI workflows.
Two-Agent AMIE Architecture Matches Physicians on 3-Visit Plans
Google Research demonstrated that its Gemini-based AMIE system matches primary care physicians in managing longitudinal patient care across multiple visits.