File tree Expand file tree Collapse file tree
core/src/main/java/com/google/adk/agents Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 with {@link LlmAgent}s:</b> a {@code LoopAgent} does not transfer control back
35+ * to a parent {@link LlmAgent}. To react to loop results, place the {@code LoopAgent} and the
36+ * follow-up {@link LlmAgent} as siblings inside a {@link SequentialAgent}. Loop sub-agents publish
37+ * via {@code outputKey} and the follow-up reads via {@code {key}} placeholders in its instruction:
38+ *
39+ * <pre>{@code
40+ * var refiner = LlmAgent.builder().name("refiner").model("gemini-flash-latest")
41+ * .instruction("Refine: {draft?}").outputKey("draft").build();
42+ * var publisher = LlmAgent.builder().name("publisher").model("gemini-flash-latest")
43+ * .instruction("Publish: {draft}").build();
44+ *
45+ * var root = SequentialAgent.builder().name("root").subAgents(
46+ * LoopAgent.builder().name("loop").subAgents(refiner).maxIterations(3).build(),
47+ * publisher).build();
48+ * }</pre>
3349 */
3450public class LoopAgent extends BaseAgent {
3551 private static final Logger logger = LoggerFactory .getLogger (LoopAgent .class );
Original file line number Diff line number Diff line change 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 with {@link LlmAgent}s:</b> a {@code ParallelAgent} does not transfer control
39+ * back to a parent {@link LlmAgent}. To follow a fan-out with an aggregation step, wrap both in a
40+ * {@link SequentialAgent} (used as the root or transferred-to agent). Each parallel sub-agent
41+ * publishes via {@code outputKey} and the aggregator reads via {@code {key}} placeholders in its
42+ * instruction:
43+ *
44+ * <pre>{@code
45+ * var contacts = LlmAgent.builder().name("contacts").model("gemini-flash-latest")
46+ * .instruction("List contacts.").outputKey("contacts").build();
47+ * var schedule = LlmAgent.builder().name("schedule").model("gemini-flash-latest")
48+ * .instruction("List schedule.").outputKey("schedule").build();
49+ * var writer = LlmAgent.builder().name("writer").model("gemini-flash-latest")
50+ * .instruction("Write: contacts={contacts}, schedule={schedule}").build();
51+ *
52+ * var root = SequentialAgent.builder().name("root").subAgents(
53+ * ParallelAgent.builder().name("gather").subAgents(contacts, schedule).build(),
54+ * writer).build();
55+ * }</pre>
3756 */
3857public class ParallelAgent extends BaseAgent {
3958
Original file line number Diff line number Diff line change 2222import org .slf4j .Logger ;
2323import 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 with {@link LlmAgent}s:</b> a {@code SequentialAgent} does not transfer control
29+ * back to a parent {@link LlmAgent}. Use it as the root or transferred-to agent and place any
30+ * follow-up {@link LlmAgent} as the next sibling. Upstream publishes via {@code outputKey} and
31+ * downstream reads via {@code {key}} placeholders in its instruction:
32+ *
33+ * <pre>{@code
34+ * var draft = LlmAgent.builder().name("draft").model("gemini-flash-latest")
35+ * .instruction("Draft a summary.").outputKey("draft").build();
36+ * var reviewer = LlmAgent.builder().name("reviewer").model("gemini-flash-latest")
37+ * .instruction("Polish the draft: {draft}").build();
38+ *
39+ * var pipeline = SequentialAgent.builder().name("pipeline").subAgents(draft, reviewer).build();
40+ * }</pre>
41+ */
2642public class SequentialAgent extends BaseAgent {
2743
2844 private static final Logger logger = LoggerFactory .getLogger (SequentialAgent .class );
You can’t perform that action at this time.
0 commit comments