Mod 3.2Designing Custom Workflows with State Graphs
Level 3›Module 3.2
Level 3: Advanced Patterns & System DesignModule 3.2

Designing Custom Workflows with State Graphs

Custom Workflows

Level 3 • Advanced Patterns & System Design
Est. ~27 mins
5 Key Topics
🎁 Free Learner Perk

Unlock Verified Certificate & Daily Streak Tracker

Ready to master Designing Custom Workflows with State Graphs? Enable cloud sync to record your daily streak 🔥 and earn your Informational Completion Badge for your study milestones.

Day 1 Streak ActiveFree Completion BadgeSync Laptop & Phone

🎯 By the end of this module, you will:

Distinguish and implement the 3 master topologies: Linear, Cyclic, Branching
Build a cyclic reflection loop with a quality score ceiling to prevent infinite iteration
Wire N-way triage branching to route intents to specialist agents
Combine topologies into a hybrid pattern for real enterprise agent workflows
Workflow Design • Graph Topologies

Designing Custom Workflows with State Graphs

Real enterprise agents are rarely single functions. They are composed into State Graphs matching one of three fundamental patterns — or a hybrid of them.

1. Linear Pipeline: Assembly Line
# LINEAR PIPELINE: Sequential, deterministic, no branching
from langgraph.graph import StateGraph, START, END

builder = StateGraph(PipelineState)

# Every node added in order — simple add_edge() chains
builder.add_node("load_pdf", load_and_parse_pdf)
builder.add_node("extract_data", extract_structured_data)
builder.add_node("format_json", format_as_json_schema)
builder.add_node("store_db", write_to_database)

# Fixed sequential edges — no conditions, always runs in order
builder.add_edge(START, "load_pdf")
builder.add_edge("load_pdf", "extract_data")
builder.add_edge("extract_data", "format_json")
builder.add_edge("format_json", "store_db")
builder.add_edge("store_db", END)

pipeline = builder.compile()

# Execution: START → load_pdf → extract_data → format_json → store_db → END
# Every run follows the same path — completely deterministic

🔬 Topology Builder

The 3 Canonical Workflow Topologies

Compare Linear, Cyclic Reflection, and Branching state graph architectures

1. Linear Pipeline (Assembly Line)Deterministic tasks with fixed steps (e.g., Ingest PDF ➔ Summarize ➔ Format JSON).

"Like baking a cake: Measure Ingredients ➔ Mix Batter ➔ Bake in Oven."

START ➔ parse_document ➔ extract_entities ➔ format_output ➔ END
Stage 1parse_document
Stage 2extract_entities
Stage 3format_output
✍️ Instructor Note: "Before writing ANY code, sketch your graph on paper. Answer 3 questions: 1) What fields live in State? 2) Which node writes each field? 3) What is the exit condition? State schema first = zero architectural refactors later."
📌 Production Rule: Every cyclic graph MUST have 2 exit conditions: a SUCCESS exit (quality threshold met) and a SAFETY exit (loop_count exceeded). Never ship a cyclic graph with only a success exit — one repeated tool error creates an infinite loop.

Topology Traps

TRAP #1: State Schema Mismatch

Node A writes state['result'] as a string. Node B reads state['result'] as a list. The graph compiles without error and fails silently at runtime. Always define a single TypedDict for state and share it across ALL nodes in the graph.

TRAP #2: Missing Loop Counter

A cyclic graph where the quality score never improves (bad prompt, wrong rubric, or broken tool) will loop at full LLM cost until the API rate limiter or your credit balance stops it. Add loop_count to state and increment it in every revision node.

Key Takeaways

  • 1.Choose Topology Based on Uncertainty: Linear = zero uncertainty (ETL). Cyclic = uncertain quality (drafting, coding). Branching = uncertain intent (customer support). Most real systems combine all three.
  • 2.State Schema is the Foundation: Define your TypedDict state before nodes. Every node reads from and writes to this schema. State defines the contract between nodes — break it and the graph silently corrupts data.
  • 3.Hybrid Patterns are the Reality: Branching without refinement produces low-quality answers. Reflection loops without triage are expensive. The best production agents combine a fast triage front-end with per-specialist quality loops at the back.
Up Next • Module 3.3

Debugging Agents by Analyzing State Transitions

Use stream_mode="updates" to replay state changes frame-by-frame and pinpoint exactly which node dropped or corrupted data.

Continue to Module 3.3