Quick revision
The short answer
We implement CallAdvisor (and StreamAdvisor if we stream): in adviseCall we copy the request with the email addresses in the user text replaced, pass it on with chain.nextCall(request), and then read the token usage from the response metadata and record it with Micrometer. Advisors form a chain ordered by getOrder(): lower values run first on the way in and last on the way out, so the redaction advisor gets a low order to run before memory and retrieval advisors see or store the text.
What to remember
In this answer, we wrote a CallAdvisor that copies the request with emails replaced, passes it on with chain.nextCall, and records prompt and completion tokens from the response metadata. We saw that advisors nest like function calls, with getOrder() deciding the position: lower values run first on the way in and last on the way out. A redaction advisor must therefore run before memory and retrieval advisors, and we register it with defaultAdvisors or per request.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We implement CallAdvisor (and StreamAdvisor if we stream): in adviseCall we copy the request with the email addresses in the user text replaced, pass it on with chain.nextCall(request), and then read the token usage from the response metadata and record it with Micrometer. Advisors form a chain ordered by getOrder(): lower values run first on the way in and last on the way out, so the redaction advisor gets a low order to run before memory and retrieval advisors see or store the text.
Why this question is asked
Advisors are Spring AI's way of adding behaviour around every model call without touching the code that makes the call, much like servlet filters around a request. Redacting personal data and recording usage are two of the most common reasons to write one. Interviewers ask this question because getting the order wrong is a real privacy bug: the email is removed from the model call but has already been saved in chat memory. In this answer, we will recap what an advisor is, write the redaction and usage advisor step by step, see how the chain runs and how order is decided, and register it.
1A Quick Recap of Advisors
When we call chatClient.prompt().user(text).call(), Spring AI does not go straight to the model. It builds a ChatClientRequest, which holds the Prompt (the list of messages) and a context map, and passes it through a chain of advisors. Each advisor can change the request, call the next advisor, and then look at or change the ChatClientResponse on the way back. The last link in the chain is the one that actually calls the ChatModel.
For blocking calls an advisor implements CallAdvisor, with the method adviseCall(ChatClientRequest request, CallAdvisorChain chain). For streaming calls it implements StreamAdvisor, with adviseStream returning a Flux of responses. These names are from Spring AI 1.0; the pre-1.0 milestones used different interfaces such as RequestResponseAdvisor and AroundAdvisor, so check your version.
2Writing the Redaction and Usage Advisor
Our advisor does two things around the call. Before it, it replaces every email address in the user message with a placeholder. After it, it reads the token counts and records them as a metric.
Notice that we never change the incoming request object. ChatClientRequest is immutable, so we build a copy with mutate(). The reason is that other advisors and our own logging may hold the original; copying keeps each step predictable.
The regular expression is deliberately simple. It catches ordinary addresses, which is the point of the example; a real redaction step usually combines several patterns (emails, phone numbers, card numbers) or a dedicated PII detection library. If the business needs the original text, for example for an audit, we store it in our own audit store with its own access rules, and only if policy allows it. It should never travel in the prompt.
3How Advisors Chain
The chain works like nested function calls. Each advisor's code before chain.nextCall runs on the way in, in order. Each advisor's code after chain.nextCall runs on the way out, in reverse order. So the first advisor in the chain is the outermost layer: it sees the request first and the response last.
7 messages in order. Play them, or step through them one at a time.
Lowest order runs first on the way in and last on the way out.
That is why the usage recording sits after nextCall in our advisor: the usage only exists once the inner advisors and the model have finished. And it is why our advisor, being outermost, records the usage of the whole call, including any extra text that inner advisors such as retrieval added to the prompt.
4Controlling the Order
Order is decided by getOrder(), from Spring's Ordered interface. Lower values run first on the way in. Spring AI's built-in advisors have default orders, and the memory and retrieval advisors let us set an order on their builders. For our case the rule is simple: the redaction advisor must have a lower order than every advisor that stores or sends text, so the memory advisor saves the redacted message and the retrieval advisor searches with it.
Because the exact default order values of the built-in advisors can change between versions, we do not guess them. We set the order of the memory and retrieval advisors explicitly when we build them, and we add a test that sends a message with an email and asserts that neither the stored memory nor the captured model request contains it.
5Registering the Advisor
We can register an advisor for every call made through a ChatClient, or for a single request.
The order in which we list them in defaultAdvisors does not decide the chain; getOrder() does. Listing them in the intended order is still kind to the reader. If the ChatClient also streams, we implement StreamAdvisor in the same class, apply the same redaction before calling the stream chain, and read usage from the last response in the stream, where providers usually put it.
6Follow-up questions
Where should redaction happen if memory is stored in a database?
Before the memory advisor, so the database only ever receives redacted text. If the original is needed for audit, it goes to a separate audit store with its own retention and access control, written by our code, not by the memory advisor. Redacting only at the model call would leave the raw email in the chat memory table, where it is replayed into every later prompt and kept for as long as memory is retained.
Why not redact in the controller instead?
We can, but an advisor guarantees that every call through that ChatClient is covered, including calls from code paths added later by other developers. A platform team can ship it as a default advisor so no team has to remember it.
Does the usage metric include tokens added by retrieval?
Yes, if our advisor is outermost. The usage comes from the model's response to the final prompt, which includes history and retrieved chunks added by inner advisors.
7Conclusion
In this answer, we wrote a CallAdvisor that copies the request with emails replaced, passes it on with chain.nextCall, and records prompt and completion tokens from the response metadata. We saw that advisors nest like function calls, with getOrder() deciding the position: lower values run first on the way in and last on the way out. A redaction advisor must therefore run before memory and retrieval advisors, and we register it with defaultAdvisors or per request.
Open with
I would implement CallAdvisor, and StreamAdvisor too if we stream. In adviseCall I take the ChatClientRequest, build a copy whose user message has email addresses replaced with a placeholder, using a regex or a proper PII detector, and call chain.nextCall with the copy.
Then, if they want more
When the response comes back, I read chatResponse().getMetadata().getUsage() and record prompt and completion tokens as Micrometer counters. Advisors chain like nested calls: code before nextCall runs on the way in, code after runs on the way out in reverse. getOrder() decides the position; a lower value runs first on the way in and last on the way out.
That matters here, because if the memory advisor runs before redaction, the raw email is stored in chat memory and replayed on later calls. So I give the redaction advisor a very high precedence, set the memory and retrieval advisors' order explicitly, and add a test that checks neither memory nor the model request contains the email.
I register it with defaultAdvisors on the ChatClient builder, or per request with advisors. If the original text must be kept, it goes to a separate audit store, never into the prompt. Interface names changed before 1.0, so I would check the version.
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 "Write a custom Spring AI advisor that removes email addresses from the user message...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/e2a7e55241/