LangChain Chat Models

A chat model works like a text conversation with a very well-read assistant. You send a list of messages, and the assistant sends a message back. Almost every modern AI application uses chat models, because the message format carries roles, history, and instructions in an organized way.

Message Types

Every message carries a role. The role tells the model who is speaking.

SystemMessage

A system message sets the rules. It defines personality, tone, and limits. Users never see it, yet it guides every reply.

HumanMessage

A human message holds the user's input, such as a question or a command.

AIMessage

An AI message holds a reply from the model. Developers add earlier AI messages to the list to show the model what it already said.

ToolMessage

A tool message carries the result of an outside function. Topic 23 covers tools in detail.

The Conversation Flow

 [SystemMessage]  "You are a friendly travel guide."
 [HumanMessage]   "Suggest a weekend trip near Delhi."
        |
        v
   +------------+
   | Chat Model |
   +------------+
        |
        v
 [AIMessage]      "Try Jaipur for forts and food..."

Starting a Chat Model

The helper init_chat_model creates a model from a simple text name. The same line of code works for many providers, which makes switching easy.

from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-4o-mini", model_provider="openai", temperature=0)

You can also import a provider class directly.

from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4o-mini", temperature=0)

Sending Messages

from langchain_core.messages import SystemMessage, HumanMessage

messages = [
    SystemMessage(content="You are a friendly travel guide."),
    HumanMessage(content="Suggest a weekend trip near Delhi."),
]

reply = model.invoke(messages)
print(reply.content)

A short form uses plain tuples. LangChain converts them into message objects for you.

reply = model.invoke([
    ("system", "You are a friendly travel guide."),
    ("human", "Suggest a weekend trip near Delhi."),
])

Reading the Reply

The reply is an AIMessage object. It holds more than text.

  • content holds the answer text.
  • usage_metadata holds token counts for input and output.
  • response_metadata holds provider details such as the model name and finish reason.
print(reply.usage_metadata)
# {'input_tokens': 28, 'output_tokens': 61, 'total_tokens': 89}

Important Settings

Temperature

Temperature controls creativity. A value near 0 gives steady, repeatable answers. A value near 1 gives varied, imaginative answers. Pick low values for facts and math. Pick higher values for stories and brainstorming.

Max Tokens

Max tokens caps the reply length. A smaller cap lowers cost and stops runaway answers.

Timeout and Retries

Timeout limits how long you wait. Retries repeat a failed call automatically, which smooths over short network problems.

model = ChatOpenAI(model="gpt-4o-mini", temperature=0.2, max_tokens=300, timeout=30, max_retries=2)

Calling Many Inputs at Once

The batch method sends several requests together and returns replies in the same order.

answers = model.batch([
    "Name one fruit rich in vitamin C.",
    "Name one planet with rings.",
])

Common Mistakes

  • Sending only a HumanMessage and expecting a specific tone. Add a SystemMessage for control.
  • Printing the whole reply object instead of the content field.
  • Forgetting to set the API key before the first call.

Real-World Scenario

A language tutor app greets each learner in a gentle voice. The developer writes one system message that says the tutor speaks simply, corrects mistakes kindly, and replies in short sentences. Each learner message arrives as a human message. The model returns an AI message, and the app shows the content to the learner. Changing only the system message turns the same app into a strict exam coach or a playful storyteller. The messages shape the personality, and no other code changes.

Practice Tasks

  1. Write three system messages that make the same model sound formal, friendly, and humorous.
  2. Send the same question at temperature 0 and at temperature 1. Compare five replies each.
  3. Print usage_metadata for a short and a long question. Note how the numbers change.
  4. Use batch to answer four quiz questions in one call.

Key Points

  • Chat models read lists of role-based messages.
  • System messages set rules, human messages ask, and AI messages answer.
  • init_chat_model starts a model from a text name.
  • Temperature, max tokens, timeout, and retries tune behavior.