LangChain and LangGraph · Chapter 13 of 25

Branches and loops

By the end of this chapter you can make a graph choose its next step from the state, draw a graph to check its shape, build a loop that goes back to an earlier step, and make sure every loop stops, even when the model never gives you what you were waiting for. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangGraph
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

Chapter 12's graphs run in a straight line: every node, every time. Real messages are not like that. Mrunal's phone has rent questions for Faisal, repairs for her own team and guest requests for the front desk, and each needs a different next step. Faisal's reminders should go out again until the rent is paid, but not forever. And the model itself fails now and then: a busy server, a rate limit. The assistant should try again, and give up politely when the model is down.

All three are the same two ideas: a branch (the graph picks the next node from the state) and a loop (an edge back to a node that already ran). With them, a graph can do what a chain cannot.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    route.py: route by intent

    Every message should go to the right person. Build a graph whose state has message, intent and reply. The classify node asks lab-chat for the message's intent with structured output (a Pydantic model whose intent is one of rent, deposit, notice, maintenance, guest, food, room_change, complaint, praise), sending the message alone. Then a conditional edge from classify picks one of three nodes, each returning the reply:

    finance, for rent and deposit: Faisal will reply about your INTENT today.

    repairs, for maintenance: Mrunal is sending someone to fix it.

    front_desk, for everything else: Thanks! The front desk will get back to you.

    All three go to END. Compile it as graph, invoke it once for each of these messages, in order, and print one line each: the intent, a colon and a space, then the reply.

    I paid rent on the 8th, why is there a late fee

    The fan in my room is making a loud noise

    Can my parents stay over this weekend?

    The check also sends your graph other messages.

  2. Task 2

    retry.py: a retry loop that stops

    The assistant must survive a model that fails now and then, and give up politely when it is down. Build a graph whose state has question, model, attempts (an int) and reply. The ask node makes a ChatOpenAI for the state's model with max_retries=0, adds one to attempts, and sends the question alone; on success it returns attempts and the reply's text, and on any exception only attempts. After ask, a routing function goes to END when there is a reply, back to ask while attempts is under 3, and otherwise to give_up, whose reply is:

    Sorry, the assistant is busy. Mrunal will reply soon.

    give_up goes to END. Compile it as graph, invoke it with the question Is there a gym at the Baner property?, the model lab-chat-flaky and attempts 0, and print two lines: Attempts: and the number, then the reply. The check also runs your graph with lab-chat-down and lab-chat.

  3. Task 3

    draw.py: the graph, drawn

    Tanvi wants a picture of the routing graph for the team. Build the same graph as task 1 (copy it), but do not invoke it: print only its Mermaid drawing, from the compiled graph's get_graph and then draw_mermaid. The drawing must show the three places classify can go, as dotted arrows, and no arrow from classify straight to END.

  4. Task 4 · interview question

    Interview: endless.py, the loop that never ends

    A draft loop: the write_draft node sends lab-chat a system message, Answer in at most 10 words., and the question When is rent due?, and returns the reply as draft and drafts plus one. The state has question, draft and drafts (an int, starting at 0). After write_draft, a routing function goes to END when the draft has at most 10 words, and otherwise back to write_draft.

    First run it as it is with a recursion_limit of 5 in the config, catch the error and print Endless: and the error's class name. Then build it again with a routing function that also stops once drafts reaches 3, run it with no limit, and print Fixed:, the number of drafts, the word drafts, a colon and a space, then the last draft. For example: Fixed: 3 drafts: the draft.

    The interviewer will ask: why does the first loop never end, how many model calls would it have made without the limit, and what is the real fix?

What you will learn

  • Conditional edges: a routing function that reads the state and names the next node.
  • The path map: telling LangGraph where a branch can go, and drawing the graph to check.
  • Loops: an edge back, a counter in the state, and a stop.
  • The recursion limit: LangGraph's safety net for a loop that never stops.
  • Model failures inside a node: catching them, and why ChatOpenAI retries by itself.

More in LangGraph

Build "Branches and loops" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.