LangChain: Building LLM Applications with Python

Lesson 5 of 6

LangGraph: Stateful, Multi-Step Agent Workflows

AgentExecutor works well for a simple "call tools until done" loop, but real workflows often need more control: conditional branches, loops that revisit earlier steps, or multiple cooperating steps that each need to see and update a shared state. LangGraph (from the LangChain team) models an application as an explicit graph of steps (nodes) and transitions (edges) over a shared state object, instead of an implicit loop.

State: the shared object every node reads and updates

Pythonshares state with other Python blocks

Every node in a LangGraph graph receives the current state and returns a dictionary of updates to merge into it, this is what makes multi-step workflows explicit and inspectable: at any point, state is a plain, printable object describing exactly where the workflow is.

Defining nodes

Pythonshares state with other Python blocks

Each node is a plain Python function, search_node would call a real search tool and a model call would live in answer_node, kept as placeholders here since both need real API access, the graph structure around them is what this lesson is actually teaching.

Wiring the graph

Pythonshares state with other Python blocks

add_edge('search', 'answer') is a fixed transition, always go from search to answer. add_edge('answer', END) marks answer as a terminal step, END is a special LangGraph constant meaning "the graph is finished."

Conditional edges: branching based on state

Pythonshares state with other Python blocks

add_conditional_edges routes to a different next node depending on a function of the current state, here, looping back to search again if the results look too thin, this is the core capability AgentExecutor's simple loop doesn't give you directly: an explicit, inspectable decision about what happens next, rather than an implicit "keep calling tools until the model stops."

TIP

Think of AgentExecutor as one specific, common pattern (loop until no more tool calls), and LangGraph as the general-purpose tool for building that pattern, and any other control flow (branches, loops, multiple cooperating agents) your workflow actually needs.

📝 LangGraph Quiz

Passing score: 70%
  1. 1.What does every node in a LangGraph graph receive and return?

  2. 2.add_conditional_edges lets a graph route to different next nodes based on the current state, unlike a fixed add_edge transition.

  3. 3.In LangGraph, ____ is the special constant used to mark a node as the terminal step of the graph.