Courses / Agentic AI from Scratch (Python)
Session 24 of 40
Conditional routing and several tools: policy, ticket or fallback
Notes
How to Route Questions in LangGraph With Conditional Edges and a Fallback Path
Agentic AI Foundations · Session 24 — Conditional routing: policy, ticket or fallback
Before you start: what do I need before I add conditional routing to a graph?
You need the code folder for this session open in your terminal. It holds routed_graph.py, tools.py (session 23’s two tools plus the new create_ticket), model_prompts/sort.md and model_prompts/ticket.md, the copilot from session 19 (helpdesk_rag.py and search.py) with build_index.py and load_policies.py, make_tickets_db.py, data/tickets.csv with five sample tickets and data/policies/ with the five policies. Install the libraries with pip install langchain langchain-openai langchain-chroma langchain-community langchain-text-splitters langgraph, and set LLM_BASE_URL, LLM_API_KEY, LLM_MODEL and EMBED_MODEL as in earlier sessions. Two things are not in the folder, policy_index/ and tickets.db; build them with python build_index.py and python make_tickets_db.py. Recap of what this session assumes (session 23): a graph is a state, nodes and edges; a node is a function that returns only the keys it changes; START and END are the graph’s entry and exit; and compile() returns the runnable graph. Have paper or a notes app ready for the Flashcards and the Model answer.
Summary: what is conditional routing in LangGraph?
Conditional routing lets one node lead to one of several others, chosen at run time. In session 23 the Copilot was a straight line, and the label written by the sort step was never read. Here a routing function reads that label from the state and returns the name of the next node, and add_conditional_edges follows it to a policy path, a ticket path or a fallback path. The ticket path can look up an existing ticket or create a new one, and the fallback path answers with a fixed sentence and makes no model call. Because the routing function ends by returning the fallback, a question that fits nowhere still leaves the graph with a safe, honest reply.
Key concepts: LangGraph routing terms explained in plain English
| Term | Definition |
|---|---|
| Conditional edge | A link from one node to several possible next nodes, where a function decides at run time which one to follow. |
| Routing function | A function that reads the graph’s state and returns the name of the next node; it does no other work. |
add_conditional_edges |
The builder call that takes a node, a routing function and the list of nodes it may return. |
Label (kind) |
The state key the sort node writes, policy, ticket or other, which the routing function reads. |
| Policy path | The node that hands the question to session 19’s copilot and adds the source to the answer. |
| Ticket path | The node that reads whether the person wants a look-up or a new ticket, then runs the matching tool. |
| Fallback path | The route for a question that fits no other path: a fixed sentence saying what the copilot can do. |
create_ticket |
A tool that adds a new row to tickets.db and returns the new ticket’s id. |
Explanations: how do you route questions in a LangGraph graph?
Why does a straight-line graph need a fork?
Failure first. An employee writes, “Please raise a ticket: my monitor flickers.” In session 23’s graph, the sort step labels it a ticket question, and the question still goes to the same agent as every other question. The label was written and then nothing read it.
Picture a mail room. If every letter went into one tray, a person would have to sort them later. A sorting desk reads each letter and places it in the right tray. A road junction with a signpost works the same way: the sign reads the destination and points the car down one road. A conditional edge gives the graph that signpost. A normal edge always goes from one node to the next; a conditional edge calls a function to determine the next node.
What is a conditional edge, and what does the fork look like?
In routed_graph.py, after sort the question goes to policy_node, ticket_node or fallback_node, and each path then ends at END:
START -> [ receive ] -> [ sort ]
|
route_by_kind
policy / ticket | \ anything else
v v v
[ policy_node ] [ ticket_node ] [ fallback_node ]
\ | /
v v v
END
The first two nodes are session 23’s receive and sort. The sort node still makes one model call with a typed reply, policy, ticket or other; the change is that something now reads the label. LangGraph’s documentation gives an example of this pattern: a routing workflow processes the type of a product question first and then routes it to a specific process.
What is a routing function?
A receptionist with a clipboard reads the reason for a visit and says which door to use. They do not do the work behind the door. A routing function is that receptionist: it reads the state and returns the name of the next node.
def route_by_kind(state):
if state["kind"] == "policy":
return "policy_node"
if state["kind"] == "ticket":
return "ticket_node"
return "fallback_node"
It returns names as plain text, and the names match the ones you gave to add_node. Anything that is not policy or ticket returns fallback_node, so even a label the model invents cannot leave the graph with nowhere to go.
How do you wire the conditional edge?
Think of fitting the signpost at the junction and then listing the roads it may point to. You tell LangGraph where the junction is, which function reads the sign and which roads exist:
builder.add_conditional_edges(
"sort", route_by_kind,
["policy_node", "ticket_node", "fallback_node"])
builder.add_edge("policy_node", END)
builder.add_edge("ticket_node", END)
builder.add_edge("fallback_node", END)
("sort", route_by_kind, [...]) means: after sort, call the function and follow the name it returns. The list names the nodes it may return, which lets LangGraph check the shape when you compile. sort has no plain edge to another node as well: LangGraph’s documentation warns that for one node you should use either static edges or dynamic routing, not both, because mixing them can run both paths. Each path then has a plain edge to END. A routing function receives the current state, as a node does.
What does each of the three paths do?
A help desk has an expert for policies, a clerk for tickets and a front-desk notice for everything else. The three nodes are those three.
The policy path is session 19’s copilot with the source added to the end of the answer:
def policy_node(state):
reply = ask_helper(state["question"])
if reply is None:
return {"answer": "The policy helper failed. ..."}
return {"answer": f"{reply['answer']} "
f"(source: {reply['source']}, {reply['section']})"}
The ticket path is one node that first makes a model call with a typed reply, TicketRequest, holding an action of lookup or create, a ticket_id and a title. model_prompts/ticket.md explains each. Then the matching tool from tools.py runs:
request = reader.invoke([("system", ticket_text), ("human", state["question"])])
if request.action == "lookup":
return {"answer": lookup_ticket.invoke({"ticket_id": request.ticket_id})}
return {"answer": create_ticket.invoke({"title": request.title})}
lookup_ticket is session 21’s tool. create_ticket is new: it writes a row to tickets.db, giving the next number after the highest id in the table, with status open and assignee unassigned. It changes data, so later sessions put a person’s approval in front of it. Reading a record and writing one are different risks.
What is a fallback path, and why does the graph need one?
A lost-property desk takes whatever matches no shelf. Without it, odd items pile up in the wrong place. A fallback path does the same for questions: the route for any question that fits no other path.
def fallback_node(state):
return {"answer": "I can't help with that. I answer policy "
"questions and look up or raise tickets. "
"Please raise it with a person."}
It makes no model call: a fixed sentence says plainly what the copilot can do and hands the person on. A model-written reply could be wrong in a new way each time; a fixed sentence cannot. The table shows how every label ends up somewhere:
| The sort step writes | route_by_kind returns |
The person sees |
|---|---|---|
policy |
policy_node |
A cited answer from the policies |
ticket |
ticket_node |
A ticket looked up or created |
other |
fallback_node |
The fixed “I can’t help with that” |
| a label the model invents | fallback_node |
The same fixed sentence |
How do you run the graph and read the three paths?
Run python build_index.py and python make_tickets_db.py once, then python routed_graph.py. The script asks three questions: the leave-days question, “Please raise a ticket: my monitor flickers.” and a weather question. For each question, read the node lines for three things. The first two nodes are receive and sort every time, and the sort line shows the label it wrote. The third node differs: policy_node for the leave question, which ends with a cited answer; ticket_node for the monitor request, which reports a new ticket id; and fallback_node for the weather question, which gives the fixed sentence. The ticket path writes a real row in tickets.db, and each run adds one, so run python make_tickets_db.py again to reset the table.
What goes wrong with routing in practice, and what does it mean for the design?
Before the design table, three situations you will meet:
- A ticket question is answered as a policy question. The sort step gave the wrong label, so the routing function followed it faithfully.
- A weather question gets a confident, invented answer. There was no fallback, so the question was forced down a path built for something else.
- A run raises a new ticket every time it is tried. The ticket path writes a row on each run, so a repeat creates a repeat.
What these mean for the design:
| What you learned | The design decision | Where it shows |
|---|---|---|
| The router trusts the label | Make the sort prompt say what each label means | model_prompts/sort.md |
| Some questions fit nowhere | End the routing function with a fallback | route_by_kind |
| A path may change data | Name it, and put a check in front of it later | create_ticket |
| The paths must be checkable | List the nodes the router may return | add_conditional_edges |
Basic assignment: run python routed_graph.py and, for each of the three questions, write down the nodes in the order they fired and the label sort wrote.
Project step: change one thing and see. Make route_by_kind always return "fallback_node" and say which questions now fail and what the person sees. Ask “Can I claim a taxi to the airport?” and say which path it takes and why. Ask for a ticket that does not exist, such as T-9999, and read what the lookup returns. Add a fourth label to Sorted and sort.md, such as greeting, with its own node that says hello. The build prompt is prompts/1-write-routed-graph.md. Stop when you can say why a fallback keeps every question from leaving the graph stuck.
Flashcards: test yourself on LangGraph conditional routing
Say the answer aloud before revealing it. Speaking is a different skill from reading, so practise both.
1. What is a conditional edge?
A link from one node to several possible next nodes, where a function decides at run time which one to follow. A normal edge always goes to one node.2. What does a routing function do?
It reads the state and returns the name of the next node. It does no other work: the nodes behind the names do the policy, ticket or fallback job.3. What do the arguments of add_conditional_edges mean?
The first is the node to leave, here sort; the second is the routing function; the third lists the nodes it may return, which lets LangGraph check the shape.4. Why does route_by_kind end with return "fallback_node"?
Anything that is not policy or ticket lands there, so a label the model invents still has somewhere to go and the graph is never left with no next node.5. What does the policy path do?
It calls session 19's ask_helper with the question and returns its answer with the source and section added on the end. If the helper fails, it returns a plain sentence.6. How does the ticket path decide between a look-up and a new ticket?
One model call with a typed reply returns an action of lookup or create, plus a ticket id or a title. Then lookup_ticket or create_ticket runs.7. Why does the fallback make no model call?
A fixed sentence that says what the copilot can do cannot be wrong in a new way. It is a safe, honest reply for a question that fits no path.8. Why is create_ticket riskier than lookup_ticket?
It writes a row to the database, so a repeat run raises a repeat ticket. Later sessions put a person's approval in front of actions that change data.9. A ticket question took the policy path. What do you change?
The model gave the wrong label, so the router followed it. Read model_prompts/sort.md, make the ticket line clearer, and run again. The routing function is working as written.10. Why list the nodes the router may return?
The list names every path the function can take, so LangGraph can check the shape of the graph. It also shows a reader at a glance where a question can end up.Model answer: how do I explain conditional routing in an interview?
The core question this session answers: “How do you make a LangGraph graph send policy questions, ticket questions and everything else down different paths?”
Four beats a teammate listens for, in this order: label first · a routing function · a conditional edge · a fallback. Skip any of these four and the answer is incomplete.
- Label first: a sort step writes a
kindinto the state. - A routing function: it reads the state and returns the next node’s name.
- A conditional edge:
add_conditional_edgesfollows that name aftersort. - A fallback: anything unplanned goes to a safe path.
The full answer, said aloud:
(Label first) “A sort node makes one model call and writes a label, policy, ticket or other, into the state.
(A routing function) A routing function reads that label and returns the name of the next node.
(A conditional edge) I add a conditional edge after the sort node, so LangGraph calls the function and follows the name it returns, and I join every path to END.
(A fallback) The last line of the function returns a fallback node, so a label nobody planned for still has somewhere safe to go.“
Missing a beat is a failure state. The follow-up question lands where you went quiet: leave out the fallback and the next question is what happens to a question you did not plan for. Practise until the four come out without prompting, then stop talking and let the questions come.
Why this matters: why does conditional routing matter at work?
In a conversation with a teammate or manager: when an assistant treats a request the wrong way, the question is which path it took and why. A router gives you an answer: the label, and the node it chose.
On the job: a real assistant meets questions nobody planned for. A fallback path, and a clear note of which paths change data, are the parts a product owner and a security reviewer ask about first.
In the rest of this course: session 25 saves progress between runs, session 26 puts a person’s approval in front of risky actions such as creating a ticket, and later sessions add more paths and checks to the same graph.
After this unit: what do I do after this lesson, and what comes next?
Assignment. Run python routed_graph.py and, for each of the three questions, write down which node reached END and why. The brief for the project work, forcing the fallback and adding a greeting label, is the project file. Keep routed_graph.py and tools.py: the next session builds on them.
What’s next. Session 25 is about saved progress: checkpoints that save the graph’s state after each step, threads that keep one conversation for each user, and durable execution that picks up where the work stopped.
Want help building this with someone?
Tell us what you are working on and we will write back about a mentor-led cohort, live sessions or 1:1 training. It is only an enquiry: you choose whether to go further.
Ask about mentor-led trainingTechEazy Consulting training material — not affiliated with or endorsed by OpenAI, Anthropic, LangChain or any other company named in this course.