LangChain and LangGraph · Chapter 24 of 25

Capstone

By the end of this chapter you have built RoomRaja's tenant assistant in one file: it answers policy questions with the rules it used, looks up a tenant's own rent and raises their tickets, remembers each tenant, walks a move-out to the deposit refund and waits for the manager when the policy says so, keeps answering when the main model is down, and comes with tests. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 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

Tanvi has set the date: on Monday the assistant answers Kothrud's tenants on WhatsApp, at any hour. Mrunal will read every conversation for the first week. Faisal will check every rupee of every refund it touches.

You have built each piece on its own. A real assistant is all of them at once, and the hard part is where they meet: which question goes to the policy and which to the tools, whose rent a tool may read, where a tenant's memory lives, what pauses and who resumes it. This chapter puts them together, with two new ideas on the way.

What you build: 5 tasks

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

  1. Task 1

    tenant_assistant.py: policy answers with sources

    Write answer_policy(question): make one Document per numbered rule of /workspace/data/policy.md (the lines like 2.2 After moving out...), with the rule's number in its metadata; embed them with lab-embed into an InMemoryVectorStore; put the 3 nearest rules in the prompt and have the model answer only from them, or say it does not know. Return {"answer": the reply's text, "sources": the 3 rules' numbers, like "5.1"}.

    Build the store the first time it is needed, so importing the file calls no model. The check asks about lunch, the notice period and an overnight friend, and compares your sources with the rules your prompt really sent.

  2. Task 2

    tenant_assistant.py: a chat that knows each tenant

    Write chat(tenant_id, message), returning the reply's text. Classify the message with the model into the nine intents of messages.jsonl. For rent, maintenance or complaint, an agent answers with tools that act for this tenant only and take no tenant id: rent_due (roomraja.rent_due for this tenant), raise_ticket(issue, description) for this tenant's room, and ticket_status(ticket_id). Anything else is answered from the policy, as answer_policy does.

    Each tenant's conversation lives in a checkpointed thread of its own: the check sends T1001 two messages and expects the second request to carry the first, then sends T1004 a message that must carry none of T1001's.

  3. Task 3

    tenant_assistant.py: the move-out and the manager

    Write move_out(tenant_id): a graph that gives notice (roomraja.give_notice), then asks for the deposit refund (roomraja.request_deposit_refund). When the refund is pending_approval (policy 2.4), it pauses with interrupt until the manager decides. Write manager_decides(tenant_id, approved): it resumes that tenant's move-out, records the decision with roomraja.approve_refund, and raises ValueError when nothing of that tenant's is waiting.

    Both return {"refund_id", "refund", "last_day", "status"}, status being approved, rejected or pending_approval. The check moves out T1002 (approved at once), T1012 (waits, then approved), and T1036 and T1037 together.

  4. Task 4

    tenant_assistant.py: never down

    Every model call (the classifier, the policy answer, the agent) uses the model named by the environment variable ROOMRAJA_MODEL, lab-chat when it is not set, with its own retries off, and falls back to lab-chat-mini when it fails.

    The check loads your file with ROOMRAJA_MODEL=lab-chat-down and asks chat and answer_policy: every call must try lab-chat-down once and get its answer from lab-chat-mini.

  5. Task 5

    test_tenant_assistant.py: tests for the move-out

    Write pytest tests for move_out and manager_decides. The check runs them on your tenant_assistant.py and on another correct one (both must pass, sending no request to the lab model), then on three copies with one careless change each, each of which must make a test fail: a refund that needs approval is approved without asking the manager; the manager's no approves the refund anyway; every tenant's move-out shares one thread, so a decision lands on whichever tenant waited last.

What you will learn

  • answer_policy (chapters 7 to 9): the policy as one Document per rule, the three nearest rules in the prompt, and the rules' numbers as sources.
  • chat (chapters 4, 12 to 16): a graph that classifies the message, sends account questions to an agent with tools and policy questions to RAG, with a thread per tenant.
  • move_out and manager_decides (chapter 17): notice, refund, and a pause for the manager.
  • A backup model (chapter 22) for every model call.
  • Tests for the move-out (chapter 23).

More in Production

Build "Capstone" 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.