Loop Agent

Purpose

Within the broader scope of Agent Workflow Management, the Loop Agent addresses the need to repeatedly execute a set of sub-agents either for a fixed number of iterations or until a specified termination condition is met. This subtopic enables workflows that require iterative refinement, continuous monitoring, or repeated attempts to achieve a goal—scenarios where running sub-agents just once (as with the Sequential Agent) or concurrently (as with the Parallel Agent) is insufficient.

Typical use cases include iterative problem solving, progressive refinement of an output (such as code revision or multi-step reasoning), or polling-like behaviors where the loop continues until an escalation or stop signal occurs.

Functionality

The Loop Agent extends the base agent behavior by orchestrating its sub-agents in a repeating cycle. Key aspects of its functionality include:

Core Looping Logic Example

func (a *loopAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
    count := a.maxIterations

    return func(yield func(*session.Event, error) bool) {
        for {
            shouldExit := false
            for _, subAgent := range ctx.Agent().SubAgents() {
                for event, err := range subAgent.Run(ctx) {
                    if !yield(event, err) {
                        return
                    }
                    if event.Actions.Escalate {
                        shouldExit = true
                    }
                }
                if shouldExit {
                    return
                }
            }

            if count > 0 {
                count--
                if count == 0 {
                    return
                }
            }
        }
    }
}

This snippet demonstrates the continuous execution of sub-agents, yielding their events, and checking for termination conditions.

Integration

The Loop Agent is a specialized implementation within the Agent Workflow Management topic, complementing the Sequential Agent and Parallel Agent by providing iterative execution semantics.

Relationship with Other Workflow Agents

flowchart TD
LoopAgent[Loop Agent]
SequentialAgent[Sequential Agent]
ParallelAgent[Parallel Agent]
SubAgent1[Sub-Agent 1]
SubAgent2[Sub-Agent 2]
SubAgent3[Sub-Agent 3]
LoopAgent --> SubAgent1
LoopAgent --> SubAgent2
SequentialAgent --> SubAgent1
SequentialAgent --> SubAgent2
ParallelAgent --> SubAgent2
ParallelAgent --> SubAgent3

The diagram illustrates how the Loop Agent manages its own sub-agents similarly to other workflow agents but with repetition semantics.

Diagram of Loop Agent Execution Flow

flowchart TD
Start[Start Loop]
IterationCheck{MaxIterations reached?}
RunSubAgents[Run Sub-Agents Sequentially]
YieldEvents[Yield Events to Caller]
EscalateCheck{Escalate Flag?}
Terminate[Terminate Loop]
NextIteration[Next Iteration]
Start --> IterationCheck
IterationCheck -- No --> RunSubAgents
RunSubAgents --> YieldEvents
YieldEvents --> EscalateCheck
EscalateCheck -- Yes --> Terminate
EscalateCheck -- No --> NextIteration
NextIteration --> IterationCheck
IterationCheck -- Yes --> Terminate

This flowchart visualizes the Loop Agent's core iterative process, showing how it runs sub-agents repeatedly, yields events, checks for escalation, and respects maximum iteration limits.


The Loop Agent’s design enables developers to build iterative AI workflows that require repeated processing steps, refinement cycles, or monitoring loops, integrating tightly with the broader agent orchestration capabilities described in Agent Workflow Management.