Close

AI LangChain4j - List vs ChatMemory in LangChain4j

[Last Updated: Jan 19, 2026]

We can use a simple List<ChatMessage> to create a multi-turn conversation (check out this example), but that is not same as using ChatMemory. In this tutorial we are going to see what the difference is.

Core Difference

List<ChatMessage> is just a data container, while ChatMemory is a managed conversation state system.

List<ChatMessage> (Manual Control)

A simple collection you manage completely yourself.

List<ChatMessage> messages = new ArrayList<>();

Your Responsibilities:

  • Adding messages in correct order
  • Removing old messages manually
  • Preventing unlimited growth
  • Managing session separation

Limitations:

  • No automatic message trimming
  • Easy to exceed token limits
  • No built-in strategies
  • No persistence support

ChatMemory (Managed System)

A contract defining how conversation history is managed.

ChatMemory memory = MessageWindowChatMemory.withMaxMessages(10);

Advantages:

  • Automatic message windowing
  • Pluggable strategies (window, token-based, persistent)
  • Built-in safety controls
  • Session-aware design

When to Use Each

Use List<ChatMessage> for:

  • Single-turn or very short conversations
  • Demos and examples
  • When you need absolute manual control

Use ChatMemory for:

  • Real applications
  • Multi-turn conversations
  • Multiple users/sessions
  • When you need persistence or scaling

Bottom Line

List<ChatMessage> is simple storage. ChatMemory is storage plus policy, lifecycle, and safety management.

See Also

Join