LangChain and LangGraph · Chapter 15 of 25
create_agent
By the end of this chapter you can build RoomRaja's agent in one call with create_agent, say exactly what graph it builds, give it a system prompt, make it answer in a Pydantic schema, put middleware around its model calls, and decide when to use it and when to write the graph yourself. 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 14's agent took a model node, a ToolNode, tools_condition and three edges. Every agent Sid builds starts with those same lines, and most of them then need the same extras: a system prompt, a limit on how many times the model is called, a structured answer at the end, a summary when the conversation grows long, a human's approval before a risky tool runs.
LangChain packages all of that as create_agent, from langchain.agents. It is not a different kind of agent: it builds the LangGraph graph you built by hand, plus places to hook in the extras. Because you built that graph yourself first, nothing it does is hidden from you.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
prebuilt.py: a prebuilt agent
Build RoomRaja's agent with create_agent from langchain.agents instead of by hand: the model is a ChatOpenAI for lab-chat, the tools are chapter 14's four (get_tenant, rent_due, raise_ticket, ticket_status, each a @tool around RoomRaja's function of the same name), and the system prompt is You are RoomRaja's tenant assistant. Use the tools to answer.
Invoke it with the human message My geyser is not working in BAN-204, please send someone and print only the text of the last message.
- Task 2
schema.py: an agent that answers in a schema
Mrunal wants repair messages turned into data, not prose. Make a Pydantic model Triage with room_id (a string), property_id (one of KOT, VMN, HIN, BAN, WAK, KHR) and issue (one of wifi, plumbing, electrical, furniture, housekeeping). Build an agent with create_agent: lab-chat, no tools (an empty list), the system prompt You turn tenants' WhatsApp messages into repair requests. and Triage as its response_format.
Invoke it once for each message, in order, and print one line each from the result's structured_response: the room, the word at, the property, a colon and a space, then the issue. For example: KOT-104 at KOT: wifi
Water is leaking from the ceiling in KOT-206, please come fast
The chair in VMN-104 is broken
- Task 3
limit.py: middleware that stops a runaway agent
lab-chat-loopy asks for the same tool again after every result. Build an agent with create_agent for lab-chat-loopy, the one tool ticket_status (a @tool around roomraja.ticket_status), and middleware: a ModelCallLimitMiddleware from langchain.agents.middleware with a run_limit of 3. Invoke it with the human message What is the status of TK5051? and print two lines: Model calls: and the number of AI messages in the result that ask for tools, then the text of the last message.
- Task 4 · interview question
Interview: compare.py, by hand or prebuilt
Build chapter 14's agent graph by hand and an agent with create_agent, both with the same ChatOpenAI for lab-chat and one tool, rent_due (a @tool around roomraja.rent_due), and no system prompt. Stream each, by hand first, with the human message I am T1077. Is my rent paid? and print one line each: By hand or create_agent, a colon and a space, the nodes that ran joined with an arrow ( -> ), a colon and a space, then the text of the last message of the last update.
The interviewer will ask: what does create_agent build, and when would you write the graph yourself?
What you will learn
- create_agent: the agent graph in one call, and what it really builds.
- system_prompt: where it goes, and why it is not in the state.
- response_format: an agent whose final answer is a Pydantic object.
- Middleware: hooks that run before or after the model, as nodes of the graph.
- By hand or prebuilt: when each is the right choice.
More in LangGraph
Build "create_agent" 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.