Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-025: Corrective RAG — Retrieval Grading & Web-Search Routing

Context

The plain RAG pipeline (ADR-007) has a silent failure mode: when the uploaded document simply does not contain the answer, cosine ranking still returns the "least irrelevant" three chunks, and the model must choose between refusing and hallucinating around thin context. Both outcomes are wrong for a showcase whose entire point is transparent, honest grounding. The demo should instead demonstrate retrieval judgement: recognize when the document can answer, recognize when it cannot, and visibly route to a better source rather than fabricate — the pattern published as Corrective RAG (CRAG).

Decision

Extend the Q&A pipeline with an evaluation-and-routing stage between retrieval and generation; retrieval itself (chunking, batched embeddings, cosine top-3, two-tier caching) is exactly ADR-007.

1. Retrieval Quality Grader: a low-temperature Gemini call with a strict structured schema — { isSufficient, reason } — judges whether the top-3 passages contain direct, sufficient information for the question.
2. Sufficient → generation grounded strictly on the document passages, as before.
3. Insufficient → the query routes to Gemini's native googleSearch grounding tool; returned grounding chunks become the cited sources (flagged isWeb), and the system prompt requires the answer to open by disclosing that the document lacked the information — ADR-013 transparency applied to routing.
4. Observable pipeline: the SSE stage vocabulary (ADR-015) gains evaluate and search events, so the UI shows the grader's verdict and routing live.
5. Fail-conservative posture: a grader failure defaults to document grounding (never an unrequested web call); a search failure falls back to labeled model knowledge. Neither aborts the request.

Consequences

Benefits:
* The silent failure mode is gone: off-document questions produce a disclosed, web-grounded answer instead of a hallucination or an unhelpful refusal.
* Provenance is always explicit: every source is labeled document-or-web in the UI, and web-routed answers say so in their first sentence.
* The showcase got smarter: grading and routing are visible pipeline stages, demonstrating agentic RAG rather than similarity search alone.

Trade-offs:
* One extra Gemini call per question (the grader). The request guards don't count it separately: the per-IP limit (30 requests / 15 min) and the site-wide daily AI cap (300/day) both count HTTP requests, so a question costs one unit either way; the price is model spend and latency. The Vertex quota override is disabled because the eu endpoint has no regional quota bucket. Embedding costs remain cache-absorbed.
* The strict document boundary is now conditional: insufficient retrievals deliberately leave the document context for public web search — only the user's question is sent, never document content.
* Grader quality bounds routing quality: an overconfident verdict reproduces the old failure mode for that question.

Alternatives considered

1. Plain RAG (status quo): Rejected — silent hallucination on off-document questions is the exact behavior a grounding showcase must not exhibit.
2. Always-hybrid (document + web search on every question): Rejected — doubles cost per question and dilutes the claim that answers are grounded in your document unless stated otherwise.
3. Self-RAG / iterative multi-hop retrieval: Rejected — multiple grading-and-rewrite loops multiply latency and quota use for marginal gain on single-document Q&A.
4. Heuristic sufficiency check (similarity-score threshold): Rejected — cosine scores measure closeness, not answerability; judging sufficiency is a language task.

Architecture Decision Records