Message history management is a central element in the development of conversational agents with LangChain. A good structuring of the history allows for more relevant, contextual, and coherent responses while optimizing resource usage. In this article, we break down the main features offered by LangChain to manipulate messages and maintain an effective history.
Why manage message history?
A conversational agent relies on context to generate its responses. This context is generally formed by the history of interactions between the user and the system. Without memory or history management, each request would be treated as a new conversation, making the agent unable to maintain a logical thread.
LangChain provides powerful tools to store, update, and manipulate this history in the form of standardized "messages".
Message types in LangChain
LangChain offers several types of messages to structure exchanges:
HumanMessage: represents messages sent by the user.
AIMessage: represents responses generated by the model.
SystemMessage: allows adding initial instructions to the model.
FunctionMessage and ToolMessage: useful for interaction with external functions or tools.
All these messages inherit from the BaseMessage class, allowing for uniform manipulation.
Creating a history
The history is typically a list of messages. LangChain provides classes like ChatMessageHistory or ConversationBufferMemory to centralize this management.
Basic example:
python
History management
In addition to storing and maintaining history, LangChain provides tools to effectively manipulate message lists.
filter_messages
This function allows filtering messages by type, name, or identifier. It is useful for targeting only certain parts of the history, for instance, to keep only user messages:
python
trim_messages
The <code>trim_messages function is useful for truncating a history to meet a length limit (in tokens or number of messages). This is essential when trying to avoid context overflow with a LLM.
It uses a LLM model to calculate the number of tokens consumed by each message and progressively removes the oldest ones (by default) until the limit is respected. This method ensures that the provided context remains relevant while considering the model's constraints. There are also many other parameters to reduce your message history:
python
Using memories
LangChain offers several types of memories to retain message history in an agent.
Here is a complete example illustrating the use of a ConversationBufferMemory memory in a LangChain chain:
python
This script shows how memory allows the model to remember the previous conversation and respond contextually.
LangChain offers several types of memories to keep message history in an agent. Each memory has its specificities and advantages depending on the intended scenario.
ConversationBufferMemory
This is the simplest memory: it stores all messages in order.
python
ConversationBufferWindowMemory
This memory limits history to the k most recent messages.
python
ConversationSummaryMemory
Summarizes the history to retain only essential points.
python
ConversationKGMemory
Builds a knowledge graph from the history.
python
Integration into a LangChain agent
The history is typically passed at the time of creating a chain or agent via the memory parameter:
python
Best practices for history management
Choose the right type of memory based on the use case: long contexts, simple interactions, summary...
Regularly clean or truncate the history to avoid high processing costs.
Add SystemMessages to guide the model's behavior at the start of interactions.
Leverage advanced types like FunctionMessage to integrate API calls.
Conclusion
LangChain offers great flexibility for managing conversational histories. By choosing the right memory and structuring your messages wisely, you can create smarter, more coherent agents tailored to your needs. Feel free to experiment with the different types of memory to find the strategy that best fits your application.
If you are still unsure or need specific support, our team is here to help
Want to go further?
This topic is part of our Generative AI for Developers course. Browse the full programme, or get it by email.
Share with
Romain DE LA SOUCHÈRE
Tech Lead, CTO AXI Technologies
Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.