tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > LangGraph > LangGraph Conditional Edges

LangGraph Conditional Edges

Author: Venkata Sudhakar

A conditional edge in LangGraph decides which node to visit next based on the current state. This is how you give an agent the ability to branch - for example, routing to a tool executor when the LLM wants to call a tool, or routing to END when the LLM has a final answer. Without conditional edges, your graph can only go in one fixed direction. With them, the graph adapts dynamically at runtime.

A conditional edge is added with graph.add_conditional_edges(from_node, routing_function). The routing function receives the current state and returns a string - the name of the next node to go to, or END. Typically you inspect the last message in the state: if it has tool_calls, route to the tools node; otherwise route to END.

The below example shows the exact routing function pattern used in a ReAct agent loop. The LLM node runs, then the conditional edge checks whether the LLM wants to call a tool or is done.


It gives the following output,

from langchain_core.messages import HumanMessage

result = app.invoke({"messages": [HumanMessage(content="What is 6 multiplied by 7?")]})
print(result["messages"][-1].content)

# Execution trace:
# llm_node  -> tool_calls: [multiply(a=6, b=7)]  -> route returns "tools"
# tools     -> result: 42                        -> edge goes back to llm
# llm_node  -> no tool_calls, final answer       -> route returns END

# Output:
The result of 6 multiplied by 7 is 42.

The routing function is just a plain Python function - you can put any logic there. Return a string matching a node name to continue, or END to stop. You can also return different node names for different cases, for example routing to an error_handler node if the tool raised an exception.


 
  


  
bl  br