A linear chain of LLM calls (step 1 feeds step 2 feeds step 3) is easy to reason about and build. It can’t express one of the most common patterns in agent design: “if step 3’s output fails a check, go back to step 2 and try again.” Once a step needs to route backward conditionally, you need something that can represent a loop: a state graph with back-edges.
What a back-edge looks like as code
In a research-agent project built on LangGraph, the critic step sits inside a cycle. After the critic scores a draft, the routing function decides whether the graph is done or needs to go back to an earlier node:
def route_after_critic(state: AgentState) -> str:
"""Decides if we are done or need to retry, and routes through memory writer."""
feedback = state.get("critic_feedback", "")
revisions = state.get("revision_count", 0)
if feedback == "APPROVED":
return "memory_writer"
elif revisions >= 3:
console.print(
"[bold red]Max revisions reached. Ending to save costs.[/bold red]"
)
return END
else:
return "synthesizer"
That last branch is the back-edge: route_after_critic can send control flow to synthesizer, the same node that produced the draft the critic just rejected. In a pipeline, “synthesizer” would appear once, upstream of “critic”. Here the graph can return to it, conditionally, based on state computed downstream. A chain of function calls can’t express that; it takes named nodes and edges that can point anywhere, including backward, which is what build_graph() sets up:
builder.add_conditional_edges("critic", route_after_critic)
The same file has a second conditional edge earlier in the graph, route_after_memory_retriever, which is a fork instead of a loop but has the same need: the next step depends on the state just computed.
def route_after_memory_retriever(state: AgentState) -> str:
"""If a cached report was found, short-circuit the pipeline."""
if state.get("draft_report"):
return "critic"
return "researcher"
If a cached report already exists for the topic, the graph skips straight to the critic and never runs the researcher at all. A fixed pipeline can’t skip a step conditionally any more than it can repeat one; both require a node that gets to choose its own successor.
Ending the loop
A back-edge on its own can loop forever. route_after_critic guards against that with a counter checked before the loop path is taken:
revisions = state.get("revision_count", 0)
...
elif revisions >= 3:
return END
revision_count is incremented inside the critic node itself, every time a draft is rejected, and the cap of three turns “loop until the critic is satisfied” into “loop until the critic is satisfied or three revisions are spent”. Without it, a critic that’s slightly too strict, or a synthesizer that keeps making the same mistake, would retry forever. The cap only guarantees termination, not correctness, but without a bound on retries you can’t put a cost or latency budget on the system.
When you need a graph
LangGraph and other graph-based orchestration are often justified by general flexibility. The more useful check for your own design is whether any step’s output decides that an earlier step must run again. If so, you have a cycle, and it needs a routing function that inspects state, a named back-edge to the node that re-runs, and a counter with a cap so the cycle ends. A prompt-chaining library that only supports “call this, then call that” can approximate a fixed number of retries by unrolling them into repeated steps, but it can’t express “however many retries the critic actually needs, up to a cap” as a single reusable path, because it has no concept of returning to a node it already visited.