LangChain and LangGraph · Chapter 14 of 25
An agent by hand
By the end of this chapter you can build an agent as a LangGraph graph: a messages state that grows instead of being replaced, a model node, a node that runs the tools the model asks for, and the routing that sends the run back to the model until it answers. You can also point at the loop in the graph and say what ends it. 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
In chapter 6 you did one tool round trip by hand: bind the tools, read the model's tool_calls, run the tool yourself, send a ToolMessage back, ask again. That is one round. A tenant's question may need two tools, or a second round after the first result, and you do not know in advance how many. An agent is that round trip in a loop: the model asks, the tools run, the model reads the results, and it goes round again until the model answers in words.
Chapter 13 gave you everything a loop needs: a node, a routing function, an edge back. This chapter builds the agent loop out of them, using three pieces LangGraph provides for exactly this: MessagesState, ToolNode and tools_condition. Chapter 15's create_agent builds this same graph in one line; here you see every part of it first.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
agent_graph.py: the agent loop as a graph
Build the agent loop as a LangGraph graph. Make one tool, ticket_status, with @tool around roomraja.ticket_status (a ticket_id parameter and a docstring). The state is MessagesState. The model node invokes lab-chat, with the tools bound, on the state's messages and returns the reply as a one-item list under messages; the tools node is a ToolNode of the same tools. Edges: START to model, a conditional edge from model with tools_condition, and tools back to model.
Compile it as graph, invoke it with the human message What is the status of TK5033? and print only the text of the last message.
- Task 2
roomraja_tools.py: RoomRaja's tools in the graph
Give the agent four tools, each a @tool around RoomRaja's function of the same name and parameters, with a docstring saying what it returns: get_tenant(tenant_id), rent_due(tenant_id), raise_ticket(room_id, issue, description) and ticket_status(ticket_id). Build the same graph as task 1 with all four, compile it as graph, and invoke it once for each question, in order, each as a new conversation:
I am T1061. How much rent do I owe?
The light in KHR-202 is not working, please fix it
Is breakfast included?
For each, print one line: the names of the tools the model asked for in that run, joined with a comma and a space (or the words no tools), then a colon and a space, then the final answer's text.
- Task 3
two_tools.py: two tools in one turn
Use task 2's graph and tools. Invoke it with the human message I am T1088, how much rent do I owe? Also what is the status of TK5047? and print every message of the final state, one line each, by its type:
a human message: human: and its text
an AI message that asks for tools: ai: asks and the tool names, joined with a comma and a space
a tool message: tool: and the tool's name
any other AI message: ai: and its text
- Task 4 · interview question
Interview: where_loop.py, where the loop is
Write a function that builds task 1's graph (the ticket_status tool) for a model name you pass it. For lab-chat, then lab-chat-loopy (a model that asks for the same tool again after every result), stream the graph with the human message What is the status of TK5040? and a recursion_limit of 6 in the config, and print one line: the model's name, a colon and a space, then the names of the nodes that ran, joined with an arrow ( -> ). If the run stops with an error, add a space and the error's class name in brackets.
The interviewer will ask: where is the loop in this graph, what normally ends it, and what ended the second run?
What you will learn
- MessagesState and add_messages: a state key that adds to a list.
- The model node: a model with tools bound, given the whole conversation.
- ToolNode: runs every tool call of the last AI message.
- tools_condition: the routing function that ends the loop.
- The loop: where it is in the graph, and what stops it.
More in LangGraph
Build "An agent by hand" 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.