LangChain and LangGraph · Chapter 12 of 25

Your first graph

By the end of this chapter you can say what LangGraph is for, build a graph from a state, nodes and edges, run it with invoke, watch what each node did with stream, and explain exactly what LangGraph does with the dictionary a node returns. 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

Mrunal's most common message is a repair: "the tap in my room is leaking". Answering it is not one call to a model. Someone has to read the message and find the room and the problem, raise a ticket in RoomRaja's system, and write back to the tenant with the ticket number and how long the fix will take. Three steps, one after the other, each using what the step before found.

A chain (chapter 3) can do a straight line like that. But Tanvi's list does not stay straight for long: a rent question needs a different path from a repair, a failed step should be tried again, an agent goes round and round between the model and its tools, and a conversation has to be remembered between messages. LangGraph is LangChain's library for programs like these: steps that share what they know, branch, loop, and can pause and resume. This chapter builds its smallest graph, from zero.

What you build: 4 tasks

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

  1. Task 1

    two_nodes.py: a graph with two nodes

    Build your first LangGraph graph. Its state is a TypedDict with four keys: tenant_id, name, room_id and welcome (all strings). Two nodes run one after the other: look_up, which finds the tenant with roomraja.get_tenant and returns their name and room_id, then welcome, which returns the welcome text:

    Welcome to RoomRaja, NAME! Your room is ROOM.

    Edges go from START to look_up, from look_up to welcome, and from welcome to END. Compile it into a variable called graph, invoke it with the tenant_id T1015, and print only the welcome text. The check also invokes your graph with other tenants.

  2. Task 2

    ticket_flow.py: a three-step ticket workflow

    Mrunal's most common message is a repair. Build a graph whose state has the keys message, room_id, issue, ticket_id, hours and reply, with three nodes in a line:

    extract: asks lab-chat for the room and the issue with structured output (a Pydantic model with room_id, and issue as one of wifi, plumbing, electrical, furniture, housekeeping), sending the tenant's message alone.

    file_ticket: raises the ticket with roomraja.raise_ticket(room_id, issue, message) and returns its ticket_id and fix_within_hours (as hours).

    write_reply: returns the reply, written exactly as: Ticket TICKET raised for ROOM (ISSUE). We will fix it within HOURS hours.

    Compile it as graph, invoke it with the message The tap in WAK-203 has been leaking since last night and print the reply.

  3. Task 3

    each_step.py: the state after each node

    Faisal wants to see what each step of the notice workflow did. Build a graph whose state has tenant_id, name, last_day and message, with three nodes in a line: look_up returns the tenant's name (roomraja.get_tenant), give_notice returns last_day from roomraja.give_notice, and confirm returns the message:

    NAME, your notice is recorded. Your last day is LAST_DAY.

    Each node returns only the one key it fills. Do not invoke the graph: stream it with the tenant_id T1030 and, for every node that ran, print one line: the node's name, a colon and a space, then the dictionary it returned.

  4. Task 4 · interview question

    Interview: node_returns.py, what a node returns

    The interviewer asks: what does a node return, and what does LangGraph do with it? Answer with a program. The state has two keys, message and intent. Build three one-node graphs (the node is called tag, from START to tag to END), each invoked with the message Can my brother stay over on Saturday? and print three lines:

    Kept: and the state from a graph whose node returns {"intent": "guest"}

    Typo: and the state from a graph whose node returns the misspelt {"intnet": "guest"}

    Not a dict: and the name of the error's class when the node returns the plain string guest

    Be ready to explain each line.

What you will learn

  • The state: one dictionary every step reads and adds to.
  • Nodes: plain Python functions, one per step.
  • Edges, START and END: the order the steps run in.
  • compile, invoke and stream: running a graph and watching each step.
  • What a node returns, and what LangGraph does with it.

More in LangGraph

Build "Your first graph" 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.