Agentic RAG Foundations: Grading & Query Rewriting: overview
- Source
- From the RAG & Agents Engineering course
- By
- Sourav Banerjee
- Last updated
- 9 Nov 2026
Larkspur retrieves a refund-related passage that does not answer when payment arrives. You will grade that evidence, rewrite the query and prove the retry loop stops when the handbook has no answer. The tutorials connect conditional edges, document grading, topic checks and explicit retry budgets.
The notebooks run on a research paper (lesson 1 of T9.1, and lesson 2 of T9.1, which moves the same graph onto a managed vector index) and a fictional company's support documents (lesson 3 of T9.1). These pages put the same graphs on the Larkspur Cloud handbook from T5.1: its twelve chunks, and five golden questions that each take a different path through the loop. Can I export my data after I cancel my plan? is answered on the first retrieval, How long does a refund take? after one rewrite, What happens if my server goes down? after two, Do you offer a student discount? has no answer in the handbook, and What's the weather like today? is off topic.
In short
- A document grader is an LLM call with structured output that reads retrieved text next to the question and says whether it helps answer it. A conditional edge turns that verdict into the next node: generate, or rewrite and retrieve again.
- A retriever returns
kchunks for every query. In the lab, the student-discount question retrieves two chunks at 0.000, and a rewrite of it retrievessupportat 0.503; neither says anything about discounts, and only the grader keeps them from the model. - A rewrite loop without a counter runs until the grader says yes or LangGraph stops it. At the default recursion limit of the pinned
langgraph1.2.18 (10,007 steps), a grader that always says no costs 3,336 retrievals. - Lesson 3 of T9.1's graph allows 2 rewrites per question. On the five golden questions, that bound answers 3 at 31 LLM calls; a bound of 3 answers the same 3 at 34.
- A topic check before retrieval costs one call per question and stops off-topic ones early: the weather question costs 1 call instead of 8, and gets an off-topic reply instead of "not found".
Mental model
- A customer asks “What happens if my server goes down?”
- One structured-output call classifies it as on topic, so it goes on to retrieval.
- Top-2 retrieval returns reboot and sizing: they share the word “server” with the question.
- The grader reads each chunk against the question. Neither says what happens in an outage: 0 of 2 pass.
- No rewrite has been used yet and two are allowed, so the router sends the query to the rewriter.
- “What happens when my server is unavailable?” retrieves the same two chunks. 0 of 2 pass again.
- The last allowed rewrite names the outage and the availability guarantee. sla and incidents come back and both pass.
- The model answers from the two passing chunks. The question cost 10 LLM calls and 3 retrievals; one answered on the first pass costs 4.
Every self-correcting design in T10 is this loop with more branches. The grader decides whether the loop runs again, and the counter decides when it must stop. Without the grader the model answers from whatever came back; without the counter an unanswerable question never ends.
Interactive lab
Grade, rewrite, retry: open the file in a browser. Pick What happens if my server goes down? and lower the bound from 2 to 1: the evidence is one rewrite out of reach. Pick Do you offer a student discount? and raise the bound to 3: three more calls and no answer. Then switch the topic check off and pick the weather question.
How it connects
- Before: T5.1 supplies the handbook, the golden set and the baseline this loop is measured against. T4.3 builds LangGraph state and conditional edges, T4.4 adds checkpointers and bounded loops, T7.1 binds tools to a model, and T6.2 rewrites queries before retrieval.
- Next: T9.2 adds a web-search fallback when the grade fails (CRAG) and checks the answer itself (Self-RAG). T9.3 routes each question to a knowledge base or the web before retrieving, T9.4 hands the whole RAG pipeline to an agent as one tool, and T9.5 detects unsupported answers and learns from user feedback.
Key Takeaways
- Grade retrieved text against the question before the model reads it. A similarity score measures shared words; only reading the chunk tells you whether it answers.
- Put the verdict in the state and route on it with a conditional edge, so every path the graph takes can be logged and explained.
- Bound every retry loop with a counter in the state that resets each turn, and set the bound where the golden set stops gaining answers.
- Check the topic before retrieval when off-topic traffic is real, and watch the no-results rate it leaves behind: those questions are gaps in the handbook.