Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

In Spring AI, what are advisors, and how would you use them to add chat memory and retrieval to a ChatClient?

Advisors are interceptors around a ChatClient call: each one sees the request on the way to the model and the response on the way back, and can change either. MessageChatMemoryAdvisor, backed by a ChatMemory such as MessageWindowChatMemory, adds the earlier messages of a conversation to the prompt and records the new ones, while QuestionAnswerAdvisor or RetrievalAugmentationAdvisor searches a VectorStore and adds the relevant documents as context. Advisors run in order, which decides what each one sees, and we pass a conversation id with every request so memory is kept per user conversation.

Why this question is asked

A model on its own remembers nothing between calls and knows nothing about our documents. Almost every real assistant needs both, and without a framework we would end up with the same code to load history, search documents and assemble prompts copied into every service. Spring AI solves that with advisors. Interviewers ask about them because using them well requires understanding the chain they form, the order they run in and the per-conversation state they keep. In this answer, we will see what an advisor is, write a tiny one, add chat memory, add retrieval, and then look at ordering and conversation ids.

1What an Advisor Is

If we have used servlet filters or Spring's HandlerInterceptor, advisors will feel familiar. When we call a ChatClient, the request does not go straight to the ChatModel. It passes through a chain of advisors. Each advisor receives the request, can change it, for example by adding messages or context, then passes it on to the next advisor in the chain. The last link calls the model. The response then travels back through the same advisors in reverse order, and each can read or change it, for example to store it or log it.

This design keeps cross-cutting behaviour out of our business code. Memory, retrieval, logging and safety checks become reusable pieces that we attach to a client with defaultAdvisors() or to one request with advisors().

2A Tiny Advisor to See the Mechanism

In Spring AI 1.0, a blocking advisor implements CallAdvisor (and a streaming one StreamAdvisor). This one logs the size of the prompt and the time the model took. The interface names changed during the milestones, so check them for your version.

Java
class TimingAdvisor implements CallAdvisor {
public ChatClientResponse adviseCall(ChatClientRequest request, CallAdvisorChain chain) {
log.info("messages in prompt: {}", request.prompt().getInstructions().size()); // 4
long start = System.nanoTime();
ChatClientResponse response = chain.nextCall(request); // the rest of the chain, then the model
log.info("model took {} ms", (System.nanoTime() - start) / 1_000_000); // 1830
return response;
}
public String getName() { return "timing"; }
public int getOrder() { return Ordered.HIGHEST_PRECEDENCE; } // runs first, sees everything
}

Notice the single call to chain.nextCall(request). Everything before it acts on the request, everything after it acts on the response. An advisor could also skip the call entirely, for example to return a cached answer or block a request.

3Adding Chat Memory

Chat memory makes a conversation feel continuous. ChatMemory is the interface that stores messages per conversation, and MessageWindowChatMemory is an implementation that keeps the last N messages, 20 by default, so the prompt does not grow forever. Its storage is a ChatMemoryRepository, in memory by default, or JDBC and other stores for persistence across restarts. MessageChatMemoryAdvisor uses it: before the call it loads the conversation's previous messages and adds them to the prompt, and after the call it saves the new user message and the answer.

Java
ChatMemory memory = MessageWindowChatMemory.builder()
.chatMemoryRepository(jdbcChatMemoryRepository) // survives restarts
.maxMessages(20)
.build();
ChatClient chatClient = builder
.defaultAdvisors(MessageChatMemoryAdvisor.builder(memory).build())
.build();
String reply(String conversationId, String text) {
return chatClient.prompt()
.user(text)
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId)) // per conversation
.call()
.content();
}
// reply("c-17", "My name is Priya.") -> "Nice to meet you, Priya."
// reply("c-17", "What is my name?") -> "Your name is Priya."
// reply("c-99", "What is my name?") -> "I don't know your name yet."

The conversation id is what keeps users apart. Without it, every request would share one default conversation, and one user's messages would appear in another's prompt, which is both a bug and a data leak. The id should come from our server side, such as a conversation row owned by the authenticated user, never directly from untrusted input. The parameter constant shown is the 1.0 name; it was different in milestones, so check your version.

4Adding Retrieval From a VectorStore

Retrieval gives the model knowledge it was not trained on. QuestionAnswerAdvisor takes the user's question, runs a similarity search on a VectorStore, and adds the matching documents to the prompt with instructions to answer from that context. RetrievalAugmentationAdvisor is the more modular option, built from pieces such as a query transformer, a document retriever and a query augmenter, useful when we want to rewrite the question or combine several retrievers.

Java
ChatClient chatClient = builder
.defaultAdvisors(
MessageChatMemoryAdvisor.builder(memory).build(),
QuestionAnswerAdvisor.builder(vectorStore)
.searchRequest(SearchRequest.builder().topK(4).similarityThreshold(0.6).build())
.build())
.build();
// "How many vacation days do new employees get?"
// prompt now holds: system text, 6 earlier messages, 4 policy chunks, the question
// -> "New employees get 25 days a year, according to the Leave Policy section 2."
Step throughOne request through two advisors
ServiceMemory advisorRetrieval advisorModel
user: How many vacation days?
load history for conversation c-17
request + 6 earlier messages
search VectorStore, top 4 chunks
history + chunks + question
answer
answer
save question and answer to c-17
25 days a year...

9 messages in order. Play them, or step through them one at a time.

5Why the Order of Advisors Matters

Each advisor has an order value, and lower values run earlier on the way in and later on the way out, as with Spring's Ordered interface. That decides what each advisor sees. If a safety advisor that rejects forbidden requests runs first, nothing else wastes work on them. If the memory advisor runs before retrieval, the retrieval advisor sees a prompt that already contains history, but it searches with the user's current text; for follow-up questions such as And for part-time staff?, that text alone retrieves poorly, which is a reason to add a query rewriting step with RetrievalAugmentationAdvisor. On the way back, the memory advisor should save the final answer after any advisor that modifies it. So we set orders deliberately instead of relying on defaults, and we write a test that inspects the final prompt.

6Follow-up questions

Where is the memory stored, and is it safe across instances?

By default MessageWindowChatMemory uses an in-memory repository, which is lost on restart and not shared between instances. For a real service we use a persistent repository such as the JDBC one, so any instance can continue any conversation.

What is the difference between MessageChatMemoryAdvisor and PromptChatMemoryAdvisor?

MessageChatMemoryAdvisor adds the history as real messages in the prompt, preserving roles. PromptChatMemoryAdvisor writes the history as text into the system message. The message form usually works better with modern chat models; the text form helps with models or prompts where separate messages are not wanted.

Can retrieval respect permissions?

Yes, through a filter expression on the search request, for example limiting chunks to the user's groups, which can be set per request. The filter must come from the authenticated user on the server, not from the prompt.

7Conclusion

In this answer, we saw that advisors are interceptors around a ChatClient call that can change the request and the response, and we wrote a small one to see the chain. MessageChatMemoryAdvisor with a MessageWindowChatMemory added a conversation's earlier messages and saved new ones, QuestionAnswerAdvisor and RetrievalAugmentationAdvisor added documents from a VectorStore, the order of advisors decided what each saw, and a server-side conversation id kept each user's memory separate.

What to say in the interviewAbout 80 seconds aloud

Open with

Advisors are Spring AI's interceptors for ChatClient. When I call the client, the request goes through a chain of advisors before it reaches the ChatModel, and the response comes back through them in reverse, so each one can modify the prompt or the answer, similar to servlet filters.

Then, if they want more

For memory I create a ChatMemory, usually MessageWindowChatMemory that keeps the last twenty or so messages, backed by a persistent repository like the JDBC one, and register MessageChatMemoryAdvisor. Before each call it loads the conversation's earlier messages into the prompt, and afterwards it saves the new question and answer.

The key detail is the conversation id: I pass it per request as an advisor parameter, the ChatMemory.CONVERSATION_ID constant in 1.0, and it comes from my server side, otherwise users would share history. For retrieval I add QuestionAnswerAdvisor with a VectorStore and a SearchRequest, say top four with a similarity threshold, and it adds the relevant chunks as context.

RetrievalAugmentationAdvisor is the modular alternative when I want query rewriting or several retrievers. Advisors have an order, and lower values run first on the request and last on the response, so I place safety checks first, and I make sure memory saves the final answer. I also verify the final prompt in a test.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring AI reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring AI, what are advisors, and how would you use them to add chat memory and...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/54b81dda91/