Ahmad Tawil

Python · Shipped

Agentic RAG vs. Scale

A 2×2 factorial study testing whether an agentic self-correction pipeline beats a simpler one, and whether a bigger model beats better architecture, on HotpotQA.

Python

Does an agentic self-correction pipeline always beat a simpler one? Does a bigger model? This project answers both questions with a controlled 2×2 factorial experiment.

Read Paper


Experimental Design #

Four systems cross two model families with two pipeline architectures:

Naive PipelineAgentic Pipeline
GPT-5.4-miniSystem A — GoliathSystem D — Titan
Llama-3.1-8BSystem C — HermesSystem B — David

All four systems share identical embeddings, vector store, retrieval configuration, and benchmark — isolating model choice and pipeline architecture as the sole independent variables.


Results #

Evaluated on 100 questions from the HotpotQA distractor benchmark (seed = 42):

Exact-Match Accuracy #

SystemModelPipelineOverallBridgeComparison
A — GoliathGPT-5.4-miniNaive52.0%50.0%64.3%
B — DavidLlama-3.1-8BAgentic58.0%55.8%71.4%
C — HermesLlama-3.1-8BNaive65.0%64.0%71.4%
D — TitanGPT-5.4-miniAgentic58.0%55.8%71.4%

RAGAS Metrics (judge: GPT-5.4) #

MetricA — GoliathB — DavidC — HermesD — Titan
Faithfulness0.6030.7140.7330.643
Answer Relevancy0.6600.5160.5410.688
Context Precision0.6640.6880.6520.690
Context Recall0.7500.7400.7600.770

The Key Finding — Interaction Effect #

The agentic pipeline is not model-agnostic:

Naive → Agentic
GPT-5.4-mini+6.0 pp
Llama-3.1-8B−7.0 pp

13 pp interaction term. Agentic self-correction helps the more capable model but hurts the smaller one — suggesting a capability threshold for productive self-correction. All four systems achieved 100% completion by running Llama on a local GPU (NVIDIA Tesla T4 via Ollama), eliminating the rate-limit failures of earlier work.


Charts #

Exact-Match Accuracy by Question Type #

Exact Match

RAGAS Evaluation — All Four Systems #

RAGAS

2×2 Interaction: Model Scale vs Pipeline Architecture #

2x2 Interaction

Agentic Loop Behaviour — Systems B and D #

Loop Behaviour


Systems #

A — GoliathB — DavidC — HermesD — Titan
ModelGPT-5.4-miniLlama-3.1-8BLlama-3.1-8BGPT-5.4-mini
PipelineNaive RAGAgentic RAGNaive RAGAgentic RAG
OrchestrationLangChainLangGraphLangChainLangGraph
Self-correctionNoneDoc grading + query rewriting + hallucination checkNoneDoc grading + query rewriting + hallucination check

Agentic Pipeline (Systems B and D) #

Question


Retrieve (ChromaDB, top-5)


Grade Documents ──── < 50% relevant? ──── Rewrite Query ──┐
   │                                                       │
   │ ≥ 50% relevant (or max 3 retries)               (loop back)

Generate Answer


Check Hallucination ──── not grounded? ──── (regenerate, max 2×)


Answer

Benchmark #

HotpotQA — distractor setting, validation split

ParameterValue
Questions sampled100 (seed = 42)
Paragraphs ingested1,000 (100 × 10)
Gold paragraphs per question2
Distractor paragraphs per question8
Question typesBridge (86), Comparison (14)
DifficultyHard (all)

Project Structure #

agentic-rag-vs-scale/
├── config.py                        # Central configuration (all models, limits, paths)
├── requirements.txt                 # Dependencies
├── .env.example                     # API key template
├── check_apis.py                    # Test OpenAI, Groq, and Ollama backends

├── ingestion/
│   └── ingest_hotpotqa.py           # Download HotpotQA + build ChromaDB corpus

├── systems/
│   ├── llm_factory.py               # LLM backend selector (Groq / Ollama / local GPU)
│   ├── system_a_goliath.py          # Naive RAG + GPT-5.4-mini
│   ├── system_b_david.py            # Agentic RAG + Llama-3.1-8B
│   ├── system_c_hermes.py           # Naive RAG + Llama-3.1-8B
│   └── system_d_titan.py            # Agentic RAG + GPT-5.4-mini

├── evaluation/
│   ├── load_benchmark.py            # Load sampled questions
│   ├── run_tournament.py            # Run all four systems on all questions
│   ├── evaluate_ragas.py            # Score with RAGAS 0.4.x framework
│   ├── visualize_results.py         # Generate publication charts
│   └── results/                     # Output JSON, CSV, and PNG files

└── agentic-rag-arxiv/
    └── main.tex                     # LaTeX research paper

Quick Start #

1. Clone and install #

git clone https://github.com/AhmadTawil1/agentic-rag-vs-scale.git
cd agentic-rag-vs-scale
pip install -r requirements.txt

2. Set API keys #

copy .env.example .env

Edit .env:

OPENAI_API_KEY=your_openai_key
GROQ_API_KEY=your_groq_key          # for Llama via Groq (optional)
GROQ_API_KEY_2=your_backup_groq_key # backup key (optional)

To run Llama locally via Ollama instead of Groq (recommended — no rate limits):

USE_OLLAMA=true

3. Build the corpus #

Downloads HotpotQA from HuggingFace and ingests 1,000 paragraphs into ChromaDB.

python ingestion/ingest_hotpotqa.py

4. Run the tournament #

python evaluation/run_tournament.py

All four systems answer all 100 questions. Results are saved to evaluation/results/.

Llama rate limits: Systems B and C each make multiple LLM calls per question. Running on Groq's free tier will hit daily token limits. Use USE_OLLAMA=true with a local GPU (≥8 GB VRAM) to eliminate this constraint entirely.

5. Evaluate with RAGAS #

Run locally after downloading the result JSON files:

python evaluation/evaluate_ragas.py \
  --system-a evaluation/results/system_a_results_<TIMESTAMP>.json \
  --system-b evaluation/results/system_b_results_<TIMESTAMP>.json \
  --system-c evaluation/results/system_c_results_<TIMESTAMP>.json \
  --system-d evaluation/results/system_d_results_<TIMESTAMP>.json

6. Generate charts #

python evaluation/visualize_results.py \
  --system-a evaluation/results/system_a_results_<TIMESTAMP>.json \
  --system-b evaluation/results/system_b_results_<TIMESTAMP>.json \
  --system-c evaluation/results/system_c_results_<TIMESTAMP>.json \
  --system-d evaluation/results/system_d_results_<TIMESTAMP>.json \
  --comparison-csv evaluation/results/comparison.csv \
  --output-dir evaluation/results

Configuration #

Key parameters in config.py:

ParameterDefaultDescription
HOTPOTQA_SAMPLE_SIZE100Questions sampled from validation split
HOTPOTQA_SEED42Random seed for reproducibility
RETRIEVAL_TOP_K5Documents retrieved per query
MAX_RETRIEVAL_LOOPS3Max query rewrites (agentic systems)
MAX_GENERATION_RETRIES2Max regeneration attempts (agentic systems)
GOLIATH_MODELgpt-5.4-miniSystem A model
TITAN_MODELgpt-5.4-miniSystem D model
DAVID_MODELllama-3.1-8b-instantSystem B model (via Groq)
HERMES_MODELllama-3.1-8b-instantSystem C model (via Groq)
EVALUATOR_MODELgpt-5.4RAGAS judge model
EMBEDDING_MODELall-MiniLM-L6-v2Shared embedding model (local)

Paper #

The full research paper is in agentic-rag-arxiv/main.tex.

To compile (requires MiKTeX or Overleaf):

cd agentic-rag-arxiv
pdflatex main.tex
pdflatex main.tex   # second pass for cross-references

Author #

Ahmad Tawilahmadtawil.se@gmail.com


License #

This project is licensed under the MIT License — © 2026 Ahmad Tawil.