DEV Community

Neeraj Sharma
Neeraj Sharma

Posted on AI-assisted

Google Rebuilt a Monolithic AI Agent Into 5 Sub-Agents. Here Is the Same Refactor in 57 Lines of Python.

A five step agent pipeline on a developer monitor

Most first agents look the same: one long prompt, one loop, and a prayer. Google's developer relations team published a clear write-up of fixing exactly that. They took a sales research agent called Titanium, a single Python script built on a for loop, and rebuilt it. When one sub-task failed, from an API timeout or a made-up answer, the whole run stalled without saying why.

Their fix used Google's Agent Development Kit. The five changes are worth copying whatever framework you use, so below I list them and then show the same refactor in 57 lines of plain Python that I ran.

The five changes

1. Sub-agents in a row. The monolith became a pipeline of five narrow nodes: Company Researcher, Search Planner, Case Study Researcher, Selector and Email Drafter. A model doing one small job is more reliable than one model juggling a large multi-step prompt.

2. Structured outputs. The JSON format used to live inside the prompt string, which made parsing fragile. They moved it into Pydantic classes, so the schema is code and the prompt only describes the task.

3. Retrieval instead of a hardcoded list. The prototype carried 12 case studies in the script. The new version crawls the source site in the background, stores the pages in a vector index and uses hybrid search: meaning plus exact keywords.

4. Tracing. Every model request, token count and tool call is emitted as an OpenTelemetry trace. A silent failure becomes a visible one.

5. Retries from the framework. Exponential backoff, timeouts and retry limits come from the orchestration layer, not from hand-written try and except blocks around every call.

The same refactor without a framework

Here is a small version with three steps. It covers changes 1, 2, 4 and 5. The model call is a stub that times out 30% of the time, so you can see the retries.

import json, random, time
from pydantic import BaseModel, ValidationError

# 1. One schema per step. The format lives in code, not in the prompt.
class CompanyIntel(BaseModel):
    company: str
    pain_points: list[str]

class CaseStudy(BaseModel):
    title: str
    why_it_fits: str

class Email(BaseModel):
    subject: str
    body: str

def call_llm(prompt: str, schema: type[BaseModel]) -> str:
    """Swap in your model client. Ask it for JSON that matches `schema`.
    This stub fails sometimes, so you can watch the retries work."""
    if random.random() < 0.3:
        raise TimeoutError("model timed out")
    fake = {
        "CompanyIntel": {"company": "Acme Freight", "pain_points": ["slow quotes"]},
        "CaseStudy": {"title": "Quote bot for a broker", "why_it_fits": "same problem"},
        "Email": {"subject": "Faster quotes", "body": "Hi, saw that quotes take a day..."},
    }
    return json.dumps(fake[schema.__name__])

TRACE = []  # 4. One record per attempt. Send these to your tracing tool.

def step(name: str, prompt: str, schema: type[BaseModel], retries: int = 3):
    """2 + 5. Run one narrow task, validate the output, retry with backoff."""
    for attempt in range(1, retries + 1):
        start = time.perf_counter()
        try:
            result = schema.model_validate_json(call_llm(prompt, schema))
            TRACE.append({"step": name, "attempt": attempt, "ok": True,
                          "ms": round((time.perf_counter() - start) * 1000)})
            return result
        except (TimeoutError, ValidationError) as err:
            TRACE.append({"step": name, "attempt": attempt, "ok": False,
                          "error": type(err).__name__})
            time.sleep(0.2 * 2 ** attempt)  # exponential backoff
    raise RuntimeError(f"{name} failed after {retries} attempts")

def pipeline(company: str) -> Email:
    """1. Three small agents in a row. Each one sees only what it needs."""
    intel = step("research", f"Research {company}. List its pain points.", CompanyIntel)
    case = step("case_study", f"Pick a case study for: {intel.pain_points}", CaseStudy)
    return step("draft", f"Write an email to {intel.company} using: {case.title}", Email)

if __name__ == "__main__":
    random.seed(7)
    email = pipeline("Acme Freight")
    print(email.subject)
    for record in TRACE:
        print(record)
Enter fullscreen mode Exit fullscreen mode

Running it prints:

Faster quotes
{'step': 'research', 'attempt': 1, 'ok': True, 'ms': 1}
{'step': 'case_study', 'attempt': 1, 'ok': False, 'error': 'TimeoutError'}
{'step': 'case_study', 'attempt': 2, 'ok': True, 'ms': 0}
{'step': 'draft', 'attempt': 1, 'ok': False, 'error': 'TimeoutError'}
{'step': 'draft', 'attempt': 2, 'ok': True, 'ms': 0}
Enter fullscreen mode Exit fullscreen mode

Two of the three steps timed out once and the run still finished. In the monolith, either timeout would have ended it.

What the small version teaches

  • The step function is the whole trick. Every model call goes through one place that validates, retries and records. Add a timeout or a cost counter there once and every agent gets it.
  • Validation is a retry trigger. If the model returns JSON that does not match the schema, ValidationError sends it round again. Bad output never reaches the next agent.
  • Each step sees less. The drafter never sees the raw research, only the fields it needs. Smaller inputs mean fewer tokens and fewer ways to go wrong.
  • The trace answers "why was it slow?" Sort the records by ms and count the failed attempts per step. That is your first dashboard.

When to reach for a framework

Plain Python is fine up to a handful of steps. Use a framework such as ADK or LangGraph when you need parallel branches, human approval in the middle, or state that survives a restart. The five ideas stay the same.

I write more about building agents that hold up in production, including an agent orchestrator with an MCP harness. If you want one built for your product, that is what we do at Axionry.

Source

Top comments (0)