Skip to Content

What is a LangGraph state machine and why prefer it over a linear chain?

Category: Backend & API

Answer

LangGraph is a graph framework where you declare typed state, named nodes (functions), and directed edges (conditional or fixed). The graph can cycle, branch, and pause for human input. Linear chains (LCEL pipe) can’t represent loops, persistent memory, or routing — those need a graph.

Concrete examples from the fca project context

Example 1

StateGraph(State) with messages: Annotated[list, operator.add] accumulates chat turns across nodes.

Example 2

add_conditional_edges(“model”, decide, {”loop”: “model”, END: END}) lets the LLM decide to keep going.

Example 3

compile(checkpointer=memory) persists state by thread_id so the run survives a crash.

Data flow / flow chart

START -> call_model -> should_continue? -> yes -> call_tool -> call_model
                                         -> no  -> END

Takeaway

If your task needs loops, branching, or pause/resume, use LangGraph. If it’s one straight shot, LCEL is enough.