Urgent.News

What's breaking now, across hundreds of outlets.

AI

Chat Memory per User in Java with LangChain4j and Claude: Using @MemoryId

Most AI examples end the same way when they go to production: a single shared conversation starts mixing people together. Ana tells the assistant her name, and a minute later Luis asks what his name is and gets Ana’s name back. Nothing crashed, nothing threw an exception, and the answer still looks confident. That is what makes this bug dangerous: it is silent. This article shows how to avoid it…

When AI applications move from development to production, they often encounter a common issue: a single chat history is used for all users, causing problems. In this article, we'll explore how to prevent this issue using Java, LangChain4j, Claude, and a feature called @MemoryId.

The example code can be found in the repository 04-chat-memory-memoryid. To avoid the shared conversation problem, we use an identifier for each conversation, a memory provider that creates a separate memory for each identifier, and a limit on how much history is kept.

Most introductory examples create one chat memory and reuse it for every call, which works for command-line demos but creates issues when used in web applications. With a shared memory, the model receives messages from all users, leading to privacy concerns and degraded quality as the model may provide incorrect answers based on irrelevant conversation history.

To address this, we use the @MemoryId annotation in LangChain4j, which allows us to describe the assistant as an interface with a single method taking two parameters: the memory identifier (userId) and the user's message. This design choice ensures that each conversation is isolated and cannot interfere with others.

The chat memory provider is responsible for creating a memory object for each identifier. In this example, we use MessageWindowChatMemory with a maximum of 10 messages per conversation. If the provider returned the same memory object for every identifier, we would encounter the same problem as in the initial shared memory scenario.

Additionally, we limit the conversation history to 10 messages, which helps control costs and processing time. Older messages are dropped, but it's important to decide what should remain in memory based on the nature of the conversation. For instance, a customer support chat may require a longer memory window than a quick question-and-answer tool.

The AI model is configured separately from the memory. In this example, we use Claude's AnthropicChatModel with a maximum of 256 tokens per response. This keeps the answers concise and helps manage costs.

Finally, we run a conversation with three turns. First, Ana introduces herself to the assistant, which sets her memory window. Then, Luis asks the assistant for his name, but since Luis has his own memory, the model should not know the answer. Finally, Ana asks again, and the model should return her name since it is still in her memory window.

By following these guidelines and using @MemoryId, LangChain4j, and Claude, developers can create multi-user assistants that maintain separate conversation histories, ensuring privacy and improving the quality of responses.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in AI

More from Saturday, 28 November 2026 →