Introduction to Spring AI Recursive Advisors
Standard Spring AI advisors process requests in a single pass—ideal for simple interactions but limited for iterative tasks like retrying failed outputs or chaining multiple tool calls. Spring AI 1.1 introduces Recursive Advisors, enabling multi-pass workflows where the system loops through the advisor chain until a condition is met. This tutorial explains how to leverage this powerful feature for advanced AI applications.
Setting Up Your Project
To use Recursive Advisors, configure your Maven project with Spring Boot 3.5 and Spring AI 1.1.2:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
</dependencies>
How Recursive Advisors Work
Recursive Advisors differ from standard ones by looping through the advisor chain multiple times. Here’s how it works:
1. **Initial Request**: The request flows down the advisor chain to the LLM.
2. **Response Evaluation**: If the result doesn’t meet conditions (e.g., invalid format), the advisor triggers a new iteration.
3. **Retry Loop**: The chain restarts from the recursive advisor, re-evaluating the request until success.
This pattern is ideal for tasks like structured output validation or multi-step tool execution.
Practical Use Cases
– **Structured Output Validation**: Retry until the LLM returns a valid JSON format.
– **Multi-Step Workflows**: Chain tool calls (e.g., search + summarize) iteratively.
– **Error Recovery**: Automatically retry failed API calls with adjusted parameters.
Build Your First Recursive Advisor
1. Extend `AbstractRecursiveAdvisor` and override `shouldRetry()` to define loop conditions.
2. Use `AdvisorChain` to manage the advisor sequence.
3. Test with a simple validation rule, like ensuring the LLM response contains specific keywords.
Conclusion
Spring AI Recursive Advisors unlock advanced AI workflows by enabling iterative processing. Whether you’re handling complex validation or multi-step tasks, this feature simplifies dynamic interactions with LLMs. Ready to level up your Java apps? Start experimenting with recursive advisors today!
FAQs
- What are Spring AI Recursive Advisors and how do they work?
- Recursive Advisors loop through the advisor chain multiple times until a condition is met, enabling iterative LLM interactions.
- Can I use Recursive Advisors with any LLM provider?
- Yes—Spring AI supports OpenAI, Anthropic, and other providers via its modular architecture.
- How do I handle infinite loops in Recursive Advisors?
- Implement a maximum retry limit in your `shouldRetry()` logic to prevent endless iterations.
- What’s the difference between Recursive and Standard Advisors?
- Standard Advisors process requests once; Recursive Advisors enable multi-pass workflows for complex scenarios.
- Where can I find examples of Recursive Advisor implementations?
- Check the official Spring AI GitHub repository for sample projects and integration tests.







