Spring AI's Advisors API provides a composable interception layer around ChatClient calls, similar to Servlet filters or AOP advices but scoped to AI request/response cycles. The post walks through building a logging advisor (CallAdvisor) that wraps model calls with before/after logging, and a safety advisor that short-circuits the chain without calling the model when sensitive words are detected. Key concepts covered include advisor ordering (lower getOrder() = higher precedence), the difference between CallAdvisor and StreamAdvisor interfaces, registering advisors as defaults vs. per-request, and a survey of Spring AI 2.0.0 built-in advisors for logging, memory, RAG, safety, tool calling, and output validation. Practical testing and production considerations like redacting sensitive data from logs are also discussed.

9m read timeFrom sivalabs.in
Post cover image
Table of contents
Table of ContentsIntercept AI Request and Responses using AdvisorsBuilding an advisor to log LLM request and responseSafetyCheckAdvisor: Blocking a request without calling the modelAdvisor Ordering is ImportantCallAdvisor and StreamAdvisorDefault vs Per-request AdvisorsSpring AI Built-in AdvisorsSummaryRelated PostsSpring AI : Chat MemorySpring AI : Structured OutputSpring AI : Chat with LLMs
2.5K Impressions