A cost-aware, self-verifying Retrieval-Augmented Generation backend built with FastAPI and Python. Explores difficulty-based model tier routing, hybrid dense-and-BM25 retrieval, calibrated multi-factor hallucination scoring, and bounded auto-escalation with async SQLite telemetry.
TrustRoute is a Python RAG service that routes incoming questions to small, mid, or frontier LLM tiers based on predicted query difficulty, evaluates the generated answer's hallucination risk across three verification signals, and escalates to a stronger tier when risk exceeds a safety threshold.
02. Problem & Exploration
Documented around two recurring RAG bottlenecks: unnecessary inference cost from routing simple factual queries to frontier models, and unverified answers returning unsupported claims without grounding checks.
What This Experiment Explores
Explores cheap-first model routing, combining multiple lightweight verification heuristics into a calibrated risk score, and comparing routing policies systematically via a reproducible benchmark harness.
03. Implemented Capabilities
Hybrid retrieval combining L2-normalized TF-IDF random-projection vectors and BM25Okapi keyword search via Reciprocal Rank Fusion (RRF) and a cross-reranker.
Query difficulty classifier using scikit-learn logistic regression over eight structural and lexical features to route questions across small, mid, and frontier model tiers.
Multi-factor hallucination scorer combining lexical/numeric grounding overlap, multi-sample self-consistency agreement, and entailment/contradiction checks calibrated with Platt sigmoid scaling.
Bounded auto-escalation policy (capped at one escalation per query) with async SQLAlchemy/SQLite audit logging of token cost, latency, citations, and savings against an always-frontier baseline.
HTTP model adapters for OpenAI, Anthropic, and Groq alongside an offline calibrated simulator and a four-pipeline comparative benchmark runner.
04. Technical Approach & Stack
A FastAPI application initializes the hybrid corpus index on startup and exposes query, standalone verification (/verify), dashboard telemetry, and benchmark endpoints. TrustRouteAgent orchestrates retrieval, difficulty classification, candidate generation, self-consistency sampling on cheaper tiers, composite risk scoring, conditional escalation, and async SQLite logging.
Verified Technology Stack
Python
FastAPI
Pydantic
SQLAlchemy
SQLite
scikit-learn
NumPy
BM25
Docker
05. Known Limitations & Notes
—Dense retrieval uses TF-IDF with a fixed deterministic random projection rather than a neural embedding model.
—NLI contradiction detection combines lexical overlap with domain-specific heuristic rules tailored to the bundled four-document CloudScale sample corpus.
—Without live provider API keys, generation defaults to a deterministic simulator designed for zero-cost offline testing and benchmarking.