LangChain and LangGraph · Chapter 6 of 25

Tools

By the end of this chapter you can turn a Python function into a tool with @tool, say what the model is really sent about it, offer tools to a model with bind_tools, read the tool_calls it answers with, run a tool yourself and send its result back as a ToolMessage, and explain why the model never runs anything. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangChain basics
  • 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

Since chapter 1 the model has answered RoomRaja's questions from what PG policies usually say. "How much rent do I owe?" has one right answer, in RoomRaja's rent ledger, and it is different for each of 124 tenants. No prompt can hold that. Faisal wants the real number, late fee and all. Mrunal wants a leaking tap to become a real ticket in RoomRaja's system, not a promise.

RoomRaja's systems already know these things: import roomraja gives plain Python functions such as roomraja.rent_due("T1042") and roomraja.raise_ticket(...). This chapter lets the model ask for them.

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_tools.py: two RoomRaja tools

    Turn two of RoomRaja's systems into tools with the @tool decorator. rent_due(tenant_id: str) returns roomraja.rent_due(tenant_id); raise_ticket(room_id: str, issue: str, description: str) returns roomraja.raise_ticket(room_id, issue, description). Give each a docstring that says what it does (the model reads it), and import roomraja.

    Then print one line per tool, rent_due first: its name, its arguments' names in brackets separated by a comma and a space, a colon and a space, then its description. For example: get_room(room_id): Get a room's details.

    Read every part from the tool objects; do not type them in.

  2. Task 2

    choose.py: the model chooses a tool

    Bind your two tools, rent_due and raise_ticket, to lab-chat with bind_tools. Send each of these three messages on its own, in order:

    kitna rent baaki hai? Main T1047 hoon

    The tap in WAK-201 is leaking

    What time is breakfast?

    For each reply: if it has tool calls, print one line per call, the tool's name, a space, then its args dictionary as Python prints it; if it has none, print text: and a space, then the reply's text.

  3. Task 3

    round_trip.py: one round trip by hand

    Answer a tenant's question with a tool, doing every step yourself. Bind rent_due and raise_ticket to lab-chat. Send a system message, You are RoomRaja's tenant assistant., and the human message: How much rent do I owe? I am T1004

    Run the tool call the model asks for, send the conversation back with the model's message and the tool's result as a ToolMessage, and print only the model's final answer.

  4. Task 4 · interview question

    Interview: never_runs.py, the model never runs anything

    Bind only raise_ticket to lab-chat and send: The Wi-Fi in BAN-102 is not working

    Print four lines. Text: and the reply's content with repr. Tool call: and the first tool call's name, a space, then its args. Before: and the error message roomraja.ticket_status raises for TK5061 (the next ticket's id) at this point. Then run the tool call yourself, and print After: and the status roomraja.ticket_status('TK5061') now gives.

    The interviewer will ask: the model "raised a ticket". Did it? Who runs a tool, and why is that a good thing?

What you will learn

  • A tool: a function plus a description the model can read, made with @tool.
  • bind_tools: offering tools with every request.
  • tool_calls: the model's request to run a tool, with arguments.
  • ToolMessage: sending the result back, so the model can answer.
  • Who runs the tool: your code, never the model.

More in LangChain basics

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