Skip to content
Visual explainer 3h 30m to learn 5 steps

Sequential, parallel and loop workflows in ADK

Build fixed routing and data handoffs with the workflow primitives in the pinned release, including sequential, parallel and bounded loop steps.

By Invicly Tech, , in Google ADK with Python for AI Agents. About 11 minutes to read.

Topic 10 of 29

In short

A workflow is a route you fix in code: which step runs first, what runs next, what runs in parallel and when a loop stops. In ADK 2.x you build it with the Workflow class, a list of edges between nodes, and nodes that are plain Python functions or agents. Because the code decides the order, a workflow is cheaper, faster and easier to test than letting a model choose every step.

Think of it like this: A workflow is a railway timetable: trains always run the same lines, and the only choices are the switches you built into the track.

Explained simply

Many support questions follow the same path every time. Read the question, decide if it is about a ticket or about the product, look up the right thing, then write a reply. You do not need a model to choose that path. You can write it down in code, and the code will follow it the same way every time.

In ADK 2.x that written-down path is a Workflow. You list nodes, which are the steps, and edges, which say what comes after what. A node can be a plain Python function, such as a classifier that checks for a ticket ID, or an agent, such as a writer that turns facts into a friendly sentence.

Three shapes cover most needs. A chain runs steps one after another. A fan-out runs several steps at the same time and a join waits for them all. A loop goes back to an earlier step, for example to retry a draft, and stops when a condition says so.

Older tutorials use SequentialAgent, ParallelAgent and LoopAgent for these shapes. They still exist in ADK 2.x but are marked deprecated, so new code should use Workflow. When you read an older example, map each of those classes to the matching Workflow shape.

Press Play on a diagram to walk through it one step at a time.

FlowA fixed support workflow
  1. 1
    START

    The user message enters

  2. 2
    classify

    Function node returns a route

  3. 3
    lookup_ticket or search_docs

    Chosen by the route

  4. 4
    write_reply

    Agent node reads state and drafts

  5. 5
    Final output

    Saved as an event in the session

CompareEdge shapes in a Workflow
Chain
  • ("START", a, b, c)
  • Runs a, then b, then c
  • Each output is the next input
  • Replaces SequentialAgent
Fan-out and join
  • Several rows start from the same node
  • Branches run concurrently
  • JoinNode waits for all of them
  • Replaces ParallelAgent
Routed loop
  • Node returns Event(route=...)
  • A dict maps routes to targets
  • A back-edge forms the loop
  • You must bound it yourself
CycleA draft-and-check loop with a hard stop
  1. 1
    draft

    Writes a reply, rounds += 1

  2. 2
    check

    Validates citations and length

  3. 3
    Route retry

    Only if rounds < 3

  4. 4
    Route ok or give_up

    Leaves the loop

  5. Back to step 1

In depth

Nodes, edges and what flows between them

A Workflow takes a name and a list of edges. Each edge row is a tuple that starts at the special "START" node or at an earlier node, and lists nodes in order. A plain function becomes a node automatically. It receives node_input, which is the previous node's output; the first node receives the user's message as a Content object. A parameter annotated as Context receives the run context, other parameters are filled from session state by name, and a missing value is an error. A function can return a value, which becomes its output, or an Event, which can carry output, a route and state changes at once. An agent used as a node receives the previous output as its input message, and its text reply becomes its output. Because outputs flow along edges, most data handoffs need no shared state at all; write to state only for values that later, non-adjacent steps or agent instructions need.

  • from google.adk import Event, Workflow
  • Return Event(output=..., state={...}, route=...)
  • Agent nodes read state through {key} in their instruction
  • Set include_contents="none" on agent nodes that only need their input

Routes: choosing a branch in code

To branch, a node returns Event(route="ticket") and the edge row uses a dict that maps route values to target nodes, such as {"ticket": lookup_ticket, "docs": search_docs}. Route values can be strings, numbers or booleans. DEFAULT_ROUTE from google.adk.workflow names a fallback branch for values that match nothing else, which is a good place for a safe reply. A node may also return a list of routes to start several branches at once. Routing in code is fast and free: a regular expression for ticket IDs costs microseconds, while a model-based router costs a model call, latency and some error rate. Use a model only when the decision really needs language understanding.

MapRoute table for the support workflow
classify returns a route
  • ticket
    • lookup_ticket
    • write_reply
  • docs
    • search_docs
    • write_reply
  • DEFAULT_ROUTE
    • safe_reply

Fan-out and join

List several edge rows that start from the same node and their targets run concurrently. To collect the results, point every branch at the same JoinNode, imported from google.adk.workflow. The join waits until all incoming branches finish and passes their outputs to the next node as one dict keyed by node name, for example {"search_docs": [...], "search_faq": [...]}. Parallel branches cut latency when the steps are independent and slow, such as two lookups that each take a second. They do not cut cost: each branch still runs. Give branches distinct state keys so they never overwrite each other, or better, rely on their outputs instead of state.

Bounded loops and older loop examples

A loop is a routed edge back to an earlier node: check returns "retry" to go back to draft, or "ok" to continue. ADK rejects a cycle made only of unconditional edges, but it does not count iterations for you; the docs state that a graph cycle is not bounded automatically. So keep a counter in state, increment it in the looping node, and route to an exit node when it reaches the limit. The deprecated LoopAgent had max_iterations built in, which is why older examples look shorter. For complex loops, ADK also offers dynamic workflows: a node decorated with @node(rerun_on_resume=True) calls other nodes with await ctx.run_node(...) inside an ordinary Python while or for loop that you bound yourself.

  • Every cycle needs at least one routed edge
  • Count rounds in state, exit at the limit
  • Dynamic workflows: ctx.run_node inside a normal loop

Learn it in 5 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Chain steps with a sequential workflow~40m
  2. 2
    Fan out with a parallel workflow~40m
  3. 3
    Bound a loop with max iterations~40m
  4. 4
    Pass data between steps through state~40m
  5. 5
    Use graph routes where supported~40m

Key points

  • Build workflows with Workflow(name=..., edges=[...]) from google.adk; edge rows start at "START".
  • A node's output becomes the next node's input; Event(state={...}) also writes values to session state.
  • Fan out by listing several rows from the same node, and gather results with JoinNode, which passes a dict keyed by node name.
  • Graph loops are not bounded automatically: count iterations in state and route to an exit when the limit is reached.
  • SequentialAgent, ParallelAgent and LoopAgent from older examples are deprecated in ADK 2.x in favour of Workflow.

Key terms

Tap a card to flip it. 0/5 known

Examples

python
from google.adk import Event, Workflow

def classify(node_input) -> Event:
    text = node_input.parts[0].text
    route = "ticket" if "T-" in text.upper() else "docs"
    return Event(route=route, state={"question": text})

root_agent = Workflow(
    name="support_flow",
    edges=[
        ("START", classify, {"ticket": lookup_ticket, "docs": search_docs}),
        (lookup_ticket, write_reply),
        (search_docs, write_reply),
    ],
)

Routing without a model call. lookup_ticket, search_docs and write_reply are function or agent nodes defined elsewhere.

Two lookups in parallel, joined

Both searches start from START at the same time. The join passes a dict keyed by node name to merge, which picks the passages to use. This replaces a ParallelAgent followed by a merging agent.

python
from google.adk import Workflow
from google.adk.workflow import JoinNode

def search_docs(node_input) -> list:
    return ["docs/account#reset-password"]

def search_faq(node_input) -> list:
    return ["faq/login#locked-out"]

join = JoinNode(name="join")

def merge(node_input: dict) -> list:
    return node_input["search_docs"] + node_input["search_faq"]

root_agent = Workflow(name="lookups", edges=[
    ("START", search_docs, join), ("START", search_faq, join), (join, merge)])

A loop that stops after three rounds

draft counts rounds in state. check routes back to draft only while the reply is not good and fewer than three rounds have run; otherwise it exits to done or give_up. Without the counter, a check that never passes would loop with no end.

python
from google.adk import Context, Event, Workflow

MAX_ROUNDS = 3

def draft(node_input, ctx: Context) -> Event:
    rounds = ctx.state.get("rounds", 0) + 1
    return Event(output=f"draft {rounds}", state={"rounds": rounds})

def check(node_input: str, ctx: Context) -> Event:
    if looks_good(node_input):
        return Event(route="ok")
    return Event(route="retry" if ctx.state["rounds"] < MAX_ROUNDS else "give_up")

root_agent = Workflow(name="drafting", edges=[
    ("START", draft, check),
    (check, {"retry": draft, "ok": done, "give_up": safe_reply})])

Quick reference

  • Workflow(name=..., edges=[("START", a, b), ...]) from google.adk.
  • Node output becomes the next node's node_input.
  • Event(route="x") plus {"x": node} in an edge row makes a branch.
  • JoinNode passes a dict keyed by branch node name.
  • Graph cycles are not bounded automatically: count rounds in state.
  • SequentialAgent, ParallelAgent, LoopAgent: deprecated in ADK 2.x, use Workflow.
  • Dynamic workflow: @node(rerun_on_resume=True) and await ctx.run_node(...).

Common mistakes

  • Copying a LoopAgent example from an older tutorial and expecting max_iterations on a graph loop. In Workflow you bound the loop yourself.
  • Building a cycle from only unconditional edges. ADK rejects it when the Workflow is built; a cycle needs at least one routed edge.
  • Passing a whole conversation between nodes when the next step only needs a ticket ID and a status.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1How does a function node choose which branch runs next?

Question 2

2What does the node after a JoinNode receive?

Question 3

3You copy an old LoopAgent example with max_iterations=3 into a Workflow. What must you add?

Question 4

4When does fan-out reduce latency?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Rewrite this older chain as a Workflow: SequentialAgent(sub_agents=[classify_agent, lookup_agent, reply_agent]).

  2. Exercise 2medium

    Build the routed support workflow with a ticket branch, a docs branch and a DEFAULT_ROUTE branch to safe_reply. Run it in adk web with three test messages and check which nodes appear in the trace.

  3. Exercise 3hard

    Check three ticket IDs from one message in parallel with a dynamic workflow, and cap it at five IDs whatever the user sends.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Will SequentialAgent stop working?
In ADK 2.x it still runs but is marked deprecated in favour of Workflow, with removal planned for a future release. Use Workflow for new code and migrate old code when you touch it.
Can an agent be a node in a Workflow?
Yes. Put the agent in an edge row like a function. It receives the previous node's output as its input and its reply becomes its output.
Do I need graph workflows or dynamic workflows?
Use a graph when the steps and branches are known in advance and you want them visible as edges. Use a dynamic workflow when the number of steps depends on data, such as one lookup per ticket ID.

Go deeper

ShareXLinkedInWhatsApp