Skip to content

Commit d217eee

Browse files
damianmomotgooglecopybara-github
authored andcommitted
docs: clarify LlmAgent composition for workflow agents
Document on ParallelAgent, SequentialAgent, and LoopAgent that they do not transfer control back to parent LlmAgent orchestrator after they complete, and that wrapping with a SequentialAgent is the recommended pattern to chain a workflow agent's results into a follow-up step. This matches the behavior of adk-python. PiperOrigin-RevId: 922007532
1 parent d3e7f31 commit d217eee

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

core/src/main/java/com/google/adk/agents/LoopAgent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
*
3131
* <p>The loop continues until a sub-agent escalates, or until the maximum number of iterations is
3232
* reached (if specified).
33+
*
34+
* <p><b>Composition:</b> a {@code LoopAgent} does not transfer control back to a parent {@link
35+
* LlmAgent}, so an LlmAgent orchestrator that transfers to a {@code LoopAgent} cannot react to the
36+
* loop's results afterwards. To run a loop and then react to its result, place the {@code
37+
* LoopAgent} and the follow-up agent as siblings inside a {@link SequentialAgent} (used as the root
38+
* or transferred-to agent).
3339
*/
3440
public class LoopAgent extends BaseAgent {
3541
private static final Logger logger = LoggerFactory.getLogger(LoopAgent.class);

core/src/main/java/com/google/adk/agents/ParallelAgent.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@
3434
* <p>This approach is beneficial for scenarios requiring multiple perspectives or attempts on a
3535
* single task, such as running different algorithms simultaneously or generating multiple responses
3636
* for review by a subsequent evaluation agent.
37+
*
38+
* <p><b>Composition:</b> a {@code ParallelAgent} does not transfer control back to a parent {@link
39+
* LlmAgent}, so an LlmAgent orchestrator that transfers to a {@code ParallelAgent} cannot react to
40+
* the parallel results afterwards. To follow a fan-out with an aggregation/follow-up step, place
41+
* the {@code ParallelAgent} and the follow-up agent as siblings inside a {@link SequentialAgent}
42+
* (used as the root or transferred-to agent):
43+
*
44+
* <pre>{@code
45+
* SequentialAgent.builder()
46+
* .subAgents(ImmutableList.of(parallelDataAgent, aggregatorLlmAgent))
47+
* .build();
48+
* }</pre>
49+
*
50+
* <p>The {@code SequentialAgent} runs the {@code ParallelAgent} first, then runs {@code
51+
* aggregatorLlmAgent}, which can see the parallel sub-agents' events in the session.
3752
*/
3853
public class ParallelAgent extends BaseAgent {
3954

core/src/main/java/com/google/adk/agents/SequentialAgent.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

25-
/** An agent that runs its sub-agents sequentially. */
25+
/**
26+
* An agent that runs its sub-agents sequentially.
27+
*
28+
* <p><b>Composition:</b> a {@code SequentialAgent} does not transfer control back to a parent
29+
* {@link LlmAgent}, so an LlmAgent orchestrator that transfers to a {@code SequentialAgent} cannot
30+
* react to its results afterwards. Use {@code SequentialAgent} itself as the root or transferred-to
31+
* agent and place any follow-up LlmAgent as the next sibling sub-agent.
32+
*/
2633
public class SequentialAgent extends BaseAgent {
2734

2835
private static final Logger logger = LoggerFactory.getLogger(SequentialAgent.class);

0 commit comments

Comments
 (0)