📘 Introduction

Most basic LLM examples answer one message at a time. That works for simple prompts, but it feels strange in a real chat application because the assistant forgets what the user said a few seconds ago. If a user says their name, describes a project, and then asks a follow-up question, your app needs chat history to keep the conversation useful.

In this tutorial, you will learn how LangChain helps you add memory-style behavior with chat history. We will start with the idea, set up a small Python project, inspect how messages are stored, and then build a runnable chat example that can answer follow-up questions using previous context.

💡
For modern LangChain apps, think of memory as chat history that your chain can receive and use. The important skill is passing the right previous messages into the next model call.

💡 What is LangChain memory?

LangChain memory is the pattern of giving an LLM access to earlier parts of a conversation. Instead of sending only the latest user message, your application sends the latest message together with relevant previous messages.

In current LangChain code, this is often done with chat message history and RunnableWithMessageHistory. The message history stores previous user and assistant messages, while the runnable uses that history when it calls the model.

ConceptBeginner-friendly meaningExample
Chat historyThe previous messages in a conversationUser says their name, then asks a follow-up question
Short-term memoryRecent context used during the current sessionA chatbot remembers what was discussed five messages ago
Persistent memoryConversation or user data saved beyond the current processA support assistant loads past tickets from a database

🧠 Why chat history matters in AI apps

Chat history makes an AI app feel more natural. Without it, every message is isolated. With it, the assistant can understand follow-up questions, refer back to earlier details, and avoid asking the user to repeat the same information.

For example, imagine a user says they are building a LangChain learning plan for beginners. If they later ask, "Can you make it easier?", the assistant needs the earlier topic to understand what "it" means.

Memory does not mean the model magically remembers everything forever. Your application must decide what history to store, how much to pass back, and when persistent storage is needed.

✅ Prerequisites

Before getting started, make sure you have:

☑️ Python installed
☑️ Basic Python knowledge
☑️ An OpenAI API key or another supported chat model provider
☑️ A terminal or command prompt

⚙️1️⃣ Create a project folder

Create a new local project folder for this tutorial:

mkdir langchain-memory-chat-history
cd langchain-memory-chat-history

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

On Windows, activate it with:

.venv\Scripts\activate

📦3️⃣ Install libraries

Install the packages we need for this tutorial:

pip install langchain-core langchain-openai

🔐4️⃣ Set your API key

If you use OpenAI, set your API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"
🔐
Keep secrets outside your code. Environment variables are enough for this beginner tutorial and safer than hardcoding keys into Python files.

🧾5️⃣ Understand chat messages first

Before connecting a model, let’s inspect what chat history looks like in code. Create a file named inspect_history.py and add a few messages manually.

from langchain_core.chat_history import InMemoryChatMessageHistory

history = InMemoryChatMessageHistory()

history.add_user_message("Hi, my name is Maya.")
history.add_ai_message("Nice to meet you, Maya.")
history.add_user_message("I want to learn LangChain memory.")

for message in history.messages:
    print(type(message).__name__, "->", message.content)

inspect_history.py

Run the script:

python inspect_history.py

You should see the conversation messages printed in order:

HumanMessage -> Hi, my name is Maya.
AIMessage -> Nice to meet you, Maya.
HumanMessage -> I want to learn LangChain memory.

This is the basic mental model. Memory starts with messages. The next step is sending those messages to a model automatically when the user asks a follow-up question.

🎓
Want the full implementation? In the Academy section, we connect chat history to a real chat model and test whether it remembers details from the earlier conversation.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In