Overview
Classic RAG is a fixed pipeline. It embeds the question, retrieves top-K chunks once, and generates an answer. Agentic RAG puts an LLM agent in control of retrieval. The agent decides whether it needs to search at all, which source to query, how to rewrite the query, whether the results are good enough, and whether to search again before answering.
The benefit is better answers for complex questions, such as comparisons, multi-step lookups, or questions spanning several systems. The cost is more LLM calls, higher and less predictable latency, and harder debugging. A good design uses classic RAG for simple questions and escalates to the agentic path only when needed.
Classic RAG is a librarian who hears your question once, fetches five books, and hands them over. Agentic RAG is a research assistant who reads the first results, realizes they need a different angle, checks the database and another shelf, and only then writes the summary.
When to use it
- Questions that need several lookups, for example comparing two policies or joining data across systems.
- Many heterogeneous sources such as docs, SQL databases, APIs, and web search.
- When single-shot retrieval often returns irrelevant chunks.
Where it shows up in interviews
Recognize it when: answering needs facts from more than one place.
- Design an AI search engine
- Design a RAG system
Recognize it when: data lives in docs, databases, and APIs.
- Design an AI agent
- Design an AI customer-support system
Where it is used in real software
Deep-research style products plan searches, read results, and iterate before writing a report.
Choose between wiki search, ticket search, and a metrics database depending on the question.
Search the repository, open files, and search again based on what they find.
Key terms
- Query rewriting
- Rephrasing or splitting the question to improve retrieval.
- Router
- A step that chooses which source or tool to query.
- Self-evaluation
- The agent judges whether retrieved context is sufficient before answering.
- Iteration limit
- A cap on retrieval rounds to bound cost and latency.
How it works, step by step
- 1Decide whether to retrieve
Simple greetings or general questions may not need retrieval at all.
- 2Plan and route
Break the question into sub-questions and choose a tool for each, such as vector search, SQL, or an API.
- 3Retrieve and grade
Run searches and score whether results are relevant and sufficient.
- 4Refine or continue
If results are weak, rewrite the query or try another source, up to an iteration limit.
- 5Synthesize with citations
Combine the gathered evidence into one answer that cites each source.
STEP 1Question "Compare our 2024 and 2025 remote-work policies". The agent plans two lookups.
Classic vs agentic
Same knowledge base, three question types.
| Question type | Classic RAG | Agentic RAG |
|---|---|---|
| Single fact lookup | Good, 1 LLM call | Good, but 2-3 calls |
| Comparison across documents | Often misses one side | Retrieves each side separately |
| Needs live data from an API | Cannot | Calls the API tool |
NOWQuestion type: Single fact lookup | Classic RAG: Good, 1 LLM call | Agentic RAG: Good, but 2-3 calls
Route simple questions to classic RAG and escalate complex ones to the agent.
Implementation
type Tool = { name: string; run: (query: string) => Promise<string[]> }; async function agenticRag(question: string, tools: Tool[], maxRounds = 3) { const evidence: string[] = []; let query = question; for (let round = 0; round < maxRounds; round++) { const decision = await llmJson<{ tool: string; query: string } | { done: true }>( `Question: ${question}\nEvidence so far:\n${evidence.join("\n")}\n` + `Tools: ${tools.map((t) => t.name).join(", ")}. Reply {"done":true} if evidence is sufficient, ` + `otherwise {"tool": name, "query": text}.`, ); if ("done" in decision) break; const tool = tools.find((t) => t.name === decision.tool); if (!tool) break; query = decision.query; evidence.push(...(await tool.run(query))); } return llmText(`Answer using only this evidence and cite it:\n${evidence.join("\n")}\n\nQuestion: ${question}`);}Complexity and performance
Each planning and grading step is a call.
Stream progress updates to keep users informed.
Trade-offs
Iteration improves complex answers but multiplies model calls. Cap rounds and route simple questions to the fast path.
A fixed pipeline is easy to test and debug. Agents can take different paths for the same question, so tracing and evaluation are essential.
Variants and related techniques
Grades retrieved chunks and falls back to another source when they are irrelevant.
Separate agents handle retrieval, verification, and writing.
Common mistakes
- Unbounded loops.
Fix: Set a maximum number of rounds, a token budget, and a timeout.
- Using the agent for every question.
Fix: Route by complexity so most traffic stays on the cheap, fast path.
- No tracing.
Fix: Log every plan, tool call, and retrieved result to debug and evaluate behavior.
Interview questions
When would you choose agentic RAG over classic RAG?
When questions need multiple lookups, several sources, or live data, and when single-shot retrieval quality is poor. For simple fact questions classic RAG is cheaper, faster, and easier to evaluate.
How do you keep agentic RAG costs under control?
Route simple questions away from the agent, cap iterations and tokens, use a small model for planning and grading, cache retrieval results, and monitor cost per request.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Add a relevance grader that triggers a second search | Medium | Corrective retrieval. |
| Design an assistant that answers across docs, SQL, and a ticketing API | Hard | Routing and iteration limits. |