Package com.google.adk.agents
Class LoopAgent
java.lang.Object
com.google.adk.workflow.BaseNode
com.google.adk.agents.BaseAgent
com.google.adk.agents.LoopAgent
- All Implemented Interfaces:
Node
An agent that runs its sub-agents sequentially in a loop.
The loop continues until a sub-agent escalates, or until the maximum number of iterations is reached (if specified).
Composition with LlmAgents: a LoopAgent does not transfer control back
to a parent LlmAgent. To react to loop results, place the LoopAgent and the
follow-up LlmAgent as siblings inside a SequentialAgent. Loop sub-agents publish
via outputKey and the follow-up reads via {key} placeholders in its instruction:
var refiner =
LlmAgent.builder()
.name("refiner")
.model("gemini-flash-latest")
.instruction("Refine: {draft?}")
.outputKey("draft")
.build();
var publisher =
LlmAgent.builder()
.name("publisher")
.model("gemini-flash-latest")
.instruction("Publish: {draft}")
.build();
var loop =
LoopAgent.builder().name("loop").subAgents(refiner).maxIterations(3).build();
var root = SequentialAgent.builder().name("root").subAgents(loop, publisher).build();
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic LoopAgent.Builderbuilder()static LoopAgentfromConfig(LoopAgentConfig config, String configAbsPath) Creates a LoopAgent from configuration.@Nullable Integerprotected io.reactivex.rxjava3.core.Flowable<Event>runAsyncImpl(InvocationContext invocationContext) Agent-specific asynchronous logic.protected io.reactivex.rxjava3.core.Flowable<Event>runLiveImpl(InvocationContext invocationContext) Agent-specific synchronous logic.Methods inherited from class com.google.adk.agents.BaseAgent
afterAgentCallback, beforeAgentCallback, canonicalAfterAgentCallbacks, canonicalBeforeAgentCallbacks, close, findAgent, findSubAgent, fromConfig, parentAgent, parentAgent, rootAgent, runAsync, runLive, runNode, subAgents, toolOriginMethods inherited from class com.google.adk.workflow.BaseNode
config, description, inputSchema, name, outputSchema, rerunOnResume, stateSchema, waitForOutputMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.google.adk.workflow.Node
requiresAllPredecessors
-
Method Details
-
builder
-
fromConfig
public static LoopAgent fromConfig(LoopAgentConfig config, String configAbsPath) throws ConfigAgentUtils.ConfigurationException Creates a LoopAgent from configuration.- Parameters:
config- The agent configuration.configAbsPath- The absolute path to the agent config file.- Returns:
- the configured LoopAgent
- Throws:
ConfigAgentUtils.ConfigurationException- if the configuration is invalid
-
runAsyncImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext) Description copied from class:BaseAgentAgent-specific asynchronous logic.- Specified by:
runAsyncImplin classBaseAgent- Parameters:
invocationContext- Current invocation context.- Returns:
- stream of agent-generated events.
-
runLiveImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext) Description copied from class:BaseAgentAgent-specific synchronous logic.- Specified by:
runLiveImplin classBaseAgent- Parameters:
invocationContext- Current invocation context.- Returns:
- stream of agent-generated events.
-
maxIterations
-