Articles / ragagentspythonprojects
Build an agentic RAG system in Python that runs locally, with an eval
Agentic RAG in about 200 lines of Python, with no API key. A router, a relevance grader, query rewriting and a step cap, then a 15-question eval against plain RAG.
Most agentic RAG tutorials I found start with LangGraph, a hosted model, a vector database and a web search API key. By the time the graph compiles, you have four services in the loop and no idea which one is responsible when an answer comes back wrong. They also tend to stop at "and now it works", with one example question. None of the top search results I looked at compared the agent against plain RAG on a fixed question set, or counted what the extra model calls bought.
This post builds the whole thing in one Python file with no dependencies beyond the standard library:
- a BM25 retriever written from scratch
- a router that decides whether a question needs retrieval at all
- a relevance grader that checks what came back
- a query rewriter that runs when retrieval looks poor
- a step cap so the loop always ends
- answers that cite the passage they came from
- a 15-question eval that scores plain RAG and agentic RAG on hit rate, correctness and steps used
The "LLM" is a deterministic rule-based stub, so you can run every line and get the same numbers I got. Each stub method maps to one model call, and I show the prompt that call would send. Swapping in a real model means replacing four short methods.
If you haven't decided whether you need agentic RAG at all, read RAG architectures compared first. It covers where agentic RAG sits next to naive, hybrid and GraphRAG, with latency and cost numbers. I won't repeat that here; this post is the build.
What we are building#
question
│
▼
┌─────────────┐ DIRECT ┌──────────────┐
│ router │──────────► │ answer from │──► answer
└─────────────┘ │ the model │
│ RETRIEVE └──────────────┘
▼
┌──────► BM25 search (top 2)
│ │
│ ▼
│ ┌─────────────┐
│ │ grader │── some passages relevant ──┐
│ └─────────────┘ │
│ │ none relevant │
│ ▼ │
│ step cap reached? ── yes ──────────────────────┤
│ or query unchanged? │
│ │ no ▼
│ ┌─────────────┐ ┌──────────────┐
└────────│ rewriter │ │ answer with │──► answer [doc-id]
└─────────────┘ │ citations or │ or "I don't know"
│ refuse │
└──────────────┘
Plain RAG, the baseline, is the right-hand column only: search once, put the top two passages in the prompt, answer.
The agentic version adds three decisions. The router can skip retrieval. The grader can reject what came back. The rewriter gets a second (and third) try at the search. The step cap and a repeated-query check guarantee the loop ends. This is close to the graph in the LangGraph agentic RAG tutorial, which also has a document grader and a question rewriter, and to the evaluator idea in Corrective RAG. Here every node is a plain function you can read.
Setup#
mkdir agentic-rag && cd agentic-rag
python3 -m venv .venv && source .venv/bin/activate
# nothing to pip install: standard library only
python3 agentic_rag.py
I ran everything below with python3 on macOS. The whole file is 217 lines, shown in five parts in order. Paste them into one agentic_rag.py.
Part 1: a corpus and a tokenizer#
The corpus is a made-up company handbook: 12 short policy passages. It is small on purpose, so you can read every passage and check every answer yourself. Real chunks are longer, and I come back to what that changes in the cost section.
"""Agentic RAG vs plain RAG, local, no API key. The 'LLM' is a rule-based stub."""
import math, re
from collections import Counter
# ---------- 1. corpus: a made-up company handbook ----------
DOCS = {
"leave": "Employees get 24 days of paid leave per calendar year. Unused leave up to 8 days "
"carries over to the next year. Leave requests go through the HR portal at least 5 working days ahead.",
"remote": "Staff may work from home up to 3 days a week. Fully remote arrangements need director "
"approval. The company pays a home office allowance of 15,000 rupees once every two years.",
"expenses": "Expense claims must be filed within 30 days of the purchase. Claims above 10,000 rupees "
"need approval from the department head. Reimbursement is paid with the next salary cycle.",
"travel": "Book flights through the travel desk. Economy class is the default for flights under 6 hours. "
"Hotel spend is capped at 7,000 rupees per night in metro cities.",
"laptop": "New hires receive a laptop on day one. Lost or stolen laptops must be reported to IT security "
"within 24 hours. Laptops are refreshed every 4 years.",
"oncall": "The platform team runs a weekly on-call rotation. On-call engineers get a stipend of 4,000 "
"rupees per week. Pages must be acknowledged within 15 minutes.",
"incident": "Severity 1 incidents need a status page update within 30 minutes. A written postmortem "
"is due within 5 business days of a severity 1 incident.",
"security": "Passwords must be at least 14 characters long. Multi-factor authentication is mandatory "
"for email and the VPN. Forward phishing emails to the security mailbox.",
"parental": "Birth mothers get 26 weeks of paid maternity leave. Other parents get 4 weeks of paid "
"parental leave, usable within the first year.",
"learning": "Each employee has a learning budget of 40,000 rupees per year for courses, books and "
"conferences. Unused learning budget does not roll over.",
"health": "Medical insurance covers the employee, spouse, two children and two parents. The sum "
"insured is 5 lakh rupees per family per year.",
"notice": "The notice period is 60 days for all permanent employees. It can be shortened if the "
"manager agrees and the leave balance is adjusted.",
}
STOP = set("a an the is are was be of to for in on at by with and or i my me we you it its "
"do does how what when who which can much many get must per each after our "
"there this that from up as if".split())
def tokens(text):
out = []
for w in re.findall(r"[a-z0-9,]+", text.lower()):
w = w.strip(",")
if not w or w in STOP:
continue
if len(w) > 3 and w.endswith("s") and not w.endswith("ss"):
w = w[:-1] # crude plural strip
if len(w) > 5 and w.endswith("ed"):
w = w[:-2] # reported -> report
out.append(w)
return out
The tokenizer lowercases, drops stopwords and strips a trailing "s" and "ed". That last rule exists because the first run failed "How soon must I report a missing notebook?": the passage says "reported", and without the rule "report" never matched. Your tokenizer is part of your retriever, and small choices like this move the hit rate.
Part 2: BM25 from scratch#
BM25 scores a document by summing, over the query terms it contains, the term's inverse document frequency multiplied by a saturating term-frequency factor that also normalises for document length. The formula and its history are in Robertson and Zaragoza's review of the probabilistic relevance framework.
# ---------- 2. BM25 from scratch ----------
class BM25:
def __init__(self, docs, k1=1.5, b=0.75):
self.ids = list(docs)
self.toks = {d: tokens(t) for d, t in docs.items()}
self.k1, self.b = k1, b
self.avgdl = sum(len(t) for t in self.toks.values()) / len(self.toks)
df = Counter(w for t in self.toks.values() for w in set(t))
n = len(docs)
self.idf = {w: math.log(1 + (n - f + 0.5) / (f + 0.5)) for w, f in df.items()}
def search(self, query, k=2):
q = tokens(query)
scores = {}
for d in self.ids:
tf, dl, s = Counter(self.toks[d]), len(self.toks[d]), 0.0
for w in q:
if w in tf:
s += self.idf[w] * tf[w] * (self.k1 + 1) / (
tf[w] + self.k1 * (1 - self.b + self.b * dl / self.avgdl))
scores[d] = s
ranked = sorted(self.ids, key=lambda d: -scores[d])
return [(d, round(scores[d], 2)) for d in ranked[:k] if scores[d] > 0]
k1 controls how fast repeated terms stop adding score, and b controls how much long documents are penalised. Lucene's BM25Similarity defaults to k1 = 1.2 and b = 0.75; I used 1.5; rerunning the eval with 1.2 prints the same numbers on every line. The log(1 + ...) form keeps IDF positive even for a term that appears in most documents.
Note the if scores[d] > 0 filter: when no query term appears anywhere, search returns nothing. A dense retriever never does that. It always returns its k nearest neighbours, however far away. That difference shows up in the eval.
Part 3: the "LLM" stub#
This is the part to replace when you move to a real model. The stub has four jobs, and each method counts as one model call.
# ---------- 3. the "LLM": a deterministic stub with four jobs ----------
# Swap each method for one model call. The prompt each call would send is in PROMPTS.
PROMPTS = {
"route": "Does answering this need the company handbook? Reply RETRIEVE or DIRECT.\nQ: {q}",
"grade": "Question: {q}\nPassage: {p}\nDoes the passage contain the answer? Reply YES or NO.",
"rewrite": "The search '{q}' found nothing useful. Rewrite it using words a policy document would use.",
"answer": "Answer only from the passages and cite [id] after each claim. "
"If they do not contain the answer, say I don't know.\n{passages}\nQ: {q}",
}
# What a real model brings from general knowledge: synonyms and paraphrases.
GLOSSARY = {"wfh": "work from home", "lodging": "hotel night", "notebook": "laptop",
"missing": "lost", "paternity": "parental", "training": "learning",
"sign": "approval", "resigning": "notice period", "gym": "health wellness",
"stock": "equity"}
class StubLLM:
def __init__(self):
self.calls = 0
def route(self, q):
self.calls += 1
return "direct" if re.search(r"\d+\s*percent of\s*[\d,]+", q.lower()) else "retrieve"
def direct_answer(self, q):
self.calls += 1
pct, base = re.search(r"(\d+)\s*percent of\s*([\d,]+)", q.lower()).groups()
return f"{int(pct) * int(base.replace(',', '')) / 100:g}"
def grade(self, q, passages): # one batched call grades all passages
self.calls += 1
qt = set(tokens(q))
keep = []
for d, _ in passages:
overlap = len(qt & set(tokens(DOCS[d]))) / max(len(qt), 1)
if overlap >= 0.5:
keep.append(d)
return keep
def rewrite(self, q):
self.calls += 1
words = re.findall(r"[a-z]+", q.lower())
extra = [GLOSSARY[w] for w in words if w in GLOSSARY and GLOSSARY[w] not in q.lower()]
return q + " " + " ".join(extra) if extra else q # unchanged = no new idea
def answer(self, q, doc_ids):
self.calls += 1
if not doc_ids:
return "I don't know: the handbook does not cover this."
qt = set(tokens(q))
best = max(((s, d) for d in doc_ids for s in re.split(r"(?<=\.) ", DOCS[d])),
key=lambda sd: len(qt & set(tokens(sd[0]))))
return f"{best[0]} [{best[1]}]"
What each method stands in for:
| Stub method | Real model call | What the stub does instead |
|---|---|---|
route |
Classify the question as RETRIEVE or DIRECT | Regex: "N percent of M" is direct, everything else retrieves |
grade |
One batched call: "does this passage contain the answer?" per passage | Keeps a passage if it shares at least half of the query's content words |
rewrite |
Rephrase the query in the document's vocabulary | Appends synonyms from GLOSSARY |
answer |
Answer from passages with [id] citations, or say "I don't know" |
Picks the sentence with the most word overlap and cites its passage |
The GLOSSARY is the stub's biggest cheat, and I'd rather say so plainly. It holds exactly the synonyms this corpus needs ("paternity" to "parental", "WFH" to "work from home"). A real model brings those from general knowledge, but it won't always pick the word your documents use. So the rewrite numbers below are an upper bound on what rewriting can do, not a measurement of what a model will do.
To swap in a real model, keep the method signatures and replace each body with a call that fills the matching entry in PROMPTS. Parse the reply strictly: route must return exactly "direct" or "retrieve", grade must return a list of doc ids, and anything unparseable should count as "not relevant". Keep self.calls += 1 and add token counts next to it; you'll want them for the cost check later. A local model served on your machine works behind those four methods as well as a hosted one, and nothing else in the file changes.
Part 4: plain RAG and agentic RAG#
INDEX = BM25(DOCS)
# ---------- 4. plain RAG: retrieve once, answer always ----------
def plain_rag(q, k=2):
llm = StubLLM()
hits = [d for d, _ in INDEX.search(q, k)]
ans = llm.answer(q, hits) # top-k goes straight into the prompt
return {"answer": ans, "docs": hits, "model_calls": llm.calls, "retrievals": 1}
# ---------- 5. agentic RAG: route, grade, rewrite, cap ----------
def agentic_rag(q, k=2, max_retrievals=3, trace=False):
llm = StubLLM()
if llm.route(q) == "direct":
return {"answer": llm.direct_answer(q), "docs": [], "model_calls": llm.calls, "retrievals": 0}
query, retrievals, seen_queries = q, 0, set()
relevant = []
while retrievals < max_retrievals:
if query in seen_queries: # rewrite made no progress: stop early
break
seen_queries.add(query)
hits = INDEX.search(query, k)
retrievals += 1
relevant = llm.grade(query, hits)
if trace:
print(f" round {retrievals}: query={query!r} hits={hits} relevant={relevant}")
if relevant or retrievals == max_retrievals:
break
query = llm.rewrite(query)
ans = llm.answer(query, relevant)
return {"answer": ans, "docs": relevant, "model_calls": llm.calls, "retrievals": retrievals}
Three details in agentic_rag decide whether the loop behaves:
- The cap is on retrievals, not on model calls.
max_retrievals=3means at most three searches. Model calls follow from that: 1 route, up to 3 grades, up to 2 rewrites, 1 answer, so at most 7 calls for any question. - No rewrite after the last retrieval. My first version called
rewriteand then exited the loop because the cap was hit, paying for a call whose output nobody used. With cap = 1 that wasted call happened on every failed question: the eval showed 50 model calls before the fix and 44 after. - A rewrite that changes nothing ends the loop. My first rewriter appended the glossary words again on every round, producing "lost laptop lost laptop". The query string changed, so a simple "same query" check never fired, and the loop spent its whole budget. Now
rewriteonly adds new words and returns the query unchanged when it has no new idea, and theseen_queriescheck stops the loop. With a real model you'll want a fuzzier check (normalised text, or embedding similarity above a threshold), because models rarely repeat themselves word for word.
Here is the trace for the question that shows the loop working:
Trace for one question:
round 1: query='How long is paternity leave?' hits=[('security', 2.31), ('leave', 1.99)] relevant=[]
round 2: query='How long is paternity leave? parental' hits=[('parental', 4.1), ('security', 2.31)] relevant=['parental']
Other parents get 4 weeks of paid parental leave, usable within the first year. [parental]
Round 1 is a nice example of how BM25 goes wrong. "Long" matches "14 characters long" in the password policy, and "leave" matches the annual leave passage, which repeats the word three times. Plain RAG answers from those two and returns the password rule. The grader rejects both, the rewriter adds "parental", and round 2 finds the right passage.
Part 5: the eval#
Fifteen questions: twelve with an answer in a known passage, one arithmetic question that needs no retrieval, and two with no answer in the handbook. The correctness check is deterministic. An answer is correct if it contains the required phrase and cites the expected passage. For the two unanswerable questions, it's correct only if it refuses.
# ---------- 6. eval set ----------
# (question, expected doc or None, required substring in answer; "REFUSE" = must say I don't know)
EVAL = [
("How many days of paid leave do employees get each year?", "leave", "24 days"),
("How many WFH days are allowed per week?", "remote", "3 days"),
("What is the cap on lodging spend?", "travel", "7,000"),
("How soon must I report a missing notebook?", "laptop", "24 hours"),
("Who has to sign off on a claim above 10,000 rupees?", "expenses", "department head"),
("What stipend do on-call engineers get?", "oncall", "4,000"),
("How long is paternity leave?", "parental", "4 weeks"),
("When is the postmortem due after a severity 1 incident?", "incident", "5 business days"),
("What is the minimum password length?", "security", "14 characters"),
("How much can I spend on training each year?", "learning", "40,000"),
("Are my parents covered by medical insurance?", "health", "two parents"),
("What is the notice period when resigning?", "notice", "60 days"),
("What is 18 percent of 2,500?", None, "450"),
("What is the stock option vesting schedule?", None, "REFUSE"),
("Does the company pay for a gym membership?", None, "REFUSE"),
]
def correct(result, doc, want):
a = result["answer"]
if want == "REFUSE":
return a.startswith("I don't know")
if doc is None:
return want in a
return want in a and f"[{doc}]" in a
def run(name, fn):
hit = n_hit = ok = calls = retr = 0
rows = []
for q, doc, want in EVAL:
r = fn(q)
if doc:
n_hit += 1
hit += doc in r["docs"]
c = correct(r, doc, want)
ok += c
calls += r["model_calls"]
retr += r["retrievals"]
rows.append((q, c, r))
n = len(EVAL)
print(f"{name:9s} hit rate {hit}/{n_hit} correct {ok}/{n} "
f"model calls {calls} ({calls / n:.2f}/q) retrievals {retr} ({retr / n:.2f}/q)")
return rows
if __name__ == "__main__":
print("Trace for one question:")
print(" ", agentic_rag("How long is paternity leave?", trace=True)["answer"])
print()
plain = run("plain", plain_rag)
agent = run("agentic", agentic_rag)
print("\nPer question (P = plain, A = agentic; calls/retrievals for agentic):")
for (q, cp, rp), (_, ca, ra) in zip(plain, agent):
print(f" P:{'ok ' if cp else 'BAD'} A:{'ok ' if ca else 'BAD'} "
f"{ra['model_calls']}c/{ra['retrievals']}r {q}")
print("\nCap sweep (agentic):")
for cap in (1, 2, 3):
run(f"cap={cap}", lambda q, c=cap: agentic_rag(q, max_retrievals=c))
Hit rate means "the expected passage was among the passages the answer step saw". For plain RAG that's the top two from BM25. For agentic RAG it's only the passages that survived the grader, because the rest never reach the prompt.
The results#
This is the full output of python3 agentic_rag.py, as printed:
Trace for one question:
round 1: query='How long is paternity leave?' hits=[('security', 2.31), ('leave', 1.99)] relevant=[]
round 2: query='How long is paternity leave? parental' hits=[('parental', 4.1), ('security', 2.31)] relevant=['parental']
Other parents get 4 weeks of paid parental leave, usable within the first year. [parental]
plain hit rate 10/12 correct 11/15 model calls 15 (1.00/q) retrievals 15 (1.00/q)
agentic hit rate 11/12 correct 13/15 model calls 56 (3.73/q) retrievals 19 (1.27/q)
Per question (P = plain, A = agentic; calls/retrievals for agentic):
P:ok A:ok 3c/1r How many days of paid leave do employees get each year?
P:ok A:ok 3c/1r How many WFH days are allowed per week?
P:ok A:ok 5c/2r What is the cap on lodging spend?
P:ok A:ok 5c/2r How soon must I report a missing notebook?
P:ok A:ok 3c/1r Who has to sign off on a claim above 10,000 rupees?
P:ok A:ok 3c/1r What stipend do on-call engineers get?
P:BAD A:ok 5c/2r How long is paternity leave?
P:ok A:ok 3c/1r When is the postmortem due after a severity 1 incident?
P:ok A:BAD 4c/1r What is the minimum password length?
P:BAD A:ok 5c/2r How much can I spend on training each year?
P:ok A:ok 3c/1r Are my parents covered by medical insurance?
P:ok A:ok 3c/1r What is the notice period when resigning?
P:BAD A:ok 2c/0r What is 18 percent of 2,500?
P:ok A:ok 6c/2r What is the stock option vesting schedule?
P:BAD A:BAD 3c/1r Does the company pay for a gym membership?
Cap sweep (agentic):
cap=1 hit rate 7/12 correct 9/15 model calls 44 (2.93/q) retrievals 14 (0.93/q)
cap=2 hit rate 11/12 correct 13/15 model calls 55 (3.67/q) retrievals 19 (1.27/q)
cap=3 hit rate 11/12 correct 13/15 model calls 56 (3.73/q) retrievals 19 (1.27/q)
Plain RAG: 11 of 15 correct, the right passage retrieved for 10 of 12, and 1 model call and 1 retrieval per question. Agentic RAG: 13 of 15 correct, the right passage kept for 11 of 12, and 3.73 model calls and 1.27 retrievals per question.
That's two more correct answers for 3.7 times the model calls. Look at where the gains and losses came from, because the headline hides it.
Where agentic RAG won. Paternity leave and training budget are vocabulary mismatches: the user's word isn't in the document. The grader caught the bad retrieval and the rewriter fixed it. The arithmetic question went to the router's DIRECT path; plain RAG retrieved two unrelated passages and answered from them.
Where agentic RAG lost. "What is the minimum password length?" Plain RAG got this right. The grader rejected the correct passage because only one of three query words ("password") appears in it; the passage says "at least 14 characters", not "minimum length". The rewriter had no synonym to add, so the agent refused. The grader is a new place for things to break, and a false negative there turns a right answer into "I don't know". An LLM grader handles this paraphrase far better than word overlap, but it has its own false-negative rate, and you should measure it on your data rather than assume it is zero.
Where both failed. "Does the company pay for a gym membership?" BM25 found "The company pays a home office allowance", and the overlap grader passed it because "company" and "pay" match. Both systems confidently answered a question about gyms with a fact about home offices. This is the failure that matters most in production: the passage is on the same topic, so it looks like an answer. The grade prompt asks "does the passage contain the answer?", not "is it related?", for this reason.
Where plain RAG looked better than it is. Plain RAG "correctly" refused the stock option question, but only because no query word appears anywhere in the corpus, so BM25 returned nothing. With a dense retriever it would have received two nearest neighbours and answered from them. Don't credit that refusal to the pipeline.
The cap sweep. With cap = 1 the agent is worse than plain RAG: 9 correct against 11, with 2.93 calls per question. One round with a grader and no second try can only remove passages. Cap = 2 gets all the benefit. Cap = 3 adds one call (a rewrite that found nothing new on the stock option question) and no correct answers. On this set, every recovery happened on the second retrieval. On your data, plot the same sweep and set the cap where the curve flattens.
What the extra calls cost#
The stub has no tokens, so here is an estimate with the reasoning shown. Assume real chunks of about 400 tokens, k = 2, and about 150 tokens of instructions per prompt.
- Plain RAG: one answer call with two chunks, about 2 × 400 + 150 = 950 input tokens.
- Agentic RAG, from the eval's own call counts across 15 questions: 15 route calls (about 100 tokens each), 19 grade calls (each sees two chunks, about 950), 7 rewrite calls (about 120), 14 answer calls (about 950), and one direct answer (small enough to ignore here).
That's (15 × 100 + 19 × 950 + 7 × 120 + 14 × 950) / 15 ≈ 2,250 input tokens per question, about 2.4 times plain RAG. The call count matters more for latency than the tokens: these calls run in sequence, so a question that needs a rewrite pays for five round trips instead of one. If each small call takes half a second to a second, that's an estimate of 2.5 to 5 seconds against 0.5 to 1. Measure your own numbers; my LLM cost and latency tracker is a small decorator that records both per call.
Two cheap ways to claw this back. Grade with a small model, since a yes/no relevance call doesn't need your best one. And skip the router when nearly every question needs retrieval: in this eval it sent one question in fifteen to the direct path, so 14 of 15 route calls changed nothing.
When agentic RAG is not worth it#
Agentic RAG paid off here because I wrote questions with vocabulary mismatches. Whether it pays off for you depends on how often your users' words differ from your documents' words, and how costly a wrong answer is compared with a slow one. Some cases where I would not build it:
- Most questions are lookups that plain retrieval already finds. Ten of the fifteen questions here were right in both systems. For those ten, the agent spent 37 model calls and plain RAG spent 10, for the same answers.
- Hybrid search would fix the misses. Most vocabulary mismatches are what dense or hybrid retrieval exists for. Adding embeddings and a reranker is one fixed step; a rewrite loop is a variable number of model calls. Try that first; the trade-off is laid out in RAG architectures compared.
- You have a latency budget. The agent's response time depends on how many rounds it takes, so p95 latency is set by the questions that hit the cap.
- You can't evaluate it. If you have no labelled question set, you can't tell whether the grader's false negatives cost more than the rewriter's recoveries. In this eval the margin was two questions out of fifteen, and one bad grader rule cancelled half of it.
The case where I would build it: questions that need a lookup the first search can't predict, a corpus split across sources where the router picks one, or a domain where "I don't know" is much better than a confident wrong answer and a grader buys you that refusal.
Taking it further#
- Swap BM25 for hybrid retrieval and rerun the eval. If plain RAG's misses disappear, you may not need the loop. The RAG cheat sheet covers hybrid search and reranking settings.
- Replace the stub with a real model, one method at a time, and rerun after each. Swapping the grader first tells you its false-negative and false-positive rates against the overlap rule.
- Add a groundedness check after the answer, the idea Self-RAG builds into the model itself: does every cited sentence appear in the cited passage? With extractive answers like these it is a string match.
- Grow the eval to 50 questions from real user logs before trusting any percentage. With 15 questions, one question is almost 7 points.
- Track the trajectory, not only the answer: steps used, cap-hit rate, repeated queries. My post on evaluating an AI agent in Python has eval code for exactly that. For a citing bot over a real document, see the PDF question-answering bot.
- Route by question complexity instead of by a regex. Adaptive-RAG trains a small classifier to choose between no retrieval, single-step and multi-step retrieval, which is the router in this post done properly.
Sources#
- Corrective Retrieval Augmented Generation (CRAG), Yan et al., 2024: a retrieval evaluator that grades documents and triggers corrective actions, the basis for the grader here.
- Self-RAG, Asai et al., 2023: a model trained to decide when to retrieve and to critique its own output.
- Adaptive-RAG, Jeong et al., 2024: routing questions by predicted complexity with a small trained classifier.
- ReAct, Yao et al., 2022: interleaving reasoning and actions such as search, the general loop that agentic RAG specialises.
- LangGraph agentic RAG tutorial: the same grade-and-rewrite graph built with LangGraph.
- The Probabilistic Relevance Framework: BM25 and Beyond, Robertson and Zaragoza: the BM25 formula.
- Lucene BM25Similarity: default parameters k1 = 1.2, b = 0.75.