> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Parallel and Looping Agents

> Extending workflow graphs with parallel fan-out and fan-in plus looping cycles, showing joins, routers, and termination strategies for reliable, efficient support agent workflows.

Your support agent is modeled as a directed graph: nodes represent steps and edges represent transitions. Basic graphs can choose one path over another, but real support workflows commonly need two additional capabilities:

* Run several independent checks at the same time (parallel / fan-out and fan-in).
* Revisit and revise a result multiple times until it passes review (loops / cycles).

This article expands the workflow graph with both patterns: running several nodes concurrently and joining their results, and adding back edges so a node can repeat until the work is acceptable. It also shows how to implement a router that terminates loops reliably.

## Parallel work (fan-out / fan-in)

Scenario: a customer asks, "Is something down?" The agent must search three independent sources — help docs, past support tickets, and the status page. None of these searches depends on another, so they can be executed in parallel. Running them sequentially makes the customer wait for search 1, then 2, then 3; running them concurrently means the customer only waits for the slowest search.

This is the fan-out / fan-in pattern:

* Fan-out: several edges leave `START` and each edge goes to its own node. Those nodes run concurrently.
* Fan-in (join): a join node waits for every branch to complete and merges the results so the workflow continues as a single flow.

Example: create a join node and wire three branches into it.

```python theme={null}
from google.adk import Workflow
from google.adk.workflow import JoinNode

# Create a join node that waits for all branches to complete
research_join = JoinNode(name='research_join')

workflow = Workflow(
    name='support_research',
    edges=[
        ("START", help_docs),
        (help_docs, research_join),

        ("START", support_tickets),
        (support_tickets, research_join),

        ("START", status_page),
        (status_page, research_join),

        (research_join, answer_writer),
    ],
)
```

At runtime the workflow starts all three searches together. Branches may finish in any order (for example, support tickets first). The join collects all results and hands them to the `answer_writer`. The customer waits only as long as the slowest branch, not the sum of all branches.

When to use fan-out / fan-in:

* Independent tasks that can run concurrently (searches, diagnostics, parallel checks).
* When you want to reduce end-to-end latency by overlapping I/O-bound or independent CPU work.

## Looping (cycles / back edges)

Some work requires iteration: draft → review → revise → review again. By default a workflow progresses forward without returning. To support revision you add a backward edge (a cycle). That back edge lets the flow repeat a node until the result is acceptable.

Key points:

* Add a router (routing function) that decides whether to continue or finish.
* Always include a termination condition (a passing review, a maximum iteration count, or both) to prevent infinite loops.

Example: a router function plus edges that include a back edge to close the loop.

```python theme={null}
def router(review: str) -> str:
    if review == "reply passes":
        return "done"
    return "revise"

workflow = Workflow(
    edges=[
        ("START", reply_writer),
        (reply_writer, reviewer),
        (reviewer, router),
        (router, {"done": finalizer, "revise": reviser}),
        (reviser, reviewer),  # back edge: reviser -> reviewer (closes the loop)
    ],
)
```

Notes:

* The `router` is an ordinary Python function (a routing node). It returns a key that the runtime uses to select the downstream branch.
* The loop is: `reply_writer` -> `reviewer` -> `router` -> (`done` -> `finalizer` OR `revise` -> `reviser` -> reviewer).
* Termination examples: `review == "reply passes"`, a maximum iteration counter, or an elapsed time limit.

![A dark-themed flowchart titled "Write, Review, Check – a Loop" showing a reply-writer icon feeding into Reviewer, Check, Reviser (looping) and finally a Finalizer. A sample reply bubble reads "Checkout is down / Fixed in 3 days / Try the other link."](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/ADKs-Workflow-Runtime/Parallel-and-Looping-Agents/write-review-check-loop-flowchart.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=aeeed584d3e70089f5becffba063d260)

Always include a clear termination condition for loops (for example, a passing review result, a maximum iteration limit, or a timeout) to avoid infinite revision cycles.

## Agentic templates vs explicit Workflow graphs

The ADK exposes higher-level agentic shapes that make common patterns easier to express, but you can always drop down to a raw graph for full control.

* Parallel helpers (e.g., `ParallelAgent`) accept a list of sub-agents and handle fan-out and the join automatically.
* Loop wrappers (e.g., `LoopAgent`) run a set of agents repeatedly up to a configured iteration limit.

Use agentic templates when you want convenience and fast authoring. Use explicit `Workflow` nodes when you need custom join behavior, nonstandard routing keys, or termination based on content rather than iteration counts.

## Common workflow shapes

| Shape | When to use | Example |
| - | - | - |
| Straight line (sequential) | Simple, ordered tasks where each step depends on the previous | `START -> reply_writer -> reviewer -> finalizer` |
| Fork (branching) | Conditional work where different branches run depending on a decision | `router -> {"billing": billing_agent, "tech": tech_agent}` |
| Fan-out / Fan-in (parallel) | Independent tasks that can run concurrently, then be combined | `START -> help_docs/support_tickets/status_page -> JoinNode -> answer_writer` |
| Loop (cycle) | Tasks that require revision until a condition is met | `reply_writer -> reviewer -> router -> revise -> reviewer` |

## Best practices

* Fan-out when tasks are independent to reduce latency.
* Use a join node to collect and normalize results from parallel branches.
* Implement routers as pure functions that return deterministic routing keys.
* Always include a termination condition for loops (passing criteria, iteration limit, or timeout).
* Prefer agentic templates for standard patterns and raw graphs for custom control.

## Links and references

* [Google ADK Workflows (reference)](https://developers.google.com/adk/workflows)
* [Workflow patterns (overview)](https://en.wikipedia.org/wiki/Workflow)
* [Designing resilient workflows (patterns and anti-patterns)](https://martinfowler.com/articles/enterprise-patterns.html)

![A dark-themed infographic titled "Three Shapes, Drawn Before the Run" showing workflow diagrams (Straight Line, Fork, Fan-Out or Loop) with boxes and arrows labeled things like Reply writer, Reviewer, Finalizer, Check, Reviser, and Your agent. It illustrates how tasks flow between these roles/components.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/ADKs-Workflow-Runtime/Parallel-and-Looping-Agents/three-shapes-workflow-infographic.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=f8005f4c7cf0fac8421d3617158f2d43)

Whenever your graph includes independent work, fan it out. When work may need refinement or human review, add a loop with a well-defined termination condition. These patterns help your agent run faster and iterate safely until the result is good enough.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/google-adk-2-0/module/913ea931-c1bd-4df3-8a96-c2894e9df0d2/lesson/8ecb25b5-7e33-442c-a1a3-7dfa965a9a49)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.