spring-projects / spring-projects/spring-ai
Avoid repeated context injection in RetrievalAugmentationAdvisor
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
Current Behavior
RetrievalAugmentationAdvisor is currently invoked on every LLM call, including intermediate calls triggered during tool execution.
In my setup, the advisor is configured with:
Query Transformers
RewriteQueryTransformer (uses LLM to rephrase queries for better retrieval)
Document Retriever
VectorStoreDocumentRetriever backed by Pinecone
Document Post-Processors
Custom NvidiaRerankingDocumentPostProcessor for reranking retrieved results
ChatClient Configuration
this.chatClient = chatClientBuilder
.defaultSystem(p -> p.text(systemPrompt)
.param(AgentEnvironment.ENVIRONMENT_INFO_KEY, AgentEnvironment.info())
.param(AgentEnvironment.AGENT_MODEL_KEY, agentModel)
.param(AgentEnvironment.AGENT_MODEL_KNOWLEDGE_CUTOFF_KEY, knowledgeCutoff)
)
.defaultTools(
FileSystemTools.builder().build(),
GrepTool.builder().build(),
GlobTool.builder().build(),
ShellTools.builder().build(),
AskUserQuestionTool.builder()
.questionHandler(new CommandLineQuestionHandler())
.answersValidation(false)
.build()
)
.defaultAdvisors(
retrievalAugmentationAdvisor,
ToolSearchToolCallAdvisor.builder()
.conversationHistoryEnabled(false)
.toolSearcher(toolSearcher)
.maxResults(3)
.build(),
MyLoggingAdvisor.builder()
.showUserText(true)
.showAssistantText(true)
.showAvailableTools(true)
.build()
)
.build();
Use Case
I am building a POC for an agentic JUnit test generation system.
The vector store contains domain-specific rules (e.g., rules related to Owner domain)
When generating tests for a specific class (e.g., OwnerController), the agent should:
Retrieve relevant domain rules
Use them to generate accurate test cases
Problem
While query rewriting, retrieval, and reranking work as expected, the issue is:
RetrievalAugmentationAdvisor is executed repeatedly for every LLM call, including tool calls.
This leads to:
Repeated query rewriting (LLM cost)
Repeated vector store retrieval (latency + cost)
Repeated reranking (extra processing)
Overall slower execution and increased cost
As a temporary workaround, I am considering implementing a custom advisor to control this behavior.
-->
Expected Behavior
It would be helpful to have a mechanism to:
Apply retrieval augmentation only once per user request / session
Avoid repeated execution during internal LLM calls (e.g., tool invocations)
Cache or reuse previously retrieved and processed context within the same interaction
Questions / Suggestions
Is there an existing workaround to prevent repeated retrieval during tool calls?
Would using a sub-agent be the recommended approach for this scenario?
Could a configuration option be introduced to:
Limit retrieval augmentation to the initial call
Or enable context reuse across subsequent calls
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at RetrievalAugmentationAdvisor and trace how it is invoked for the initial request and for tool-triggered LLM calls. Compare the advisor lifecycle with the expected request-scoped reuse behavior, then verify that retrieval, query rewriting, and reranking occur only when intended during an agent interaction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- ai, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 42/100