liveBook · Manning

Conditional edge

As explained in AI Agents and Applications

A conditional edge is a workflow transition whose destination is selected at runtime according to state or other conditions.

A research assistant should not write its report just because a search has finished. If the results are weak, it may need another search. A conditional edge makes that choice at runtime: instead of always following a fixed edge, the workflow uses current state to select its next node. section 5.5.3 section 5.4.3

In LangGraph, nodes are functions that process state and return updates. Edges connect those nodes. A conditional edge adds a routing function that reads the state and chooses a destination. The decision belongs at the point where the required information is available—for example, after searches have produced results. section 5.4 section 5.4.3

Search again or summarize?

A small routing function can check the number of results: `should_refine_queries(state)` returns `"refine_queries"` when `len(state["search_results"]) < 2`; otherwise it returns `"summarize_results"`. Attach it after the search node with `graph.add_conditional_edge("perform_searches", should_refine_queries)`. The search still runs as a node; the conditional edge decides what happens next. section 5.4.3

For a fuller research workflow, a relevance evaluator can trigger another pass through query generation when fewer than half the summarized results are relevant. The route moves on to report writing when results are sufficient or three iterations have been reached. The iteration limit matters: without an end condition, a refinement loop has no stated stopping point. section 5.5.3

Conditional edges also control tool-using agents. After an LLM node responds, LangGraph’s `tools_condition` checks its latest message. Tool calls send execution to the tools node; no tool calls send it to `END`. When debugging, inspect the message and chosen route together: a request for a tool should lead to tool execution, while a completed answer should end the graph. section 11.4 section 11.6.1

Figure 11.2 Conditional graph logic routes each user query through the LLM node and then dynamically directs flow to the tools node or ends the process, depending on whether tool calls are required.
figure

The diagram shows the two destinations of the agent’s conditional edge: tool execution or the end of the graph. section 11.4

How AI Agents and Applications covers it

How deep it goes
Defines state-based routing, gives a routing-function example, and follows a tool-call decision through debugging.
Examples
Uses a search-result count, a relevance-gated research report, and an agent deciding between tools and END.
What it assumes
Introduces nodes, edges, and state before routing; uses Python functions to express the decisions.
What you can do after
Build it from scratch with a state-reading routing function, then debug its destination using intermediate messages.
How it connects
Connects nodes, typed state, relevance evaluation, refinement loops, tool calling, and graph end conditions.

Also explained in

Compare how 2 books explain conditional edge →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.