Module 1 · Thinking in graphs

Limits of linear chains and implicit loops

7 min read

The first LLM applications were chains: a prompt, a model call, some processing of the answer, then perhaps a second call. That linear model carried the first wave of applications — summaries, extraction, simple chatbots. It hits its limits as soon as the application has to go back, choose between several paths, wait for a human or resume after a failure. LangGraph was born for those cases.

The linear chain and its blind spots

A chain is a pipeline: A, then B, then C. It is described very well as a Python function calling others.

async def answer_ticket(ticket: str) -> str:
    category = await classify(ticket)
    context = await search_docs(ticket, category)
    return await write_answer(ticket, context)

That is clear and sufficient as long as the flow is fixed in advance. But look at what real customer support requires:

Real needWhat the orchestration needs
The model checks a tool, reads the result, checks another…A loop whose number of turns isn't known in advance
A refund request doesn't follow the same path as a questionBranches decided at runtime
A 3-item order is assessed item by itemParallelism whose degree depends on the data
An €800 refund must be approved by a supervisorA pause of several hours, then a resume at the same spot
The server restarts in the middle of processingA state persisted at every step
The customer comes back the next dayA memory attached to the conversation and to the customer

None of these needs is exotic. Together, they describe almost every production agentic system.

Implicit loops: the while True trap

Faced with these needs, the reflex is to write the loop yourself:

while True:
    response = await model.ainvoke(messages)
    messages.append(response)
    if not response.tool_calls:
        break
    for call in response.tool_calls:
        messages.append(await run_tool(call))

This loop works — it is in fact the heart of every agent. But everything around it must be written by hand, and that is where the problems begin:

  • The state is implicit. It lives in local variables. If the process stops, everything is lost, and nothing tells you where the agent was.
  • The flow is buried in the code. To understand the possible paths, you must read every if and every break. Impossible to draw, visualize or test a step in isolation.
  • Pausing is nearly impossible. Waiting three hours for a human's approval with a while loop means keeping the process alive… or reinventing a save-and-resume system.
  • Parallelism is ad hoc. Every asyncio.gather is a special case, with its own error handling.

After a few months, this code becomes a homemade orchestration engine, undocumented and fragile.

LangGraph's idea: make the flow explicit

LangGraph proposes to describe the application as a state graph:

  • a typed, shared state that flows from node to node;
  • nodes, functions that read the state and return an update;
  • edges, fixed or conditional, that decide the next node.

The engine runs the graph step by step (in super-steps, inspired by Google's Pregel model) and saves the state after each step. Every missing capability follows from that simple choice:

CapabilityHow the graph makes it possible
LoopsAn edge can point to an already visited node
BranchesA conditional edge calls a routing function
ParallelismSeveral nodes active in the same step, or Send for a dynamic number of tasks
PersistenceA checkpointer saves the state at every step
Pause and resumeinterrupt() stops execution; the saved state allows resuming later, elsewhere
DebuggingCheckpoint history lets you replay or "fork" an execution

What LangGraph is not

It helps to clear up three frequent misunderstandings.

It is not LangChain. LangGraph is published by the same company and integrates well with LangChain models and tools, but it doesn't depend on them: a node is an ordinary Python function that can call any SDK. In this course we use the LangChain model integrations (they bring token-by-token streaming and the standard message format), but nothing forces you to.

It is not a "magic" agent framework. LangGraph decides nothing for you. It provides a low-level state execution engine; you write the nodes, the prompts and the routing logic. Higher-level abstractions (ready-made agents) are built on top of it.

It is not only for agents. A workflow with no LLM at all — document validation, a data pipeline with an approval step — benefits just as much from persistence and resumption.

Workflow or agent: LangGraph covers the whole spectrum

The distinction between a workflow (the code decides the path) and an agent (the model decides) is not binary. Real systems are hybrid, and that is where LangGraph shines:

  • the skeleton is a deterministic workflow, readable and testable (triage, refund policy, approvals);
  • the autonomy zones are bounded agentic loops (answering a question with tools).

That is exactly the architecture of Relay, this course's running project: a customer support agent for an online shop. Triage, refund rules and human approvals are code; only the conversation with tools is left to the model, in a loop with a limited number of turns.

START ─► load_profile ─► triage ─┬─► agent ⇄ tools ─────────┐
                                 ├─► refund (subgraph) ──────┼─► finish ─► END
                                 └─► handoff ────────────────┘

We will build every brick of this graph across the modules: state and agent loop (module 1), routing, parallelism and subgraphs (module 2), persistence and memory (module 3), human approvals (module 4), then the production server (module 5).

Setting up

pip install -U langgraph langchain-anthropic

The companion code installs and tests without an API key, thanks to a deterministic fake model:

cd relay-support
pip install -e ".[dev,api]"
RELAY_FAKE_LLM=1 pytest          # on Windows: $env:RELAY_FAKE_LLM="1"; pytest

The course is based on LangGraph 1.2. The APIs presented (StateGraph, Send, Command, interrupt, checkpointers, store) have been stable since version 1.0.

Key takeaways

  • A linear chain is enough as long as the flow is known in advance; loops, branches, pauses and resumes require an orchestration engine.
  • A hand-written while loop hides state and flow, and makes pausing and resuming very hard.
  • LangGraph describes the application as state + nodes + edges, and saves the state after every step.
  • Good systems are hybrid: a deterministic skeleton, with bounded zones of autonomy.
  • LangGraph is a low-level engine: it runs, persists and resumes, but decides nothing for you.

Practice

  1. Take an agent script you wrote (or the loop above) and list what would be lost if the process stopped halfway.
  2. For a use case in your company, draw on paper the steps, branches, loops and the points where a human must step in.
  3. Install the companion code and run the offline test suite: 34 tests should pass.

Resources