> ## 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.

# Static vs Dynamic Workflow Graphs

> Compares static and dynamic workflow graphs, demonstrating @node and ctx.run_node to implement data driven loops and runtime-determined parallelism in Python workflows.

Let's reuse the support-reply workflow from an earlier example.

You designed three steps: write a reply, check its quality, and revise it — and wired them in a fixed pipeline. That is a static graph: you handed the workflow a list of connections, and the runtime follows the directions you gave it. You can read the whole structure before any node runs.

First, the static version:

```python theme={null}
workflow = Workflow(
    name="support_reply",
    edges=[
        ("START", writer),
        (writer, quality_check),
        (quality_check, reviser),
    ],
)
```

This static graph is explicit and easy to reason about: Start → Writer → Quality check → Reviser. But it has a clear limitation: the reviser runs exactly once. What if the first revision still has problems? The static graph doesn't care — it reaches the end and stops.

Real support replies often require multiple revision passes. You revise, the reply changes, the check must run again, and you can't know in advance how many iterations will be necessary — 0, 1, 2, or more.

![A dark-themed infographic titled "How Many Passes?" showing a refund ticket and a reply message alongside a simple workflow: Start → Writer → Quality check → Reviser → End. The graphic illustrates a ticket (#1042 Refund) and an approval reply ("Your refund is approved — Refund in 5 days").](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/ADKs-Workflow-Runtime/Static-vs-Dynamic-Workflow-Graphs/how-many-passes-refund-workflow.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=87b6ea94cfb47bc2d8720f64be448f1d)

Instead of encoding the loop into the graph, you put the loop in Python. Mark a regular Python function with a small tag — the `@node` decorator. The decorator labels the function so the runtime treats it as a workflow step.

Inside that function, `ctx.run_node(...)` returns an awaitable you can `await` to run another node and receive its result. Use standard Python control flow (for example, a `while` loop) to decide at runtime how many times to call other nodes. You can also `yield` intermediate state so traces show each pass.

Mark functions with `@node` to make them workflow steps. Use `await ctx.run_node(...)` to execute other nodes from inside a node. Emitting intermediate `yield` values lets traces report progress across multiple passes.

Here is an example `async` node that implements dynamic behavior: it runs `writer` → `quality_check`, then repeats `reviser` → `quality_check` until no issues remain. The snippet emits intermediate state so traces record pass 1, pass 2, etc.; the final emitted state contains the completed reply.

```python theme={null}
@node
async def support_reply(ctx, ticket):
    reply = await ctx.run_node(writer, ticket)
    issues = await ctx.run_node(quality_check, reply)
    passes = 1
    while issues:
        reply = await ctx.run_node(reviser, reply, issues)
        issues = await ctx.run_node(quality_check, reply)
        passes += 1
        yield {
            "passes": passes,
            "reply": reply,
            "issues": issues,
        }
    # final emitted state includes the completed reply
    yield {
        "passes": passes,
        "reply": reply,
        "issues": issues,
        "final": True,
    }
```

A `while` loop decides the structure at runtime: as long as `quality_check` reports `issues`, you run the `reviser` and check again. Track `passes` and include it in yielded state so traces show each revision round and the evolving reply.

Conceptually, consider this run:

* The writer creates the first draft.
* The quality check finds two issues (e.g., missing API step and missing refund steps).
* The reviser runs, then the quality check runs again and now finds one issue (tone too formal).
* The reviser runs again, and a third check returns zero issues — the loop ends.

This ticket needed two revision passes. Another ticket might need four; a simple one might need zero.

![A diagram titled "Only the Ticket Decides" showing two teal tickets (#1042 Refund and #1043 Billing) flowing into a "Quality check" box, which sends and receives arrows to a "Reviser" box. The tickets have small status dots and icons beside them.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/ADKs-Workflow-Runtime/Static-vs-Dynamic-Workflow-Graphs/tickets-quality-check-reviser-diagram.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=99f47c2b5942b0356bf2104d95dcedda)

Key takeaway: nothing in the static graph decides how many times `reviser` runs — the data does. That is the first dynamic case: the data determines how many loop iterations the node performs at runtime.

There is a second dynamic case: the data can also decide how many things run in parallel. For example, a ticket might include multiple subtasks that you want to run concurrently, but only after you know how many subtasks exist. You must defer that decision until the node runs and inspects the data.

Below is a short comparison to clarify trade-offs.

| Pattern | When to use | Pros | Cons |
| - | - | - | - |
| Static graph | Fixed pipeline with known structure | Easy to visualize and reason about ahead of time | Inflexible when runtime decisions or repeated passes are required |
| Dynamic node-managed flow | When the runtime data decides loops or parallelism | Fully data-driven; can loop or spawn parallel work based on input | Requires careful async control and correct use of runtime APIs (`ctx.run_node`) |

Be careful with common Python idioms that eagerly evaluate or spawn tasks (for example, building a list of awaited calls or using list comprehensions that call `ctx.run_node` immediately). If you create tasks or call nodes before you intend to, you can accidentally hardcode parallelism or freeze the runtime structure. Always `await` or construct tasks intentionally inside the node when you want them to run.

References and further reading:

* Python async/await guide: [https://docs.python.org/3/library/asyncio.html](https://docs.python.org/3/library/asyncio.html)
* Workflow and orchestration patterns: [https://en.wikipedia.org/wiki/Workflow\_(computing)](https://en.wikipedia.org/wiki/Workflow_\(computing\))

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/google-adk-2-0/module/913ea931-c1bd-4df3-8a96-c2894e9df0d2/lesson/d8c70c2d-295d-4d59-a174-4423a968e463)


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