AI / LangChain4j interview questions
How does ChatMemory work in LangChain4j and what types are available?
ChatMemory in LangChain4j is the component responsible for maintaining conversation history across multiple exchanges with an LLM. Without it, every call to the model is stateless — the model has no knowledge of what was said in previous turns. ChatMemory solves this by accumulating the message history and injecting it into each subsequent LLM request.
LangChain4j ships two built-in ChatMemory implementations:
- MessageWindowChatMemory — Keeps the last N messages (by message count). When the window is full, the oldest messages are dropped to make room for new ones. Simple and predictable, but a very long first user message might push out important context.
- TokenWindowChatMemory — Keeps messages up to a maximum token count. Requires a tokenizer (model-specific) to count tokens accurately. More precise than message count for managing context window limits of the underlying LLM.
// Message-window memory — keep last 10 messages
ChatMemory memory = MessageWindowChatMemory.withMaxMessages(10);
// Token-window memory — stay under 4096 tokens
ChatMemory memory = TokenWindowChatMemory.builder()
.maxTokens(4096, new OpenAiTokenizer(GPT_3_5_TURBO))
.build();
// Inject into AI Services for automatic history management
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.chatMemory(memory)
.build();For multi-user applications where each user needs isolated memory, LangChain4j provides ChatMemoryProvider — a factory that returns a memory instance per memory ID. The memory ID is typically the user session ID or user account ID, passed as an annotated parameter on the AI Services method.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
