Skip to main content
In this tutorial, you’ll build a conversational chatbot that maintains chat history and uses LangChain models to generate intelligent responses.

What you’ll build

A chatbot that:
  • Maintains conversation history
  • Integrates with LLM providers
  • Handles multiple turns of conversation
  • Uses message state management

Prerequisites

Install required packages:
Set your API key:

Tutorial

1

Define the chatbot state

Use LangGraph’s message handling to manage conversation history.
The add_messages annotation:
  • Automatically appends new messages
  • Maintains conversation order
  • Handles message deduplication
2

Create the chatbot node

Build a node that calls an LLM to generate responses.
The chatbot node:
  • Receives all previous messages
  • Sends them to the LLM
  • Returns the AI’s response
3

Build the graph

Create a simple graph with the chatbot node.
4

Have a conversation

Run the chatbot with multiple conversation turns.
Each call:
  • Includes full conversation history
  • Maintains context
  • Generates contextual responses
5

Add conversation loop

Create an interactive chat experience.
6

Complete example

Here’s the full working chatbot:
Save as chatbot.py and run:

Expected output

When you run the chatbot:

Key concepts

  • Message History: add_messages automatically manages conversation history
  • BaseMessage Types: HumanMessage, AIMessage, SystemMessage
  • State Updates: Each node can append messages to the conversation
  • Model Integration: Easy integration with LangChain model providers

Enhancements

Next steps

Add Tools

Give your chatbot the ability to use tools

ReAct Agent

Build a reasoning and acting agent
This chatbot forms the foundation for more advanced agents. The next tutorials will add tool calling and reasoning capabilities.