Notebooks
A
Arize AI
Langgraph Router

Langgraph Router

agentsllmsLlamaIndexarize-phoenixopenaitutorialsevalsllmopsai-monitoringaiengineeringprompt-engineeringdatasetsllm-evallanggraphai-observabilityllm-evaluationsmolagentsanthropiclangchain

Open In Colab

LangGraph Router Pattern: Intent-Based Agent Routing

In this tutorial, we'll explore the Router pattern using LangGraph, a framework for building dynamic, stateful LLM applications. We’ll build a smart support assistant that can route customer queries to the appropriate specialized agent—Billing, Tech Support, or General Info—based on the user’s intent.

LangGraph enables this by letting us define a structured graph of logic, where a router node classifies the input, and conditional edges forward it to the correct sub-agent. Each agent uses local context (e.g., invoice history, troubleshooting tips) to respond intelligently.

We also trace this application using Phoenix, which gives us complete visibility into routing decisions, tool usage, and model interactions. This is helpful for debugging routing accuracy and understanding how the graph executes end-to-end.

[ ]
[ ]
[ ]
[ ]

Configure Phoenix Tracing

Make sure you go to https://app.phoenix.arize.com/ and generate an API key. This will allow you to trace your Langgraph application with Phoenix.

[ ]
[ ]
[ ]

LLM

[ ]
[ ]

Router

We define a Pydantic schema to structure the router’s output. This schema ensures the LLM returns one of three valid routing targets: "billing", "tech_support", or "general_info". We then wrap our LLM with with_structured_output, allowing LangGraph to enforce this structured response during routing.

[ ]

Defining Graph State

This state schema captures the lifecycle of a routed request. It stores the original user input (input), the classification decision made by the router (decision), and the final response generated by the appropriate support handler (output). Each node in the graph will read from and write to this shared state.

[ ]

Support Agent Nodes: Specialized Response Handlers

This section defines three specialized LLM-powered agents, each responsible for handling a different category of user queries:

Billing Agent: Uses user billing history and a billing policy context to respond to invoice or refund-related questions.

Tech Support Agent: Answers common troubleshooting queries using a predefined support knowledge base.

General Info Agent: Responds to account, subscription, and policy-related questions using general FAQs.

Each agent reads the input field from the graph's state and returns a generated output tailored to its domain. These agents form the execution endpoints of the router graph.

[ ]

Intent Classification & Routing Logic

This section introduces the decision-making logic that powers the routing mechanism:

classify_intent Node: Uses an LLM wrapped with a structured output schema to classify the user's query into one of three support categories — billing, tech_support, or general_info. The result is stored in the state's decision key.

route_to_agent Function: A conditional router that examines the classification result and sends the request to the corresponding specialized agent node. This allows LangGraph to dynamically direct queries to the most relevant response module.

[ ]

Building the Routing Graph

Here, we construct the complete LangGraph workflow by registering the nodes and defining their connections:

Node Registration: All agents (billing_agent, tech_support_agent, general_info_agent) and the classify_intent node are added to the graph.

Conditional Routing: After the graph starts at classify_intent, it uses the output of route_to_agent to forward the query to the appropriate agent node based on the classified intent.

Terminal Edges: Each agent node directly leads to END, finalizing the workflow after responding to the query.

This design reflects a typical customer support router architecture, enabling modular, extensible handling of diverse query types.

[ ]

Let's run some queries:

[ ]

Make sure to view your traces in Phoenix!

Evals

In this section we will add a simple eval that evaluates whether the router chose the correct subagent. We will use the tool calling eval template from Phoenix, as routing to sub agents mimics the process of picking a tool to call.

Get all root spans

[ ]
[ ]
[ ]

Add our subagent definitions as tools

Each column must contain information regarding the tools that could have been chosen from. Since our "tools" in this case are our subagents, we pass in the definitions (descriptions) of our subagents.

[ ]
[ ]

Extracting the subagent that was chosen.

[ ]
[ ]

Running Evals

[ ]
[ ]
[ ]

Exporting Evals to Phoenix!

[ ]
[ ]
[ ]
[ ]