Agentic RAG — Advanced Patterns
Advanced RAG where the model controls retrieval: self-RAG, corrective RAG, adaptive retrieval and query planning — when to let an agent decide whether and what to retrieve, in Java.
On this page
Basic RAG retrieves once with the raw question and answers. That fails on multi-hop questions, questions that need no retrieval, and cases where the first retrieval was poor. Agentic RAG lets the model control the retrieval process. This tutorial covers the main patterns and when they are worth the added cost.
Key Takeaways
- Agentic RAG lets the model decide whether, what, and how many times to retrieve.
- Self-RAG reflects on relevance and support; corrective RAG falls back when retrieval is poor.
- Query planning decomposes multi-hop questions into sequential retrievals.
- It costs extra model calls — use it only when basic RAG genuinely falls short.
Where basic RAG breaks
Basic RAG assumes every question needs exactly one retrieval with the raw wording. Real questions violate that constantly.
Self-RAG: reflect on retrieval
The model judges its own retrieval and answer, retrieving again or abstaining when needed:
public Answer selfRag(String question) {
// 1. Does this even need retrieval?
if (!retrievalDecider.needsRetrieval(question)) {
return answerer.answerDirectly(question);
}
for (int attempt = 0; attempt < MAX_RETRIEVALS; attempt++) {
List<Document> docs = retriever.retrieve(question);
// 2. Are the retrieved passages actually relevant?
List<Document> relevant = docs.stream()
.filter(d -> relevanceJudge.isRelevant(question, d))
.toList();
if (relevant.isEmpty()) {
question = queryRewriter.rephrase(question); // try a better query
continue;
}
Answer answer = answerer.answer(question, relevant);
// 3. Is the answer supported by the passages?
if (supportJudge.isSupported(answer, relevant)) {
return answer;
}
}
// Abstain rather than answer from unsupported context.
return Answer.insufficient();
}Corrective RAG: fall back when retrieval fails
When the knowledge base has gaps, corrective RAG detects poor retrieval and takes a different route:
public Answer correctiveRag(String question) {
List<Document> docs = retriever.retrieve(question);
RetrievalQuality quality = evaluator.assess(question, docs);
return switch (quality) {
// Good: answer from the knowledge base.
case STRONG -> answerer.answer(question, docs);
// Weak: supplement with a broader or web search before answering.
case AMBIGUOUS -> answerer.answer(question, merge(docs, webSearch.search(question)));
// None: do not answer from irrelevant context.
case POOR -> Answer.notFound(question);
};
}This makes RAG robust to the reality that your vector store does not contain everything — instead of confidently answering a gap from whatever weak matches came back.
Query planning for multi-hop questions
Some questions require chaining retrievals: "which of our suppliers is in a country affected by the new tariff?" needs the tariff list, then the supplier list, then a join. An agent plans this:
public Answer multiHop(String question) {
List<SubQuery> plan = planner.decompose(question);
Map<String, List<Document>> gathered = new LinkedHashMap<>();
for (SubQuery sub : plan) {
// Each sub-query can use results from earlier ones.
String resolved = resolveReferences(sub, gathered);
gathered.put(sub.id(), retriever.retrieve(resolved));
}
return synthesiser.answer(question, gathered);
}Adaptive retrieval
Not every question needs the same retrieval effort. Adaptive RAG routes by question complexity:
return switch (complexityClassifier.classify(question)) {
case SIMPLE -> answerer.answerDirectly(question); // no retrieval
case SINGLE_HOP -> basicRag(question); // one retrieval
case MULTI_HOP -> multiHop(question); // planned retrievals
};This spends retrieval budget where it is needed and saves it where it is not — cheaper on average than running the full agentic pipeline on every question.
Choosing the pattern
| Situation | Pattern |
|---|---|
| Single-retrieval questions answered well | Basic RAG |
| Need honest "I don't know" | Self-RAG |
| Knowledge base has gaps | Corrective RAG (with fallback) |
| Multi-hop questions | Query planning |
| Mixed question complexity | Adaptive routing |
Next
Frequently Asked Questions
What is agentic RAG?
What is self-RAG?
What is corrective RAG?
When should I use agentic RAG instead of basic RAG?
Related tutorials
- Agent Evaluation & TestingHow to evaluate and test AI agents: trajectory analysis, benchmarking, hallucination detection, outcome verification and human-in-the-loop evaluation — with Java patterns.
- Human-in-the-Loop (HITL) SystemsDesign human-in-the-loop AI systems in Java: approval flows for agent actions, escalation patterns, confidence thresholds and feedback loops — how to deploy autonomy without accepting unbounded risk.
- Building Autonomous Coding AgentsDesign autonomous coding agents in Java: code generation with verification, review agents that bias for precision, refactoring and test-generation agents — with the guardrails they need.
- Productionizing Agentic SystemsTake agents to production: per-run budgets and step caps, guardrails, durable execution, scaling on the JVM, cost control and the operational patterns that keep agents from causing incidents.