Sections in this lesson

Advisors & Conversation Memory

Compose memory into a ChatClient without mixing users, conversations or retrieved knowledge.

Reviewed: · Content owner: Interview Preparator

What you will learn

  • Use MessageChatMemoryAdvisor with a bounded message window.
  • Derive conversation scope from trusted user identity.
  • Keep conversation ownership and archival history separate.

Before you start

The flow

  1. 1.Authorize conversation
  2. 2.Build scoped key
  3. 3.Read window
  4. 4.Call model
  5. 5.Store turn

Memory is selected context

MessageWindowChatMemory retains a bounded number of messages for future requests. It is not a complete audit archive or a knowledge base. A message-count window also does not guarantee a token budget when individual messages are large.

MessageChatMemoryAdvisor applies the memory to the client flow. Advisor ordering affects which information reaches later steps; test combinations of retrieval, memory and tool execution rather than assuming their order is irrelevant.

A conversation key is not permission

The example combines a validated trusted user identifier and a UUID. Two users using the same UUID still receive different memory keys. Before invoking it, a real endpoint must resolve the conversation and verify ownership; a user-chosen UUID is not proof of ownership.

Do not store a current conversation ID in a shared field. In-memory repositories are appropriate for a local example but lose data on restart and do not coordinate across replicas.

Test isolation and retention

The fake model captures actual prompts. Tests prove that alpha's earlier topic is absent from beta's request, then present again in alpha's follow-up. Extend this with eviction, deletion and restart tests for the chosen repository.

Keep sensitive history retention explicit. Storing everything forever to make a chatbot feel consistent is not a context or privacy strategy.

Try it

ConversationExample.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.interviewpreparator.lab;
import java.util.UUID;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
public final class ConversationExample {
private final ChatClient client;
public ConversationExample(ChatModel model) {
ChatMemory memory = MessageWindowChatMemory.builder().maxMessages(6).build();
this.client = ChatClient.builder(model)
.defaultAdvisors(MessageChatMemoryAdvisor.builder(memory).build()).build();
}
public String reply(String authenticatedUser, UUID conversation, String text) {
if (authenticatedUser == null || !authenticatedUser.matches("[a-zA-Z0-9-]+")) {
throw new IllegalArgumentException("Invalid trusted user ID");
}
if (conversation == null || text == null || text.isBlank() || text.length() > 500) {
throw new IllegalArgumentException("Conversation and bounded message are required");
}
// Resolve and authorize conversation ownership before calling this method in a real service.
String key = authenticatedUser + ":" + conversation;
return client.prompt().user(text).advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, key))
.call().content();
}
}

examples/spring-ai-lab/src/main/java/com/interviewpreparator/lab/ConversationExample.java · mvn -f examples/spring-ai-lab/pom.xml test. Java 21+, Spring Boot 4.0.5, Spring AI 2.0.2 where applicable. The demo uses synthetic models; live inference quality is a separate check.

Interview questions

Is message memory a knowledge base?

No. It selects conversation context; retrieval over maintained external sources solves a different problem.

Does a unique UUID grant access?

No. The service must authorize ownership before reading or deleting conversation state.

Why test actual prompts?

To detect cross-conversation leakage in composed context, not merely verify repository key construction.

Practice

Add a second conversation for alpha and verify that it does not inherit the first conversation.

Check your solution

  • Same-user follow-ups retain permitted context.
  • Other users and conversations stay separate.
  • The window size is not described as a token limit.

Official references

Continue learning

Category sequence

Spring AI

Lesson 6 / 7

Was this lesson useful?

Change notes: Phase 2: bilingual lesson with versioned executable reference.