Introduction
In our last lesson, we built a powerful execution engine capable of running independent tasks in parallel using the fan-out/fan-in pattern. This allowed us to optimize workflows you defined manually in code. We are now at a pivotal point where we transition the responsibility of planning from you, the programmer, to the agent itself. This is the leap from executing pre-defined workflows to dynamically generating novel ones.
This lesson addresses the learning outcome: Design a prompt and schema for an LLM to generate a structured execution graph from a task description. You will learn how to instruct a "Planner" LLM to take a high-level user request and decompose it into a formal, machine-readable Directed Acyclic Graph (DAG). We will focus on two critical components: designing the JSON schema that represents the graph's nodes and dependencies, and engineering the prompt that reliably guides the LLM to produce output conforming to that schema. Your background in systems design will be invaluable here, as we are essentially defining the data structures and instruction sets for an autonomous task planner.
Part 1: The Core Concept - From Prompt to DAG
The central idea is to create a specialized agent, often called a Planner or Orchestrator, whose sole job is to think and plan. It doesn't execute tasks itself; instead, it analyzes a user's goal and produces a detailed execution plan for other agents or tools to follow. This plan takes the form of a structured data object, most commonly a DAG, which defines the sequence and parallelism of the required steps.
This architecture is elegantly visualized in the LLMCompiler model.

A systematic approach to this transformation is laid out in the "Prompt2DAG" methodology. While originally designed for data pipelines, its principles are universally applicable to agentic workflows. The initial and most crucial phase is what they call "Pipeline Analysis," which is the process of using LLM prompts to transform a natural language description into a structured JSON representation of the workflow.
A Modular Methodology for LLM-Based Data Enrichment Pipeline ...
This research paper provides a formal methodology for converting prompts to DAGs. We'll focus on the conceptual framework it presents for the analysis phase.
First, read the overview of the Prompt2DAG methodology in the introductory paragraph. This outlines the high-level transformation from language to a structured artifact. Next, read the section "Step 1: Pipeline Analysis" in detail, from its beginning to the end of the third paragraph. Pay close attention to how it advocates for using task decomposition and prompt chaining to break the analysis into focused sub-tasks (like identifying components, analyzing flow, and extracting parameters). This structured, multi-step analysis is a powerful technique for improving the reliability of the generated plan.
Part 2: Designing the Graph Schema
Before we can ask an LLM to generate a plan, we must first define the language of that plan: its schema. This schema is the contract between the Planner agent and the execution engine. It needs to be expressive enough to capture dependencies and parallelism, yet simple enough for an LLM to generate reliably.
A great starting point is a minimalist but powerful schema. Let's examine a practical implementation where the graph is defined as a list of steps.
Build a Planner Agent System with Parallel Execution: Flyte 2.0 Multi ...
This article from Union.ai demonstrates a clean, code-first approach to a planner agent. We will focus on the data structures they use to define the execution plan.
Read the introductory text about the Planner Agent's role. Then, carefully study the Python code block that follows. Focus specifically on the AgentStep and PlannerDecision dataclasses. Notice how this schema represents the DAG: PlannerDecision contains a list of AgentStep objects. These are the nodes of our graph. Each AgentStep has: agent: The name of the worker/tool to execute. task: The natural language instruction for that worker. dependencies: A list of integers. These are the edges of the graph, referencing the 0-indexed position of other steps in the list. An empty list [] signifies a root node that can be executed immediately, enabling parallelism.
This index-based dependency schema is elegant and highly effective. It's a pragmatic choice for a "from scratch" implementation.
For contrast, and to appreciate the potential complexity, the Prompt2DAG paper you read earlier defines a much more exhaustive schema. It includes not just nodes and dependencies, but also global parameters, component-specific configurations, integration points with external systems, and rich metadata.
A Modular Methodology for LLM-Based Data Enrichment Pipeline ...
Let's revisit the research paper to see an example of a more comprehensive, production-grade schema.
Skim through Appendix B. You don't need to memorize every field, but observe the structure. Find the detailed_flow_structure key and see how it defines entry_points and a dictionary of nodes, where each node lists its next_nodes. Compare this named-node approach to the index-based approach from the previous resource. Notice also the separate, detailed sections for parameters and integrations. This gives you a sense of what a mature graph schema might evolve into.
For our purposes, we will proceed with the simpler, index-based dependency list, as it provides all the necessary power for defining complex DAGs without unnecessary overhead.
Part 3: Engineering the Planner Prompt
With a schema in hand, our next task is to create a prompt that instructs the LLM to generate a valid instance of it. This is a delicate art that blends instruction, context, and example. A well-designed prompt is the difference between a reliable planner and a source of constant errors.
The image below shows a common structure for prompts that request structured output. It clearly delineates the agent's persona, its available tools (functions), the required output schema, and the user's input.

The planner_agent function in the Union.ai article provides a masterclass in applying these principles to generate our DAG schema.
Build a Planner Agent System with Parallel Execution: Flyte 2.0 Multi ...
This code contains a complete and highly effective system prompt for a planner agent.
Return to the Python code block in the article. This time, focus entirely on the multi-line string assigned to the system_msg variable. Analyze its components: Role Definition: "You are a routing agent." Context: It dynamically injects the available_agents, giving the LLM its "toolbox." Schema Explanation: It explicitly explains the dependencies field and what it means for parallel vs. sequential execution. Few-Shot Examples: This is the most critical part. It provides clear, annotated examples for: Independent tasks (parallel execution). Dependent tasks (a simple sequence). Cross-agent dependencies. Multiple dependencies (a fan-in pattern). Constraints: "Always include 'dependencies' field... even if empty []." This small instruction prevents ambiguity. This combination of clear rules and varied examples is essential for teaching the LLM the pattern of thinking required to decompose tasks into a graph.
Part 4: Under the Hood: How Structured Output Works
You might wonder how we can suddenly trust an LLM to follow a JSON schema with such high fidelity. This isn't just a result of better models or clever prompting; it's a fundamental change in the generation process itself. Your interest in first principles makes it worthwhile to understand the mechanism of constrained decoding.
Modern LLM APIs, like OpenAI's, offer features that force the model's output to conform to a provided JSON schema. This is not a suggestion; it is a hard constraint applied at every step of token generation.
OpenAI DevDay 2024 | Structured outputs for reliable applications
This talk from OpenAI's DevDay 2024 gives a fantastic overview of structured outputs, including a deep dive into the engineering that makes it possible.
First, watch the introductory segment from All of these products to understand the historical problems that motivated this feature. Next, see how the feature is used in the API. Watch from So, to solve these problems. Notice the strict=True parameter, which is the key to enabling guaranteed schema adherence. The ability to handle recursive schemas is vital for graph-like structures. Watch the UI generation demo from Now this is not a prebuilt UI. The speaker explains how defs and self-referencing in the JSON schema allow for nested and recursive components, a capability essential for defining complex graphs. Finally, for the first-principles understanding, watch the detailed technical explanation from we decided to take an approach. This section is dense but highly rewarding. It explains: Token Masking: How the model's vocabulary is filtered at each step to only allow tokens that are valid according to the schema. Context-Free Grammars (CFG): Why simple regular expressions are insufficient for enforcing nested and recursive structures (like our DAGs), and why a more powerful CFG-based approach is necessary. This directly addresses the complexity of our planning task.
Understanding constrained decoding should give you confidence that generating structured graphs is not a probabilistic trick but a reliable engineering primitive that you can build upon. It ensures that the output from your Planner agent will always be parsable by your execution engine.
Conclusion
In this lesson, you have moved from being the architect of your agent's workflow to being the teacher of an agent architect. You now have the knowledge to design a system where an LLM can autonomously create its own complex, multi-step execution plans.
Key Takeaways:
- A Planner Agent can be used to translate high-level user goals into a machine-readable Directed Acyclic Graph (DAG).
- The schema for this DAG is a critical design choice. A simple and effective schema uses a list of step objects, with dependencies indicated by list indices.
- An effective prompt for generating the DAG must include a clear role, context (available tools), an explanation of the schema, and, most importantly, few-shot examples of different dependency patterns.
- This entire process is made reliable by constrained decoding (or structured output) features in modern LLMs, which use techniques like token masking and Context-Free Grammars to guarantee schema-compliant JSON output.
Preview of the Next Lesson:
We have successfully designed the components needed to generate a structured graph as a JSON object. But how does that JSON become an active, running workflow? In our next lesson, "Implement a workflow constructor that parses a graph definition and prepares it for execution," we will build the bridge. You will write the code that consumes the planner's JSON output, validates it, and constructs an in-memory, executable representation of the workflow, ready for the orchestration engine.