New Tool Radar

AI Agents & Automation

How to Route a LangGraph Graph With a Conditional Edge After a Tool Result

Add a conditional edge to a LangGraph graph so the tool result decides which node runs next.

Schematic diagram with three steps: Add the tool node, Write the routing function, then Test both routes.

A fixed edge always goes to the same node, but a tool result should change the route: a found record goes one way, a missing record goes another. A conditional edge reads the state after the tool node and picks the next node from the result.

Before you start

You need a working LangGraph StateGraph with a tool node. Decide the two outcomes first and name them plainly, for example found and not-found, before you write the routing function.

Write a routing function that reads the tool result

Write a function that takes the state and returns the name of the next node: the success node when the tool result has the record, the fallback node when it does not. Add it as a conditional edge from the tool node, with one branch per outcome.

Test both routes with one found and one missing record

Run the graph with a record that exists and check it takes the success branch, then run it with a record that does not exist and check it takes the fallback branch. If both runs take the same branch, the routing function is reading the wrong state key.

Worked example: Two routes for a fictional orders agent

Northwind Orders is a fictional orders desk. Its graph looks up an order and routes on the result. The table shows both routes.

Order Tool result Route taken
Order 1042 Found, out for delivery Success node: reply with the status
Order 9999 Not found Fallback node: ask for the order number again

Expected result (UNTESTED): order 1042 gets a status reply and order 9999 gets a request to check the number. We checked the LangGraph documentation summary, which describes conditional edges as the routing mechanism, but we did not run the graph on this machine.

Limits

A routing function that reads the wrong key routes every run the same way, which looks like the tool failing. Keep the routing logic to one check on one key, and log which branch each run takes so a wrong route is visible.

Sources

See all