Skip to content
~/series/agentic-workflows/workflow-graphs
EST: 34mSECTIONS: 11SKILLS: 4

Lesson 1 · Agentic workflows

34 min readAdvanced

Workflow graphs

State machines and graphs for deterministic-agentic orchestration. When to graph vs free-loop.

A refund graph that pauses for a human and resumes from a checkpoint

Step 1 of 5

Retrieve order

The first node loads the order into shared state. Each node is a function that takes state and returns updated state.

The edges, not the model, decide which step can run next, and state is saved at the pause.

Workflow graphs

A workflow graph is a state machine where nodes are steps (LLM calls, tools, conditions) and edges are transitions. Unlike a free-form agent loop, the graph defines which steps can follow which — giving you determinism where you need it and flexibility where you want it.

LangGraph is the canonical framework: you define nodes (functions), edges (transitions), and conditional edges (branching). State is passed through the graph and checkpointed for human-in-the-loop.

Why graphs beat free loops

Free agent loops are non-deterministic — same input, different paths. That's great for research, terrible for production workflows where you need: auditability (which steps ran), retryability (resume from a checkpoint), human-in-the-loop (pause before a step), and composability (reuse sub-graphs). Graphs give you all four.

Graph architecture

START → node → conditional_edge → node → ... → END. Each node is a function (state) → state. Conditional edges route based on state. Checkpoints serialise state for pause/resume. Sub-graphs compose into larger graphs. Compiled graph = runnable, observable, resumable.

A LangGraph workflow

workflow.py
# Checked against LangGraph's interrupts guide on 28 October 2026.
from typing import TypedDict

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END
from langgraph.types import Command, interrupt

# vector_store and call_llm are your retrieval and LLM helpers from earlier chapters.

class State(TypedDict, total=False):
    query: str
    retrieved: list[str]
    answer: str
    needs_human: bool

def retrieve(state: State) -> dict:
    docs = vector_store.search(state["query"], k=5)
    return {"retrieved": [d["text"] for d in docs]}

def generate(state: State) -> dict:
    context = "\n\n".join(state["retrieved"])
    answer = call_llm(f"Context: {context}\n\nQuestion: {state['query']}")
    needs_human = "I'm not sure" in answer or len(state["retrieved"]) == 0
    return {"answer": answer, "needs_human": needs_human}

def human_review(state: State) -> dict:
    # interrupt() pauses the graph here and saves its state with the
    # checkpointer. The value passed to Command(resume=...) becomes its return
    # value. On resume this node runs again from the top, so keep any side
    # effects after the interrupt() call (or make them idempotent).
    decision = interrupt({"question": state["query"], "draft_answer": state["answer"]})
    return {"answer": decision["answer"], "needs_human": False}

def route(state: State) -> str:
    return "human_review" if state["needs_human"] else END

workflow = StateGraph(State)
workflow.add_node("retrieve", retrieve)
workflow.add_node("generate", generate)
workflow.add_node("human_review", human_review)

workflow.add_edge(START, "retrieve")
workflow.add_edge("retrieve", "generate")
workflow.add_conditional_edges("generate", route, ["human_review", END])
workflow.add_edge("human_review", END)

# A checkpointer is required for interrupt(). InMemorySaver is for development;
# use a durable checkpointer (for example Postgres) in production.
app = workflow.compile(checkpointer=InMemorySaver())

# The thread_id identifies this run so it can be resumed later.
config = {"configurable": {"thread_id": "session-123"}}
result = app.invoke({"query": "What is our refund policy?"}, config=config)

if "__interrupt__" in result:
    pending = result["__interrupt__"][0].value  # the payload passed to interrupt()
    # ...show pending["draft_answer"] to a reviewer, collect their edit...
    reviewed = {"answer": pending["draft_answer"]}  # or the reviewer's edited text
    # Resume the same thread: interrupt() returns reviewed inside human_review.
    result = app.invoke(Command(resume=reviewed), config=config)

print(result["answer"])

Experiment: graph vs loop

Compare a graph-based workflow vs a free agent loop on a task that needs human approval.

Choose orchestration style. See how each handles human-in-the-loop.

What to observe

Workflow graphs win when you need pause/resume (human-in-the-loop), auditability, retryability, or composability. Free loops win for always-on agents where flexibility matters more than determinism. Match the orchestration to the requirement.

Production workflows

Production graphs: checkpointing for pause/resume, sub-graphs for composability, conditional edges for branching, state versioning for migrations, full trace of node executions, and timeout per node. Use graphs for any workflow with human-in-the-loop, audit, or retry requirements.

Challenge

Design a graph for a refund-approval workflow: retrieve order, classify (auto-approve, review, reject), if review → human approval → execute refund → notify customer. Where do you checkpoint?

Production checklist

Production checklist

0 of 8 checked

Knowledge check

When should you use a workflow graph instead of a free agent loop?

Complete

You can now build deterministic-agentic workflows with graphs. Next: multi-agent systems.

Mark this chapter as complete

Track your progress and unlock the next chapter.

Email updates

Get the next chapter and new guides by email.

One email per substantial release. Unsubscribe in one click.

We will email you a confirmation link first, then send an occasional email when a substantial guide is published. Unsubscribe in one click. Stored with our email provider (Resend), never sold or shared. See the privacy policy.

Continue learning