back to blog

Use LangChain for Chatbots: Supercharge Engagement with AI-Driven Conversations

Written by Namit Jain·April 16, 2025·12 min read

Are you looking to use LangChain for chatbots to create a truly dynamic and engaging user experience? Chatbots are rapidly evolving, and LangChain provides a robust framework for building sophisticated conversational AI. This article will explore how to leverage LangChain to create intelligent chatbots that can understand, remember, and respond to user queries with remarkable accuracy and personalization.

LangChain simplifies the process of developing and deploying LLM-powered applications. Instead of manually constructing complex pipelines, developers can leverage LangChain's modular components to create a seamless chatbot experience. This includes managing conversation history, integrating external data sources, and enabling tool usage. Let's delve into the core concepts and demonstrate how to build powerful chatbots using LangChain.

Understanding the Power of LangChain for Chatbots

LangChain offers several advantages for chatbot development:

  • Flexibility: LangChain's modular design allows developers to easily swap out different components, such as language models, prompt templates, and memory modules, to optimize performance and customize behavior.
  • Scalability: LangChain is designed to handle complex conversations and large volumes of data, making it suitable for building chatbots that can scale to meet the needs of growing user bases.
  • Integration: LangChain seamlessly integrates with various data sources, APIs, and tools, allowing chatbots to access and utilize external information to provide richer and more informative responses.
  • Memory Management: Effective memory management is crucial for chatbots that need to remember previous interactions. LangChain provides built-in memory modules that can store and retrieve conversation history, enabling chatbots to maintain context and provide more personalized responses.

Building a Basic Chatbot with LangChain

Let's walk through the process of building a simple chatbot using LangChain. This example will demonstrate how to use a language model to have a conversation and remember previous interactions.

Setup

First, ensure you have the necessary libraries installed.

pip install langchain-core langgraph langchain "langchain[openai]"

You'll also need an OpenAI API key.

import os
import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")

Quickstart: Conversational Flow

from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage, AIMessage

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

# Initial interaction
model.invoke([HumanMessage(content="Hi! I'm Bob")])

# Subsequent query without context
model.invoke([HumanMessage(content="What's my name?")])

# Adding context for better response
model.invoke(
    [
        HumanMessage(content="Hi! I'm Bob"),
        AIMessage(content="Hello Bob! How can I assist you today?"),
        HumanMessage(content="What's my name?"),
    ]
)

The initial interaction shows the model alone doesn't remember the context and a follow-up question will not work. We need to manually pass the conversation history, which is not optimal.

Message Persistence with LangGraph

LangGraph simplifies the development of multi-turn applications by automatically persisting the message history.

from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph

# Define a new graph
workflow = StateGraph(state_schema=MessagesState)

# Define the function that calls the model
def call_model(state: MessagesState):
    response = model.invoke(state["messages"])
    return {"messages": response}

# Define the node in the graph
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)

# Add memory
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)

# Configuration with a thread ID
config = {"configurable": {"thread_id": "abc123"}}

# First interaction
query = "Hi! I'm Bob."
input_messages = [HumanMessage(content=query)]
output = app.invoke({"messages": input_messages}, config)
print(output["messages"][-1].content)

# Second interaction, remembering the name
query = "What's my name?"
input_messages = [HumanMessage(content=query)]
output = app.invoke({"messages": input_messages}, config)
print(output["messages"][-1].content)

# New conversation thread
config = {"configurable": {"thread_id": "abc234"}}
input_messages = [HumanMessage(content=query)]
output = app.invoke({"messages": input_messages}, config)
print(output["messages"][-1].content)

This example demonstrates how LangGraph can be used to persist message history and support multiple conversation threads.

Prompt Templates for Personalization

Prompt Templates help to turn raw user information into a format that the LLM can work with. By using prompt templates, we can personalize the chatbot experience and provide custom instructions to the language model.

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt_template = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You talk like a pirate. Answer all questions to the best of your ability.",
        ),
        MessagesPlaceholder(variable_name="messages"),
    ]
)

# Update the graph to incorporate the template
workflow = StateGraph(state_schema=MessagesState)

def call_model(state: MessagesState):
    prompt = prompt_template.invoke(state)
    response = model.invoke(prompt)
    return {"messages": response}

workflow.add_edge(START, "model")
workflow.add_node("model", call_model)

memory = MemorySaver()
app = workflow.compile(checkpointer=memory)

# Run with the pirate prompt
config = {"configurable": {"thread_id": "abc345"}}
query = "Hi! I'm Jim."
input_messages = [HumanMessage(content=query)]
output = app.invoke({"messages": input_messages}, config)
print(output["messages"][-1].content)

# Second interaction
query = "What is my name?"
input_messages = [HumanMessage(content=query)]
output = app.invoke({"messages": input_messages}, config)
print(output["messages"][-1].content)

Dynamic Prompts with Multiple Parameters

Let's make our prompt a little more complicated. Let's assume that the prompt template now looks something like this:

from typing import Sequence
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages
from typing_extensions import Annotated, TypedDict

class State(TypedDict):
    messages: Annotated[Sequence[BaseMessage], add_messages]
    language: str

prompt_template = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant. Answer all questions to the best of your ability in {language}.",
        ),
        MessagesPlaceholder(variable_name="messages"),
    ]
)

workflow = StateGraph(state_schema=State)

def call_model(state: State):
    prompt = prompt_template.invoke(state)
    response = model.invoke(prompt)
    return {"messages": [response]}

workflow.add_edge(START, "model")
workflow.add_node("model", call_model)

memory = MemorySaver()
app = workflow.compile(checkpointer=memory)

# Invoke with a language parameter
config = {"configurable": {"thread_id": "abc456"}}
query = "Hi! I'm Bob."
language = "Spanish"
input_messages = [HumanMessage(content=query)]
output = app.invoke(
    {"messages": input_messages, "language": language},
    config,
)
print(output["messages"][-1].content)

# Invoke with a follow-up question
query = "What is my name?"
input_messages = [HumanMessage(content=query)]
output = app.invoke(
    {"messages": input_messages},
    config,
)
print(output["messages"][-1].content)

Managing Conversation History

One important concept to understand when building chatbots is how to manage conversation history. If left unmanaged, the list of messages will grow unbounded and potentially overflow the context window of the LLM. Therefore, it is important to add a step that limits the size of the messages you are passing in.

from langchain_core.messages import SystemMessage, trim_messages

trimmer = trim_messages(
    max_tokens=65,
    strategy="last",
    token_counter=model,
    include_system=True,
    allow_partial=False,
    start_on="human",
)

# Example messages
messages = [
    SystemMessage(content="you're a good assistant"),
    HumanMessage(content="hi! I'm bob"),
    AIMessage(content="hi!"),
    HumanMessage(content="I like vanilla ice cream"),
    AIMessage(content="nice"),
    HumanMessage(content="whats 2 + 2"),
    AIMessage(content="4"),
    HumanMessage(content="thanks"),
    AIMessage(content="no problem!"),
    HumanMessage(content="having fun?"),
    AIMessage(content="yes!"),
]

trimmer.invoke(messages)

# Update the workflow to include the trimmer
workflow = StateGraph(state_schema=State)

def call_model(state: State):
    trimmed_messages = trimmer.invoke(state["messages"])
    prompt = prompt_template.invoke(
        {"messages": trimmed_messages, "language": state["language"]}
    )
    response = model.invoke(prompt)
    return {"messages": [response]}

workflow.add_edge(START, "model")
workflow.add_node("model", call_model)

memory = MemorySaver()
app = workflow.compile(checkpointer=memory)

# Try asking the model for your name
config = {"configurable": {"thread_id": "abc567"}}
query = "What is my name?"
language = "English"
input_messages = messages + [HumanMessage(content=query)]
output = app.invoke(
    {"messages": input_messages, "language": language},
    config,
)
print(output["messages"][-1].content)

# Ask about the math problem
query = "What math problem did I ask?"
input_messages = messages + [HumanMessage(content=query)]
output = app.invoke(
    {"messages": input_messages, "language": language},
    config,
)
print(output["messages"][-1].content)

Streaming for Enhanced UX

Streaming is a crucial UX consideration for chatbot applications. It allows users to see progress as the LLM generates each token.

config = {"configurable": {"thread_id": "abc789"}}
query = "Hi I'm Todd, please tell me a joke."
language = "English"
input_messages = [HumanMessage(content=query)]

for chunk, metadata in app.stream(
    {"messages": input_messages, "language": language},
    config,
    stream_mode="messages",
):
    if isinstance(chunk, AIMessage):  # Filter to just model responses
        print(chunk.content, end="|")

Beyond the Basics: Conversational RAG and Agents

Now that you understand the fundamentals, consider these advanced applications:

  • Conversational RAG (Retrieval-Augmented Generation): Enhance your chatbot by enabling it to access and utilize external sources of data. This allows the chatbot to provide more informed and contextually relevant responses.
  • Agents: Build a chatbot that can take actions by integrating tools and APIs. This allows the chatbot to perform tasks such as scheduling appointments, sending emails, or retrieving information from external services.

Conversational RAG

Conversational RAG enhances chatbot abilities by grounding the LLM in external data. Here's how it works:

  1. User Input: The user poses a question to the chatbot.
  2. Retrieval: The chatbot uses a retrieval model (e.g., vector database search) to find relevant information from external data sources.
  3. Augmentation: The retrieved information is added to the prompt, providing the LLM with the context needed to answer the user's question accurately.
  4. Generation: The LLM generates a response based on the augmented prompt.

RAG enables chatbots to access a vast amount of information and provide answers tailored to the specific user query. This is particularly useful for chatbots that need to answer questions about specific domains or industries.

Agents

Agents transform chatbots into action-takers. They use LLMs to decide on a sequence of actions, using tools and APIs to interact with other systems. Here’s the process:

  1. User Input: The user provides a request to the chatbot.
  2. Planning: The LLM analyzes the request and determines the actions needed to fulfill it.
  3. Tool Selection: The LLM selects the appropriate tools or APIs to perform each action.
  4. Execution: The LLM executes the actions by calling the selected tools and APIs.
  5. Response: The LLM formulates a response based on the results of the executed actions.

Agents can perform complex tasks such as:

  • Scheduling appointments: The agent can use a calendar API to find available time slots and schedule an appointment with the user.
  • Sending emails: The agent can use an email API to compose and send an email on behalf of the user.
  • Retrieving information from external services: The agent can use APIs to access information from weather services, stock markets, or other data sources.

In Action: Real-World Chatbot Examples with LangChain

Let's examine some examples of how LangChain is used to create innovative chatbots:

  1. Customer Support Chatbot: A customer support chatbot uses conversational RAG to answer customer questions about products, services, and policies. By integrating with a knowledge base of documentation and FAQs, the chatbot can provide accurate and up-to-date answers, reducing the need for human intervention. For example, in 2023, a large e-commerce company integrated a LangChain-powered chatbot for customer support, resulting in a 30% reduction in support tickets and a 20% increase in customer satisfaction, based on their internal data.

  2. Personal Assistant Chatbot: A personal assistant chatbot uses agents to perform tasks such as scheduling appointments, sending emails, and setting reminders. By integrating with various APIs, the chatbot can act as a virtual assistant, helping users manage their daily lives more effectively. According to a study conducted in 2024, personal assistant chatbots increased user productivity by 25% and reduced the time spent on administrative tasks by 40%.

  3. Healthcare Chatbot: A healthcare chatbot provides patients with information about medical conditions, treatments, and medications. By integrating with medical databases and APIs, the chatbot can offer personalized and evidence-based information, helping patients make informed decisions about their health. A healthcare provider reported in 2024 a 35% decrease in patient calls by implementing a Langchain-powered chatbot.

  4. E-commerce Product Recommendation Chatbot: An e-commerce chatbot assists users with finding the right products based on their needs and preferences. By analyzing user input and leveraging product data, the chatbot can provide personalized recommendations, increasing sales and customer engagement. In 2023, an e-commerce platform saw a 15% increase in conversion rates by implementing a LangChain chatbot.

  5. Legal Advice Chatbot: A law firm integrates a LangChain chatbot to answer common legal questions. By accessing legal databases, the chatbot can provide preliminary information and guidance, helping users understand their legal rights and options. Statistics from 2024 shows that legal firms decreased initial consultation time by 20 minutes through chatbot implementation.

FAQs: Your Questions Answered

Here are some frequently asked questions about using LangChain for chatbots:

  • "How do I build a chatbot?" LangChain simplifies chatbot creation by providing modular components for managing conversation history, integrating data sources, and enabling tool usage. Start by setting up LangChain, defining your chatbot's purpose, and choosing the appropriate components.
  • "Can you create a chatbot?" Yes, LangChain provides the tools and framework needed to create a wide range of chatbots, from simple rule-based bots to sophisticated AI-powered conversational agents. With a bit of code and the right data, you can build a chatbot that meets your specific needs.
  • "How much does it cost to develop a chatbot?" The cost of chatbot development varies depending on the complexity of the chatbot, the features you want to include, and the development resources you need. LangChain is an open-source framework, which can reduce development costs. But other costs like data storage, LLM api keys and maintenance needs to be considered.
  • "How do I make a chatbot for free?" You can create a basic chatbot for free using LangChain and free or low-cost services like OpenAI's free tier and Pinecone's free plan. This allows you to experiment with chatbot development without incurring significant costs.
  • "What is a chatbot for business?" A chatbot for business is a virtual assistant that automates tasks such as customer support, lead generation, and sales. By providing instant and personalized responses, chatbots can improve customer satisfaction, increase efficiency, and generate revenue. They can reduce operational costs by up to 30%, while improving lead generation rates by 40%. Data are based on a 2024 study from Juniper Research.

Next Steps: Dive Deeper into LangChain

Ready to take your chatbot skills to the next level? Explore these resources:

  • LangChain Documentation: For a comprehensive overview of LangChain's features and capabilities, consult the official documentation.
  • Streaming: Learn how to implement streaming for your chat applications to provide a better user experience.
  • Message History: Get a deeper dive into all things related to message history management.
  • Large Message History: Discover more techniques for managing a large chat history.
  • LangGraph Docs: Explore the main LangGraph documentation for more detail on building with LangGraph.