All articles
12 min read
#AI workflows#Durable State#Retries#Audit Trails#Evals#Orchestration

AI Workflow Engines for Durable, Auditable Systems

I use an illustrative LLM workflow to examine durable state, recovery, audit records, and cost controls.

On this page

Why this matters

I use an illustrative workflow here: receive a request, call a model, call a tool, summarize the result, and return JSON. This is a design walkthrough, not a production incident report or a measured project. I ask what happens if a tool changes external state but the worker crashes before recording success, a model response is lost, or a reviewer approves a draft that later changes.

I treat the engine as part of the reliability boundary. I want durable records that let me inspect transitions, recover from stored state, reproduce evaluations, and account for cost. The JSON and Python below are illustrative contracts; they do not demonstrate an implemented engine.

In this article, I use “workflow engine” in a narrow sense: a component that coordinates steps, branches, retries, state transitions, tool calls, model calls, and human decisions over time. I consider graph-based orchestration when it fits the workflow. I keep a separate note on LangGraph workflow orchestration because graph execution maps well to branching AI applications. Still, the durable design matters more than the specific framework.

Two external references shape how I think about this. Temporal describes durable execution as preserving execution state so applications can recover from failures without losing progress. That model is close to what I want when a long-running AI process crosses process or machine boundaries: Temporal durable execution. LangGraph documents persistence through checkpointers that save graph state at execution steps, which is a useful primitive for conversational and branching agent state. I would use a persistent backend for restart recovery; an in-memory checkpointer loses its state when the process stops: LangGraph persistence.

Failure cases I design for

The first failure case I consider is partial progress without a trustworthy record. A tool call succeeds, but the process crashes before the orchestration layer records the next state. A retry starts from an earlier step and calls the same external system again. The final answer may look valid, but the state machine has diverged from reality.

The second failure mode is nondeterministic recovery. AI systems often include stochastic model outputs, time-sensitive retrieval, mutable tools, and changing prompts. If I retry by “just running the function again,” I may not be recovering. I may be creating a new execution that only resembles the original. That distinction matters when a workflow approves a refund, updates a ticket, drafts regulated content, or routes a customer issue.

The third failure mode is invisible branching. A workflow that asks a model to choose between “retrieve more context,” “call tool,” and “answer now” is already a branching workflow. If that branch choice exists only in logs, audit quality is poor. I want the branch decision recorded as data: input state, decision reason, selected edge, model configuration, prompt version, and next state.

The fourth failure mode is retry amplification. A timeout near the end of a step can cause the system to repeat expensive model calls or duplicate tool calls. Without idempotency keys, step-level checkpoints, and cost accounting, a retry policy can become a cost multiplier. I do not need an exotic incident to justify this design. A single slow dependency can be enough.

The fifth failure mode is evaluation drift. If the workflow state does not include the prompt version, retrieval snapshot identifier, model identifier, tool version, and branch path, then a later evaluation result is hard to compare. Reproducible evals need stable inputs, or at least a precise record of the unstable parts.

The design I would use

For this example, I would use a durable state machine with explicit step records. I give each transition a name, an input envelope, an output envelope, an error envelope, and a durable checkpoint. I represent every branch as data so I can inspect the path taken.

I would record a step like this:

{
  "workflow_id": "wf_2026_001",
  "run_id": "run_01",
  "step_id": "retrieve_context_02",
  "attempt": 1,
  "status": "succeeded",
  "started_at": "2026-08-17T10:15:30Z",
  "finished_at": "2026-08-17T10:15:34Z",
  "input_ref": "blob://inputs/retrieve_context_02.json",
  "output_ref": "blob://outputs/retrieve_context_02.json",
  "error_ref": null,
  "prompt_version": null,
  "model": null,
  "tool_name": "knowledge_search",
  "tool_version": "2026-08-01",
  "idempotency_key": "wf_2026_001:run_01:retrieve_context_02"
}

I keep the idempotency key stable across retries of the same logical operation; the attempt counter belongs in the audit record. Changing the key on every attempt would prevent the external service from recognizing a duplicate. A deliberately new operation gets a new key. This requires the external service to honor that key within its documented retention window; otherwise I need reconciliation.

I prefer references for large payloads and hashes for integrity. The database row remains readable, while the full prompt, retrieved passages, model response, and tool output remain available. For sensitive systems, I also separate audit metadata from content that may require stricter retention or access control.

I would keep the workflow state in a compact document:

{
  "workflow_id": "wf_2026_001",
  "status": "waiting_for_human_review",
  "current_node": "human_review",
  "branch_path": [
    "classify_intent",
    "retrieve_context",
    "draft_answer",
    "policy_check",
    "human_review"
  ],
  "facts": {
    "intent": "billing_question",
    "risk_level": "medium",
    "requires_human": true
  },
  "cost": {
    "model_input_tokens": 4280,
    "model_output_tokens": 812,
    "tool_calls": 3
  }
}

I would verify three properties of this split.

First, I require explicit durable state. After stopping a worker between steps, I expect the next worker to resume from the last committed transition. If the process dies during a step, the next worker must inspect the last record and determine whether retry or reconciliation is appropriate.

Second, I distinguish replay from a new attempt. I would persist completed model responses and reuse them during recovery. If no response was recorded, a new attempt would follow an explicit retry policy; it may incur another charge even if the first response was lost. For external side effects, I require an idempotency key or a reconciliation path.

Third, I would record token counts and tool calls per step and check budgets before expensive branches. The next transition could select a cheaper path, request review, or stop with a partial answer. I would test reservation and reconciliation of uncertain usage before claiming that a hard spending limit is enforced.

I would keep queues, storage, tracing, and application code alongside the engine. OpenTelemetry defines a span as a unit of work or operation, and I find that span model useful for observing workflow steps without making tracing the source of truth: OpenTelemetry traces. I would keep the durable workflow store authoritative because traces can be sampled, dropped, or retained under a different policy than business state.

Branches, retries, and human pauses

A branching AI workflow needs a stricter contract than a plain function call. Each node must declare whether it is pure, retryable, idempotent, or requires reconciliation. For this example, I use a small classification:

pure: no external side effect; safe to recompute if inputs are stable
recorded: nondeterministic output stored after success; replay uses stored output
idempotent_side_effect: external call protected by an idempotency key
non_idempotent_side_effect: retry disabled; manual reconciliation required
human_gate: pauses until an authenticated decision is recorded

I classify retrieval and model calls as “recorded” when I need to preserve their outputs. For a payment, ticket update, email send, or permission change, I require either an idempotency contract or a branch that stops for reconciliation.

For human review, I would bind the decision to an immutable draft output, its prompt version, the policy check result, and the visible context. A changed draft would require a new approval. I would record the authenticated decision as a workflow transition.

I would retry the failed node according to its classification. A completed classification could be reused after a retrieval failure if its inputs remained valid. A formatting failure would require a new policy check if the retry changed policy-relevant content. I would test those dependencies explicitly.

How I verify it

I verify an AI workflow engine at three levels: transition tests, recovery tests, and evaluation runs.

Transition tests check that each node reads a known state and writes an expected next state. These tests are not model-quality tests. They are state-machine tests. I stub model and tool outputs, then assert branch selection, status transitions, cost updates, and audit records.

A minimal Python-style test looks like this:

def test_policy_failure_routes_to_human_review():
    state = {
        "workflow_id": "wf_test",
        "current_node": "policy_check",
        "facts": {"risk_level": "high"},
        "cost": {"model_input_tokens": 1000, "model_output_tokens": 200}
    }
    output = {
        "allowed": False,
        "reason": "requires specialist approval"
    }

    next_state = apply_policy_check(state, output)

    assert next_state["current_node"] == "human_review"
    assert next_state["status"] == "waiting_for_human_review"
    assert next_state["branch_path"][-1] == "human_review"

Recovery tests are more important. I inject crashes after durable writes, before durable writes, and after external calls. For each injected failure, I expect one of three outcomes: resume from the last committed state, retry the current step safely, or stop for reconciliation. If a test cannot explain which of those outcomes should occur, the workflow is underspecified.

Evaluation runs use recorded workflow inputs and recorded branch paths. I keep the eval harness separate from production execution, but I feed it production-shaped records. That gives me reproducible evals without pretending that every upstream system is immutable. The eval record includes the prompt version, model identifier, tool outputs or retrieval references, policy flags, expected outcome, observed outcome, and cost envelope.

The most useful eval failures are not limited to wrong answers. I also look for wrong branches, excessive tool use, missing citations, policy bypasses, and budget violations. This is where workflow design and model evaluation meet. If an answer is acceptable but the path required unnecessary expensive calls, the workflow still needs work.

Choices I would avoid

I would not hide orchestration inside nested application functions. That pattern feels fast at the start and becomes hard to recover later. If a workflow has branches, retries, and external side effects, I want the state machine to be visible in data.

I would not use logs as the audit trail. Logs are useful for diagnosis, but they are not a stable workflow ledger. The audit trail needs durable identifiers, explicit transitions, payload references, actor identity for human decisions, and a retention policy that matches the application risk.

I would not retry nondeterministic steps casually. A second model call is not the same event as the first model call. It may be acceptable, but it should be recorded as a second attempt with its own output, cost, and reason.

I would not leave cost outside the workflow. Cost control is not only billing analysis. It affects branch choice, retry policy, and stop conditions. A workflow that can spend without recording the reason is incomplete.

I would not start with a maximal framework decision. I prefer to start with the invariants: durable state, deterministic recovery, reproducible evals, and cost under control. After that, I choose the engine or library that makes those invariants easier to maintain. Sometimes that is a graph framework. Sometimes it is a durable execution platform. Sometimes it is a small state machine backed by a database and a queue.

The durable design is the part I expect to keep. Model providers, prompt formats, and orchestration libraries change. I use the same acceptance questions across those choices: can I explain what happened, resume safely, repeat an evaluation, and account for cost?

FAQ

What should an AI workflow engine persist for reliable recovery?

I persist explicit step records with a transition name, input envelope, output envelope, error envelope, and durable checkpoint. For larger payloads I prefer references and hashes, so prompts, retrieved passages, model responses, and tool outputs remain available without making the main database row unreadable.

How do I handle nondeterministic model calls during recovery?

I do not try to make every model output deterministic. For completed model calls, I store the response and reuse it during recovery. For failed calls before any response arrived, I allow a new attempt under a recorded retry policy, with its own output, cost, and reason.

Why should branch decisions be stored as workflow data?

A model choice between retrieving more context, calling a tool, or answering is already a branch. I want that decision recorded as data: input state, decision reason, selected edge, model configuration, prompt version, and next state. If the branch exists only in logs, audit quality is poor.

How should retries be scoped in a durable AI workflow?

I retry the failed node according to its classification instead of retrying the whole workflow. A node can be pure, recorded, an idempotent side effect, a non-idempotent side effect, or a human gate. This reduces duplicate work and makes failures easier to inspect.

How do I make workflow evaluations reproducible?

I use recorded workflow inputs and recorded branch paths, with an eval record that includes prompt version, model identifier, tool outputs or retrieval references, policy flags, expected outcome, observed outcome, and cost envelope. This gives me reproducible evals without pretending every upstream system is immutable.

Share this article

Related articles

  • Multi-Agent Systems: Durable Coordination Patterns

    A stateful architecture for coordinating AI agents with explicit roles, durable handoffs, tool boundaries, and deterministic recovery.

    Nov 14, 202611 min read
    #Multi-Agent Systems#Agent Orchestration#Durable State#Failure Recovery#LLM Evals
  • State as the API: Three LangGraph Designs

    I compare three LangGraph state designs: flat fields, nested models, and explicit reducers, with routing and replay constraints.

    Jan 8, 202511 min read
    #LangGraph#LLM#Multi-Agent#Orchestration
  • Memory Architectures

    I separate source events from derived memory indexes, with explicit provenance, retention rules, and a recovery test plan.

    Sep 10, 202611 min read
    #AI Memory#Retrieval#Durable State#Evaluation#RAG Systems