Overview

The Loop Agent is a type of workflow agent that executes sub-agents in an iterative cycle until a stop condition is met. This pattern is ideal for processes that need continuous refinement, iterative improvement, or multiple attempts until achieving a satisfactory result.

Unlike Sequential and Parallel agents, the Loop Agent repeats the execution of sub-agents multiple times, allowing each iteration to improve the result based on feedback from the previous iteration.

Based on Google ADK: Implementation following the patterns of the Google Agent Development Kit for iterative agents.

Key Features

Iterative Execution

Repeats sub-agent execution until stop condition is met

Continuous Improvement

Each iteration can improve the result based on the previous one

exit_loop Tool

Stop control via automatic tool in instructions

Full Flexibility

Customizable stop criteria via instructions

exit_loop Tool - Stop Control

Important: The Loop Agent allows you to select which sub-agents can use the exit_loop tool. During sub-agent configuration, you define which ones have the power to stop the loop.

How it works:

  • In each sub-agent configuration, you can enable the use of the exit_loop tool
  • Only selected sub-agents can decide to stop the loop
  • The tool accepts parameters to document the reason for stopping
  • Allows granular control over who can finalize the iterative process

Configuration in the interface:

  • Sub-agents with exit_loop: Can use the tool to stop the loop
  • Sub-agents without exit_loop: Execute normally without stopping power

How to use exit_loop:

exit_loop()

The exit_loop function accepts no arguments. It simply signals that the loop should stop.

Output Keys - State Sharing

Final Response: The sub-agent that has the Output Key defined as loop_output will be used to generate the final response presented to the user at the end of the loop.

Agent types with Output Key:

  • LLM Agent: Saves language model response
  • Task Agent: Saves task execution result
  • Workflow Agent: Saves executed workflow result
  • A2A Agent: Saves Agent-to-Agent protocol response

Special Output Key - loop_output:

  • loop_output - Sub-agent that generates final response presented to user
  • This agent is executed after all iterations to consolidate result
  • This agent’s response is presented to the user as the final result
  • Only one sub-agent can have loop_output as output_key

How it works:

  1. Configure the Output Key of each sub-agent
  2. The result is automatically saved in the loop state
  3. Use placeholders {{output_key_name}} in instructions to access data
  4. State persists across all loop iterations
  5. At the end, the agent with loop_output consolidates the final response

Example flow:

Iteration 1:
- Generator (output_key: "content") → saves to state.content
- Analyzer reads {{user_input}} and {{content}} → analyzes and saves to state.analysis

Iteration 2:  
- Generator reads {{user_input}}, {{content}} and {{analysis}} → refines and updates state.content
- Analyzer reads updated {{content}} → new analysis

End of Loop:
- Finalizer (output_key: "loop_output") → generates final response based on entire state

When to Use Loop Agent

Creating a Loop Agent

Step-by-Step on the Platform

Practical Examples

1. Marketing Content Refinement

2. Optimization with Different Agent Types

3. Development with Workflow Agents

Advanced Loop Configurations

Output Keys - Shared State

Stop Control with exit_loop

Optimization Strategies

Monitoring and Debugging

Tracking Iterations

Best Practices

Common Use Cases

Content Creation

Iterative Refinement:

  • Text improvement until desired quality
  • Copy optimization for conversion
  • Commercial proposal refinement

Optimization

Optimal Parameter Search:

  • Marketing campaign tuning
  • Price optimization
  • System configuration adjustment

Development

Generation and Correction:

  • Code generation with iterative testing
  • Algorithm refinement
  • Automatic bug fixing

Negotiation

Iterative Processes:

  • Automatic contract negotiation
  • Proposal refinement
  • Commercial terms optimization

Next Steps


The Loop Agent is perfect for processes that need continuous refinement and iterative improvement. Use it when you want to achieve high quality through multiple attempts and constant feedback.