🎯 By the end of this module, you will:
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.
# 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
"Like baking a cake: Measure Ingredients ➔ Mix Batter ➔ Bake in Oven."
Topology Traps
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.
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.
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.