"Modern LLMs talk in messages — system, human, and AI. LangChain gives you one clean way to speak that language to any provider."
Level: Intermediate · Time: ~12 min · Prerequisites: Module 15
Learning Objectives
By the end of this module, you will be able to:
- Explain what a chat model is
- Understand the message roles
- Call a chat model through LangChain
- Swap providers with minimal changes
1. Chat Models Speak in Messages
Today's LLMs are chat models: instead of one blob of text, they take a list of messages, each with a role:
| Role | Purpose |
|---|---|
| System | Sets behavior & rules ("You are a helpful tutor") |
| Human | The user's message |
| AI | The model's replies (kept for context) |
The conversation is this list, growing as it goes. Each time you want a reply, you send the entire list so far — the model has no built-in memory, so the message history is its memory. When a chat feels like it "remembers" what you said earlier, that's because the app kept appending messages and resending them.
Explain like I'm new: Think of a group chat with labels. The system message is the stage director's private note ("play a patient tutor"); human and AI messages are the visible back-and-forth. The model reads the whole thread to decide what to say next.
Concept: This message format replaced the older "completion" style, where you sent one raw string and the model continued it. Roles make intent explicit — the model knows which text is instruction (system), which is the user asking (human), and which is its own prior words (AI) — which is far more reliable than hoping it infers structure from a plain paragraph.
2. Calling a Model in LangChain
LangChain wraps every provider in a consistent interface. Conceptually:
[object Object], langchain_openai ,[object Object], ChatOpenAI
model = ChatOpenAI(model=,[object Object],)
response = model.invoke([
(,[object Object],, ,[object Object],),
(,[object Object],, ,[object Object],),
])
,[object Object],(response.content)invoke sends the messages and returns the AI's reply. That's the fundamental operation every LangChain app is built on.
Key idea: The system message is your most powerful control. It sets the model's role, tone, and rules for the whole conversation — the app-builder's equivalent of a job description for the AI.
3. Swapping Providers
Because LangChain standardizes the interface, changing from OpenAI to Anthropic or a local model is often a one-line change:
[object Object], langchain_anthropic ,[object Object], ChatAnthropic
model = ChatAnthropic(model=,[object Object],) ,[object Object],Real-world use case: A startup prototypes on one provider, then switches to a cheaper or faster one as it scales — without rewriting its app, because LangChain's interface stays the same. That flexibility is a real business advantage.
4. Settings That Matter
- Temperature: higher = more creative/random, lower = more focused/consistent. Roughly, 0 gives near-deterministic answers (good for extraction), while 0.7–1.0 adds variety (good for brainstorming).
- Max tokens: caps response length (and cost). Remember a token is about ¾ of a word, so 500 tokens is roughly 375 words.
- Streaming: show the reply token-by-token for a snappy UX — the user starts reading immediately instead of staring at a spinner.
Common mistake: Leaving temperature high for tasks that need consistency (data extraction, classification). For factual or structured work, turn it down so the model behaves predictably.
Try this: Send the same prompt twice at temperature 0, then twice at temperature 1. At 0 the two answers will be nearly identical; at 1 they'll diverge. Feeling that difference tells you which setting each of your own tasks needs.
Hands-On: Try This
Try this: Write a system message that turns an assistant into "a pirate who only answers about cooking." Notice how one message reshapes every reply. That's the leverage of the system role — set it once, shape everything.
✅ Checkpoint
- What are the three main message roles?
- What does the system message control?
- Why does LangChain make swapping providers easy?
Answers: 1) System, human, AI. 2) The model's behavior, tone, and rules for the whole chat. 3) It wraps all providers in one consistent interface.
Key Takeaway: Chat models take a list of messages with roles — system (sets behavior), human (user), AI (replies). LangChain calls any provider the same way (.invoke(...)), so you can swap models with minimal code. The system message is your key control, and settings like temperature tune creativity vs. consistency.
Further Learning
Adapted from the LangChain for Beginners curriculum (MIT License).