Skip to main content

Command Palette

Search for a command to run...

Loop Agent

Updated
3 min read
Loop Agent
V

Highly skilled Data Test Automation professional with over 10 years of experience in data quality assurance and software testing. Proven ability to design, execute, and automate testing across the entire SDLC (Software Development Life Cycle) utilizing Agile and Waterfall methodologies. Expertise in End-to-End DWBI project testing and experience working in GCP, AWS, and Azure cloud environments. Proficient in SQL and Python scripting for data test automation.

Problem: One-Shot Quality

The SequentialAgent and ParallelAgent produce their final output and then stop. This 'one-shot' approach isn't good for tasks that require refinement and quality control. What if the first draft of our story is bad? We have no way to review it and ask for a rewrite.

The Solution: Iterative Refinement

When a task needs to be improved through cycles of feedback and revision, you can use a LoopAgent. A LoopAgent runs a set of sub-agents repeatedly until a specific condition is met or a maximum number of iterations is reached. This creates a refinement cycle, allowing the agent system to improve its own work over and over.

Use Loop when: Iterative improvement is needed, quality refinement matters, or you need repeated cycles.

Architecture:

Project Structure

Github repository contains the following key files:

File

Purpose

agent.py

Core sequential agent logic

.env

Environment variables (API keys, etc.)

The heart of the project is agent.py, where the agent is defined and configured.

Code Walkthrough:

1. Set up the model and retry behavior

A Gemini model (gemini-2.5-flash-lite) is configured with retry rules so that if the API hits rate limits or temporary server errors (like 429 or 503), it will automatically retry up to five times. This ensures the agent workflow is stable and resilient.

2. Create the Initial Writer Agent (first‑draft generator)

The InitialWriterAgent runs once at the start.
It takes the user’s prompt and produces a 100–150 word first draft of a short story.
Its output is stored in the shared state under current_story, which later agents will read and modify.

3. Create the Critic Agent (quality checker)

The CriticAgent reads the current story and evaluates it.
It produces one of two outputs:

  • “APPROVED” — meaning the story is good enough

  • Specific suggestions — 2–3 improvements about plot, pacing, or characters

This feedback is stored as critique.

4. Define the exit function for ending the loop

A simple Python function exit_loop() returns a status message.
The refiner agent will call this function only when the critic says “APPROVED”, signaling that the refinement loop should stop.

5. Create the Refiner Agent (improver or loop terminator)

The RefinerAgent reads both the story and the critique:

  • If the critique is exactly “APPROVED”, it calls the exit_loop() tool and stops refining.

  • Otherwise, it rewrites the story to incorporate the critic’s feedback and updates current_story.

This agent is the one that actually improves the story draft.

6. Build the workflow: initial draft → refinement loop

Two orchestration agents define the full pipeline:

  • LoopAgent runs the cycle:
    Critic → Refiner → Critic → Refiner, up to 2 iterations.

  • SequentialAgent runs the overall workflow:
    InitialWriterAgent → LoopAgent

Execution Output:

GitHub Repo:

https://github.com/puthanvipin/loop-agent-google-adk-writer-critic-refinement