tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Google Gemini API > ADK LoopAgent - Iterative Refinement Patterns

ADK LoopAgent - Iterative Refinement Patterns

Author: Venkata Sudhakar

A LoopAgent in ADK runs a sub-agent repeatedly until a termination condition is met. This is the right pattern for iterative refinement tasks - generating a first draft, evaluating it, improving it, evaluating again, stopping when quality is met. Without LoopAgent you would write a while loop manually around agent calls and parse output each time. LoopAgent gives you this declaratively: define the agent, the termination signal, and a max_iterations safety limit to prevent runaway loops.

The LoopAgent runs its sub_agent in a loop, passing the full conversation history forward so the agent sees its previous attempt and can improve it. Termination works via a keyword in the agent output that LoopAgent detects - when the agent includes the termination signal the loop stops and returns the final output. max_iterations provides a hard safety cap. This pattern is ideal for content quality loops, retry-on-failure patterns, and any task where the agent needs to self-evaluate and revise.

The below example builds a product description quality loop - a writer-evaluator agent drafts a description, scores it, and the loop continues improving until the score reaches 8 out of 10 or the iteration limit is hit.


Running the quality loop on a product brief,


It gives the following output showing the loop refining until quality is met,

Starting quality refinement loop...

[Iteration 1]
DESCRIPTION: Experience unmatched silence with Sony WH-1000XM5 headphones.
Industry-leading noise cancellation blocks out office noise and flight cabin
roar so you stay focused. 30-hour battery keeps you going from morning
meetings to your commute home. USB-C charges fast when you need it.
Multipoint Bluetooth connects your laptop and phone simultaneously.
Score: Accuracy 8, Persuasive 6, India-relevance 5, Length 7 = Overall 6.5
NEEDS_IMPROVEMENT: Add India-specific context (WFH, travel, price value).

[Iteration 2]
DESCRIPTION: India's WFH professionals and frequent flyers, meet your
perfect focus partner. Sony WH-1000XM5 delivers industry-leading ANC to
block out household noise and cabin sound alike. At Rs 29,990, enjoy
30-hour battery, instant USB-C charging, and multipoint Bluetooth to
switch seamlessly between your work laptop and phone. Foldable design
fits every bag. Your productivity. Your silence.
Score: Accuracy 8, Persuasive 9, India-relevance 9, Length 8 = Overall 8.5
QUALITY_APPROVED

=== FINAL APPROVED DESCRIPTION ===
[Iteration 2 text shown above - loop stopped at first approval]

# Loop ran 2 iterations - stopped as soon as QUALITY_APPROVED appeared
# If no approval by iteration 5, LoopAgent returns the last output anyway
# Pattern works for: content QA, data validation loops, retry-on-error

LoopAgent use cases: content quality refinement (keep improving until a quality score threshold), data extraction retry (if first extraction has missing fields, try again with more specific instructions), code generation with self-testing (generate code, run tests, fix failures, repeat until tests pass), and multi-step research where the agent decides it needs more information and keeps searching until it has enough to answer confidently. Always set max_iterations - an agent that never produces the termination signal would loop forever without this safety cap. Log the iteration count alongside your outputs to identify which tasks are consistently hitting the limit and need instruction improvement.


 
  


  
bl  br