Nathaniel
Back to posts

Why we built a stateful workflow graph instead of a single agent loop

Nathaniel LinAugust 14, 20263 min read130 views
Why we built a stateful workflow graph instead of a single agent loop

A pure ReAct loop (while (!done) { callLLM(); runTool(); }) is awesome for open-ended coding or quick research prototypes.

When you ship a Digital Employee platform to enterprise teams who want repeatable multi-step business workflows (ingest a ticket, check PII rules, query internal knowledge bases, branch by department, spawn parallel sub-tasks, pause for a manager's approval before mutating an external system), an unconstrained while loop becomes scary very quickly.

Wait until a 12-step enterprise run fails on step 10 because an upstream API returned a 503 - and your single loop has to either restart from scratch (re-running side effects!) or lose the entire state when a container restarts.

Modeling execution as a resumable workflow graph

We built a stateful workflow-graph orchestration engine where each run is an explicit DAG / state graph with typed node transitions, branching conditions, and dynamic sub-task scheduling (fan-out over a variable list of items, then join once sub-tasks finish).

The core design choice that paid off most was node snapshot persistence. After every node finishes executing, we persist an immutable checkpoint of the graph state, node outputs, and execution cursor to PostgreSQL before scheduling the next node:

interface NodeCheckpoint {
  runId: string;
  stepIndex: number;
  nodeId: string;
  status: "completed" | "waiting_approval" | "failed";
  stateSnapshot: Record<string, unknown>;
  output: unknown;
  createdAt: string;
}

async function resumeRunFromCheckpoint(runId: string): Promise<void> {
  const latest = await checkpointStore.getLatest(runId);
  if (!latest) throw new Error(`No checkpoint found for run ${runId}`);

  const graph = await workflowStore.getVersion(latest.workflowVersionId);
  const nextNodes = resolveOutgoingEdges(graph, latest.nodeId, latest.stateSnapshot);

  for (const next of nextNodes) {
    await scheduler.enqueueNode(runId, next.id, latest.stateSnapshot, latest.stepIndex + 1);
  }
}

Because every node boundary is persisted, two hard enterprise requirements basically fell out for free:

  1. Lossless crash recovery and retry: if a worker restarts or an upstream LLM times out on step 9, you resume right at step 9 using step 8's exact snapshot instead of burning tokens re-running steps 1 through 8.

  2. Human-in-the-loop approval gates: a sensitive action node (like issuing a refund or sending an external email) transitions the run into waiting_approval, releases the worker completely (zero held RAM or open sockets), and can sit paused for 10 minutes or 3 days until an authorized reviewer clicks Approve or Reject in the UI. Once approved, the engine loads the snapshot and picks right up.

Constraining LLM outputs at graph edges

When a graph branches based on an LLM node's decision (e.g., routing a customer request into billing, technical_support, or escalate), free-form text parsing is brittle. Even if the model follows instructions 99% of the time, the 1% where it replies "I think this belongs in billing!" instead of "billing" breaks your edge condition.

Combining schema-constrained decoding (forcing the model's output tokens to strictly conform to a JSON Schema / enum grammar) with fine-grained RBAC on which tools and knowledge bases each node is permitted to access made graph transitions deterministic. The LLM still does the fuzzy reasoning inside a node, while the engine enforces strict types, permissions, and audit trails between nodes.

Share this article

Reactions

Comments (0)

Why we built a stateful workflow graph instead of a single agent loop | Nathaniel's blog