LangChain and LangGraph · Chapter 1 of 25
Your first chat model
By the end of this chapter you can say what a chat model is and what LangChain adds on top of it, call a model from Python with LangChain, send it a system message and a tenant's message, read the reply and what it cost, switch to another model by changing one name, and explain why a model answers questions about RoomRaja confidently and wrongly. 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
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
RoomRaja runs co-living homes (PGs) in Pune: six properties, about 120 tenants. Every day the tenants write on WhatsApp: "rent kitna hai is month ka?", "geyser band hai", "can my parents stay over on Saturday?", "when do I get my deposit back?". Mrunal, who manages the Kothrud property, answers them one by one, late into the night. Tanvi, the founder, wants an assistant that answers tenants in minutes, any hour, and later does the work too: raises the repair ticket, checks the rent, starts a move-out.
Sid, RoomRaja's AI engineer, will build it with LangChain and LangGraph, the most used Python libraries for building with language models, and you build it with him. You do not need to have used a model before. This chapter starts at the very beginning: what a chat model is, how to ask it something from Python, and what it can and cannot know.
The lesson
A chat model writes text
A language model is a program trained on a huge amount of text to do one thing: given some text, write what most likely comes next. A chat model is a language model trained to continue a conversation: you send it messages, it writes the next one. It runs on a server; your program sends it a request over the network and waits for the reply.
Two things follow, and this whole lab builds on them:
- The model only writes text. It cannot look anything up or change anything. If it says "your ticket is raised", nothing was raised.
- It knows only what was in its training text, plus what you send it in the request. RoomRaja's policy was never in its training. Ask it about RoomRaja and it writes what such answers usually say.
This lab has its own model, lab-chat, running inside your lab with the same API as OpenAI's models. It behaves like a real model for everything the lab teaches; point your code at a real provider later and the same code talks to a real model.
Calling a model, with and without LangChain
Here is a question sent with the openai package, the provider's own library:
from openai import OpenAI
client = OpenAI()
r = client.chat.completions.create(
model="lab-chat",
messages=[{"role": "user", "content": "Is lunch included?"}],
)
print(r.choices[0].message.content)
All three meals, breakfast, lunch and dinner, are included in the rent.
And here is the same question with LangChain:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="lab-chat")
response = llm.invoke("Is lunch included?")
print(type(response).__name__)
print(response.content)
AIMessage
All three meals, breakfast, lunch and dinner, are included in the rent.
For one question the two look alike. What LangChain adds shows up as the program grows:
- One interface for every provider. OpenAI, Anthropic, Google and local models each have their own library and their own shapes. LangChain wraps each in a chat model class with the same methods (
invoke,stream,batch), so switching providers is a one-line change, not a rewrite. - Building blocks that fit together. Prompt templates, output parsers, tools, document loaders, vector stores and retrievers all speak the same interface, so they connect with almost no glue code. You meet them one chapter at a time.
- LangGraph for programs with many steps: workflows that branch, loop, remember and wait for a human. That is the second half of this lab.
ChatOpenAI finds the lab model the way the openai package does: the environment variables OPENAI_BASE_URL (where the server is) and OPENAI_API_KEY (the key) are set in your lab. Nothing in your code changes when they point at OpenAI instead.
What invoke gives back
invoke sends one request and waits for the whole reply. What comes back is not a string but an AIMessage: the model's message, with everything the provider said about it. Typed alone in the REPL it shows all of that:
>>> llm.invoke("Is lunch included?")
AIMessage(content='All three meals, breakfast, lunch and dinner, are included in the rent.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 16, 'prompt_tokens': 10, ...}, 'model_name': 'lab-chat', 'finish_reason': 'stop', ...}, id='lc_run--01a16425-...', tool_calls=[], invalid_tool_calls=[], usage_metadata={'input_tokens': 10, 'output_tokens': 16, 'total_tokens': 26, ...})
(trimmed). The parts you will use most:
.content: the text of the reply. This is what a tenant reads..usage_metadata: the tokens the request used (below)..tool_calls: empty here; chapter 6 is about them.
Messages and roles
A conversation is a list of messages, each with a role:
- system: instructions from you, the developer: who the model is, how to answer. The tenant never sees it.
- human (sent to the API as
user): what the tenant wrote. - ai (sent as
assistant): what the model said before, when you send a conversation back.
LangChain has a class for each, and invoke takes a list of them:
from langchain_core.messages import HumanMessage, SystemMessage
response = llm.invoke([
SystemMessage("You are RoomRaja's tenant assistant. Be brief."),
HumanMessage("What is the notice period?"),
])
print(response.content)
The notice period is 15 days, and you can give it to the warden on WhatsApp.
A shorter way writes each message as a pair, ("system", "...") and ("human", "..."); LangChain turns the pairs into the same messages. Order matters: the model reads the list top to bottom, so the system message goes first.
LangChain is not magic: in the end it sends the same request you would send by hand. lab requests 1 in the terminal shows exactly what the lab model received for the code above (trimmed):
{
"request": {
"model": "lab-chat",
"messages": [
{"content": "You are RoomRaja's tenant assistant. Be brief.", "role": "system"},
{"content": "What is the notice period?", "role": "user"}
]
},
"response": {
"choices": [{"message": {"role": "assistant", "content": "The notice period is 15 days, ..."}}],
"usage": {"prompt_tokens": 27, "completion_tokens": 21, "total_tokens": 48}
}
}
Your Check reads this same log to see what your code really sent.
Tokens: what a request costs
Models read and write tokens: pieces of words, about four characters of English each. Providers charge per token, for what you send (input) and what the model writes (output), and every model has a limit on how many tokens one request can hold. The reply tells you the counts:
print(response.usage_metadata)
{'input_tokens': 10, 'output_tokens': 16, 'total_tokens': 26, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}}
usage_metadata is a plain dictionary, written the same way for every provider. A system message is input too: the longer your instructions, the more every request costs. Faisal, who looks after RoomRaja's money, will ask you about this.
Switching models
init_chat_model makes a chat model from two names, the model's and the provider's:
from langchain.chat_models import init_chat_model
model = init_chat_model("lab-chat-large", model_provider="openai")
print(type(model).__name__, model.model_name)
print(model.invoke("Hi!").content)
ChatOpenAI lab-chat-large
Hi! This is RoomRaja. How can we help with your stay today?
It made a ChatOpenAI, the same class as before. With model_provider="anthropic" (and Anthropic's LangChain package installed) it would make Anthropic's chat model instead, and the rest of your program would not change. The lab has several models (lab models lists them): lab-chat-mini is the small, cheap one, lab-chat the everyday one, lab-chat-large the big one.
Confident, and wrong
Look again at the first answer: "All three meals, breakfast, lunch and dinner, are included in the rent." RoomRaja's policy (section 5.1 of /workspace/data/policy.md) says breakfast and dinner are included and lunch is not. The notice period answer was wrong too: it is 30 days, given in the app, not 15 days on WhatsApp.
The model is not lying and it is not broken. It never saw RoomRaja's policy, so it wrote what PG policies usually say, in the confident tone it was trained to use. Every model does this when asked about things it was not given. A tenant reading that answer would skip paying for lunch, or give notice the wrong way and lose money from their deposit.
So the assistant must give the model RoomRaja's facts before it answers. Pasting the whole policy into every request is one way; finding just the right part of it (RAG, class 2) and letting the model look things up with tools (chapter 6) are the ways this lab builds.
Your turn
Four tasks. Task 1 is your first call: print the reply's text, not the whole message. Task 2 adds a system message and the token counts. Task 3 asks two models through init_chat_model. Task 4 is an interview question: put the model's answer about deposits next to rule 2.2 of the policy, and be ready to say why the model sounds so sure, and what fixes it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
first_call.py: your first call to a chat model
Sid wants to see what a chat model says to a tenant's message before building anything. Make a ChatOpenAI for the model lab-chat, send it the tenant's question exactly:
Can I pay rent in cash?
and print only the text of the reply, on one line. The lab model answers at $OPENAI_BASE_URL with OpenAI's API, so ChatOpenAI finds it with no key to set.
- Task 2
with_roles.py: who the model is, and what it cost
Send lab-chat two messages in this order: a system message, You are RoomRaja's tenant assistant. Answer in one or two sentences., then the tenant's (human) message, My friend wants to stay over this weekend, is that okay?
Print the reply's text, then a second line with the request's tokens from the reply's usage_metadata, written as: Tokens: N in, N out (input tokens, then output tokens).
- Task 3
two_models.py: one interface, two models
Send the same two messages to lab-chat-mini, then to lab-chat: a system message, You are RoomRaja's tenant assistant., and the human message, Is cooking allowed in my room?
Make each model with init_chat_model from langchain.chat_models, naming the model and the provider openai. Print one line per model, in that order: the model's name, a colon and a space, then its reply.
- Task 4 · interview question
Interview: confident.py, confident and wrong
Ask lab-chat, with no system message, exactly: When will I get my deposit back after I move out?
Then read RoomRaja's real rule from /workspace/data/policy.md: the line that starts with 2.2. Print two lines: Model: and the model's reply, then Policy: and the text of rule 2.2 without its number. Read the rule from the file; do not type it in.
The interviewer will ask why the model answers so confidently, and what in this lab will fix it.
What you will learn
- What a chat model is, and what LangChain adds on top of it.
- ChatOpenAI and invoke: one question in, one message back.
- Messages and roles: the system message and the human's message.
- Tokens: what a request costs, from usage_metadata.
- init_chat_model: switching models by changing a name.
More in LangChain basics
Build "Your first chat model" 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.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.