How To Create A Routing System?
Learn how to create a routing agent in LangGraph.
Imagine you have multiple possible actions that your AI can take: sometimes it should just reply directly, and other times it needs to call a tool—like performing a mathematical calculation—before replying. In the previous lesson, we learned about maintaining state and building a single-node graph where the node itself handled both processing and deciding whether to call a tool based on the input. This linear and straightforward approach works well for simple tasks but can become cumbersome as workflows grow in complexity.
Now, we’ll take this a step further by introducing a dynamic routing system that transforms our AI model into a kind of “router.” Just like a traffic signal directs cars either straight ahead or off to a side street, our routing system uses conditional edges to determine which node to run next based on the AI’s response. By externalizing decision-making to these conditional edges, we achieve a branched and scalable workflow where multiple nodes collaborate, allowing the AI to intelligently control the workflow’s direction. This cleaner separation of concerns not only makes our system more flexible but also enhances its ability to handle a wider variety of tasks seamlessly, paving the way for more sophisticated and intelligent AI workflows with LangGraph.
How does the routing mechanism work?
We already have a state and an AI model that can recall the entire conversation, as we created in the previous lesson. Now, we can introduce a routing mechanism to examine the AI’s response and decide what to do next. If the response suggests that a tool should be called—for example, to handle a calculation—we direct the workflow to a specialized node that executes this tool. Otherwise, we simply produce the AI’s direct answer and end the process. By adding this decision-making capability, we’re effectively giving the model the power to choose different paths based on the current context, creating a simple “agent” that can respond differently depending on user input.
Below is the final code from the previous lesson for reference. Notice how we defined a MessagesState
and set up a simple graph with a single node that reads the entire conversation and returns a response. We won’t re-explain these concepts, as they were thoroughly covered before.
Get hands-on with 1300+ tech skills courses.