Skip to content

< all problems29 · Level 05, Agents

Build a LangGraph With a Conditional Edge

medium · implement · Agents

Build build_graph() returning a compiled LangGraph that:

  1. Starts at a node work, which appends its name to log and increments count.
  2. Routes conditionally: back to work while count < 3, otherwise to a node finish that appends "finish" to log.
  3. Ends after finish.

What you need from LangGraph:

  • State is the TypedDict every node receives. A node returns only the keys it changed.
  • Reducers decide how an update merges. By default a returned key replaces the old value. In the State defined above, log is Annotated[list, operator.add], so a returned list appends; count has no reducer, so it replaces. Mixing those up is the most common LangGraph bug.
  • Nodes are functions state -> partial update.
  • Edges connect nodes. add_edge is unconditional; add_conditional_edges calls a function that returns the name of the next node. That is how you branch and how you loop.

The loop must terminate. A cycle with no exit condition runs until LangGraph's recursion limit throws.