Skip to main content

Graph Workflows

SynapseKit's StateGraph lets you model any multi-step LLM workflow as a directed graph. Nodes hold your logic; edges define execution order; a checkpointer saves state after every node so workflows can pause, resume, and survive process restarts.

Guides in this section

GuideWhat you'll buildDifficulty
Linear WorkflowThe simplest possible StateGraph — three nodes connected in sequenceBeginner
Conditional RoutingA graph that inspects state and chooses which node to visit nextBeginner
Fan-Out / Fan-InParallel nodes that run simultaneously and merge resultsIntermediate
Subgraph CompositionReusable compiled subgraphs embedded as nodes in a parent graphIntermediate
Checkpointing and Resumable WorkflowsSQLiteCheckpointer for crash recovery and mid-run state inspectionIntermediate
Human-in-the-LoopGraphInterrupt pauses execution; resume() continues with human inputIntermediate
Graph Error Recoverytry/except in nodes, retry edges, fallback nodes, error state trackingIntermediate
Visualizing Graphs with Mermaidget_mermaid(), GraphVisualizer, and PNG exportBeginner

The graph execution model

Every StateGraph is parameterized on a state type — a plain dataclass whose fields accumulate results as execution moves through nodes.

initial_state


[node A] ──── edge ────▶ [node B] ──── edge ────▶ [node C]

END

Each node is a sync or async callable with the signature (state: S) -> S. Nodes receive the current state, do their work, mutate it, and return it. The graph calls nodes in the order determined by edges and routing functions.

Key primitives

APIPurpose
StateGraph(StateType)Create a new graph
graph.add_node(name, fn)Register a node
graph.add_edge(a, b)Unconditional edge from node a to node b
graph.add_conditional_edges(a, router, map)Edge that calls router(state) to pick the next node
graph.add_parallel_edges(a, [b, c])Fan-out: run b and c concurrently after a
graph.add_join_edge([b, c], d)Fan-in: continue to d after both b and c finish
graph.set_entry_point(name)First node to execute
graph.compile()Validate the graph and return a CompiledGraph
compiled.arun(state)Execute asynchronously
compiled.resume(run_id, updates)Resume a paused graph
compiled.get_mermaid()Return a Mermaid diagram string

Installation

pip install synapsekit[openai,graph]

The graph extra adds the graph engine and optional SQLite checkpointing support.