agent.go

Overview

This file implements the LoopAgent, a specialized workflow agent that repeatedly executes its sub-agents for a configured number of iterations or indefinitely until a termination condition is met. The LoopAgent is part of the broader Agent Workflow Management system, enabling iterative AI workflows such as progressive refinement, polling, or repetitive processing tasks.

The LoopAgent orchestrates its sub-agents sequentially in each iteration, streaming events generated during their execution back to the caller. It supports early termination if any sub-agent signals an escalation, allowing interruption of the loop for higher-level handling. The agent enforces internal control over the Run method to preserve consistent looping behavior.


Package and Imports


Types and Structures

Config

type Config struct {
    AgentConfig   agent.Config
    MaxIterations uint
}

loopAgent (unexported struct)

type loopAgent struct {
    maxIterations uint
}

Functions and Methods

New(cfg Config) (agent.Agent, error)

func New(cfg Config) (agent.Agent, error)
loopAgent, err := loopagent.New(loopagent.Config{
    AgentConfig: agent.Config{
        SubAgents: []agent.Agent{subAgent1, subAgent2},
        // other config fields...
    },
    MaxIterations: 5,
})

(a *loopAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]

func (a *loopAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]

Important Implementation Details


Interaction with Other System Components


Visual Diagram: LoopAgent Class Structure and Execution Flow

classDiagram
class loopAgent {
-maxIterations: uint
+Run(ctx: InvocationContext) Seq2[Event, error]
}
loopAgent ..> agent.Agent : implements
flowchart TD
Start[Start Loop]
IterCheck{MaxIterations reached?}
RunSubs[Run Sub-Agents Sequentially]
YieldEv[Yield Events to Caller]
EscalateCheck{Event Escalate?}
Terminate[Terminate Loop]
NextIter[Next Iteration]
Start --> IterCheck
IterCheck -- No --> RunSubs
RunSubs --> YieldEv
YieldEv --> EscalateCheck
EscalateCheck -- Yes --> Terminate
EscalateCheck -- No --> NextIter
NextIter --> IterCheck
IterCheck -- Yes --> Terminate

This flowchart illustrates the core execution logic of the LoopAgent:


References to Related Topics


This documentation covers the purpose, design, and detailed behavior of the LoopAgent as implemented in agent.go, highlighting its role in iterative agent workflows within the broader AI agent framework.