Introduction
In our previous module, we engineered a sophisticated dynamic tool selection mechanism, enabling our agent to intelligently choose from a vast library of tools. This empowers the agent to be highly effective in reactive, single-step scenarios. However, many real-world business processes—from processing an insurance claim to orchestrating a software deployment—are not single, reactive steps. They are multi-stage workflows with defined dependencies, handoffs, and predictable execution paths.
This lesson marks our entry into a new module, Orchestration with Workflow Graphs. We will shift our focus from the single agent's reactive loop to orchestrating complex, multi-step tasks. Our goal is to model an agentic workflow as a Directed Acyclic Graph (DAG). This powerful abstraction, common in distributed systems and data engineering, will allow us to design and execute reliable, predictable, and even parallel agentic processes. We will pay special attention to a crucial engineering principle: defining nodes with typed inputs and outputs, ensuring that data flows between tasks reliably.
Part 1: From Autonomous Agent to Structured Workflow
So far, our agent has operated on an observe-think-act loop. This provides maximum autonomy, as the LLM decides what to do at every step. While flexible, this can be a drawback for processes that require high reliability and predictability. You don't want an agent autonomously "re-thinking" whether it should seek manager approval for an expense report if business rules strictly require it.
This is where workflow orchestration comes in. Instead of a single autonomous agent, we can compose a system of specialized agents or tasks into a predefined structure. The Directed Acyclic Graph (DAG) is the canonical data structure for this.
A DAG consists of:
- Nodes (or Vertices): These represent units of work. In our context, a node could be a call to an LLM, a tool execution, or a call to another specialized agent.
- Edges (or Arcs): These are directed connections between nodes that represent dependencies. An edge from node A to node B means that node B cannot start until node A has successfully completed.
- Acyclic: The "Acyclic" property is crucial; it means there are no circular dependencies, guaranteeing that the workflow will eventually terminate and not get stuck in an infinite loop.
This concept is foundational in many areas of software engineering. For a brief overview of how DAGs are used in a familiar context, watch the introduction to this video on designing a distributed job scheduler.
20: Distributed Job Scheduler | Systems Design Interview Questions With Ex-Google SWE
This video on systems design uses a job scheduler as an example to explain the fundamental purpose of a DAG for managing dependencies.
Watch the initial segment from the beginning, where the concept of a DAG is introduced as a graph of dependent jobs. Then, note the requirements for running DAGs mentioned around this timestamp.
By defining a workflow as a DAG, we trade some of the agent's autonomy for system-level predictability, reliability, and observability.
Part 2: Representing a Workflow as a DAG
Let's visualize a simple agentic workflow as a DAG. Imagine an agent tasked with writing an article. A possible workflow could be:
- Generate a table of contents.
- Based on the table of contents, generate the full article content.
- Review the generated content. If it passes a quality check, the process ends. If it fails, revise the content and then end.
This can be represented graphically.

To make this executable by a system, we need a machine-readable definition. A common approach is to use a declarative format like YAML or JSON. This separates the workflow definition from the execution logic, a core principle in systems like Kubernetes, Ansible, and Apache Airflow.
The following article explores several agentic patterns and provides an excellent example of a DAG-based orchestration system.
Designing Cognitive Architectures: Agentic Workflow ...
This article details "Pattern 8: DAG Orchestration," which uses a central Coordinator agent to execute a workflow defined in a YAML file. This is a practical and powerful model for structuring agentic systems.
Please read the section titled DAG Orchestration. As you read, focus on these key elements: The role of the Coordinator agent as the execution engine. The use of specialized sub-agents (e.g., CollectAgent, SummarizeAgent) as the nodes in the graph. The structure of the declarative YAML file, noting how tasks and their dependencies are defined.
The article's example demonstrates a key advantage of DAGs: parallelism. Since Task 3 (extract) and Task 4 (summarize) do not depend on each other, a smart orchestrator could run them in parallel to save time.
Part 3: The Critical Principle of Type Alignment
Defining dependencies is only half the story. For Node B to use the result of Node A, it must understand the format and meaning of Node A's output. Simply connecting them is not enough if Node A outputs a string and Node B expects a list of numbers.
This brings us to the crucial concept of typed inputs and outputs, or type alignment. In a robust workflow, the output type of a parent node must match the expected input type of its child node. This allows for validation before execution, dramatically reducing runtime errors.
This idea will be very familiar from your work with statically-typed programming languages. The following resource, from the documentation of a Go-based orchestration framework, explains this principle with exceptional clarity.
Eino: Orchestration Design Principles | CloudWeGo
This document discusses the design principles of an orchestration framework built in Go, a statically typed language. Its emphasis on "Type Alignment" is a masterclass in building reliable, composable systems.
First, read the introduction and the section Type Alignment as the Basic Principle. The analogy of "building blocks" is a perfect mental model. Next, read the section Type Alignment in Graph. This explains exactly how type compatibility is enforced for nodes connected by edges in a graph.
This principle of type alignment is the bridge connecting our work in Module 1 (Structured Outputs) to our current module on orchestration. By forcing our LLM agents to produce well-defined, structured objects (like Pydantic models), we create outputs that can be reliably consumed by downstream nodes in a workflow graph.

Part 4: A First-Principles Implementation Model
How would we build a simple DAG orchestrator from scratch? We can draw inspiration from the design of distributed job schedulers. Let's outline the core components and logic.
The heart of a DAG orchestrator is a state manager and an execution loop. A database is often used to track the state of the graph and its individual nodes. The following video provides a surprisingly detailed and relevant model for this.
20: Distributed Job Scheduler | Systems Design Interview Questions With Ex-Google SWE
This video discusses the database schema and logic required to schedule and execute DAG-based jobs. This provides a fantastic low-level model for our own implementation.
Watch the segment from scheduling dag jobs. The presenter outlines a database-centric approach to managing DAG execution. Pay close attention to: The proposed table structure with fields for job_id, cron_schedule (for root nodes), children, and a list of dependencies. The concept of an epoch to track the state of a specific run and its dependencies. The core logic: when a job finishes, it updates its children, and if a child's dependencies are all met for the current epoch, it becomes runnable. Then, watch the summary of this process from the formalized process. This cleanly recaps the execution flow.
Let's formalize this into a first-principles algorithm for a DAG orchestrator:
- State Representation: A persistent store (e.g., a database table or a Redis hash) holds the state of each task run. This includes its
status(e.g.,PENDING,RUNNING,COMPLETED,FAILED), its dependencies, and its output once completed. - Initialization: The orchestrator loads the DAG definition and creates initial state entries for a new run, marking root nodes (those with no dependencies) as
RUNNABLE. - Execution Loop:
a. The orchestrator polls the state store for tasks withstatus = RUNNABLE.
b. For each runnable task, it dispatches the job to a worker process (this could involve putting it on a message queue). The task status is updated toRUNNING.
c. When a worker completes a task, it writes the result back to the state store and updates the status toCOMPLETED.
d. The orchestrator (or the worker itself) then looks up the completed task's children in the DAG definition.
e. For each child task, it checks if all of its dependencies are nowCOMPLETED.
f. If a child's dependencies are all met, its status is updated toRUNNABLE. - Termination: The loop continues until all tasks are in a terminal state (
COMPLETEDorFAILED).
This model aligns perfectly with your background in event-driven architectures and message queues.
Part 5: Representing Nodes and Data Flow in Code
Finally, let's connect these concepts to a Python implementation. While we are avoiding direct use of frameworks, examining their design can be highly instructive. The pydantic-graph library provides a clear example of how to use Python's type hints to enforce type alignment.
Create Your Own AI Agent Pipeline With Pydantic Graph | PydanticAI Tutorial
This tutorial for the Pydantic Graph library demonstrates how to define nodes as classes with typed run methods, making the flow of data explicit and safe.
Watch the section explaining the node definitions from I will create three nodes. Notice how the run method's return type annotation (-> B or -> Union[C, End]) explicitly declares what the next state can be and what data is passed. Then, skim through the more complex email agent example starting at a different example. Observe how custom Pydantic models like EmailRequiresWrite and EmailOkay are used as return types to direct the flow of the graph based on the agent's output. This is a concrete implementation of a typed, stateful graph.
The key takeaway from this approach is that a node can be represented as a class or function whose signature defines its typed inputs and outputs. An orchestrator can then inspect these signatures to validate the graph's integrity before execution.
For example, a SummarizeAgentNode might be a function with this signature:def summarize(document: str, style: str) -> str:
The orchestrator would know that any node feeding into this one must provide two str outputs, and any node that depends on it must accept a single str input.
Conclusion
In this lesson, we have laid the conceptual and architectural groundwork for building complex, multi-step agentic systems. We've moved beyond the simple reactive loop and adopted the Directed Acyclic Graph as a formal model for orchestrating workflows.
Key Takeaways:
- DAGs for Reliability: Modeling workflows as DAGs provides structure, predictability, and reliability, which are essential for production-grade applications.
- Nodes and Edges: A DAG consists of nodes (tasks) and edges (dependencies), defining a clear, non-circular execution path.
- Declarative Definitions: Using formats like YAML to define the graph separates the workflow's logic from its execution, enabling flexibility and easier maintenance.
- Type Alignment is Critical: Enforcing that a node's output type matches its successor's input type is a cornerstone of robust workflow design. This prevents runtime errors and makes the system easier to reason about.
- Execution Model: A DAG orchestrator can be implemented with a state store and an execution loop that tracks dependencies and dispatches runnable tasks.
We have defined the static structure of our workflows. But what makes them truly powerful is the ability to adapt their path based on runtime results.
Preview of the Next Lesson: We will build directly on today's foundation by learning how to implement conditional routing nodes. These are special nodes that act like an if statement within the graph, directing the flow of execution down different paths based on LLM outputs or predefined business rules. This will introduce dynamic behavior to our structured workflows, giving us the best of both worlds: reliability and flexibility.