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
$10M DeepMind Fund Targets Emergent Multi-Agent AI Risks
Google DeepMind and partners have launched a $10 million funding initiative to study collective behaviors and emergent safety risks in multi-agent ecosystems.
Thousand Token Wood Runs a 5-Agent Economy on Qwen2.5-3B
Developed for Hugging Face's Build Small Hackathon, the Thousand Token Wood simulation uses a 3-billion-parameter model to drive a real-time agent economy.
CodeRabbit Routes Claude 4.x Models to Fix AI Intent Gaps
CodeRabbit’s new orchestration layer uses Claude Opus 4.7 and Sonnet 4.6 to translate high-level Jira requirements into validated coding plans before execution.
How to Build Long-Running AI Agents With Google ADK 1.0
Google's Agent Development Kit 1.0 enables multi-day workflows that survive restarts. Learn to configure durable state machines and persistent session storage.
How to Build Multi-Agent CNC Workflows on AMD MI300X
Learn how to coordinate LangChain agents and Qwen 2.5 7B on the AMD MI300X to reduce CNC manufacturability analysis time from hours to seconds.