Close

AI LangChain4j - Understanding MessageWindowChatMemory

[Last Updated: Jan 19, 2026]

What is MessageWindowChatMemory?

There are currently two ChatMemory implementations in LangChain4j:

  • MessageWindowChatMemory
  • TokenWindowChatMemory

We have already seen an example of MessageWindowChatMemory. In this tutorial we are going to understand the purpose of MessageWindowChatMemory.

MessageWindowChatMemory is a ChatMemory implementation that keeps only a fixed number of the most recent messages. Older messages are automatically discarded.

Why message windowing matters

Large conversations increase token usage and cost. Message windowing provides predictable memory growth and protects applications from exceeding model limits.

Typical use cases

  • Short-lived conversational flows
  • Chatbots with limited context needs
  • Cost-sensitive applications

Example

package com.logicbig.example;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import java.util.List;

public class MessageWindowChatMemoryExample {

    public static void main(String[] args) {

        ChatModel model = OllamaChatModel.builder()
                                         .baseUrl("http://localhost:11434")
                                         .numCtx(4096)
                                         .temperature(0.2)
                                         .modelName("phi3:mini-128k")
                                         .build();

        ChatMemory memory = MessageWindowChatMemory.withMaxMessages(2);

        // Add three messages to memory
        memory.add(UserMessage.from("My name is Alice."));
        memory.add(UserMessage.from("I live in New York."));
        memory.add(UserMessage.from("What is my name and where do I live?"));

        // Send to LLM and get response
        String response = model.chat(memory.messages()).aiMessage().text();
        System.out.println("\nLLM Response: " + response);
    }
}

Output


LLM Response: Your provided text does not contain your personal name, so it's impossible to determine that from the given information alone. However, based on the location mentioned - "New York" � we can infer you reside in New York City or one of its surrounding areas within the state of New York.

Conclusion

The output shows that the model responds using only the most recent messages. Earlier messages are not considered once the window size is exceeded, confirming automatic eviction behavior.

Example Project

Dependencies and Technologies Used:

  • langchain4j 1.10.0 (Build LLM-powered applications in Java: chatbots, agents, RAG, and much more)
  • langchain4j-ollama 1.10.0 (LangChain4j :: Integration :: Ollama)
  • JDK 17
  • Maven 3.9.11

AI LangChain4j - MessageWindowChatMemory Select All Download
  • message-window-chat-memory
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MessageWindowChatMemoryExample.java

    See Also

    Join