Standard single-pass RAG breaks down when user queries are underspecified or when retrieval pulls noisy chunks. Rather than passing low-confidence context straight to a generator—and letting the model fill gaps from parametric memory—a Corrective RAG (CRAG) loop grades retrieved chunks first and rewrites weak queries before generation.
In Meridian (meridian.ahad.works), this pattern runs inside a deployable FastAPI 0.136 + LangGraph 1.2 + PostgreSQL pgvector server with semantic caching, input/output guardrails, FastMCP tool discovery, and automated RAGAS evaluation.
End-to-End Request Lifecycle
Loading diagram...
1. Hybrid Retrieval in PostgreSQL (tsvector + pgvector + RRF)
Operating both lexical and vector search inside PostgreSQL eliminates the synchronization drift of maintaining separate search clusters. Every ingested Markdown or PDF chunk stores a tsvector column for BM25-style keyword ranking alongside a pgvector embedding from all-MiniLM-L6-v2.
Rankings from both lists are merged via Reciprocal Rank Fusion (k = 60):
1RRF_score(d) = 1 / (60 + rank_bm25(d)) + 1 / (60 + rank_vector(d))
Multi-tenant isolation is enforced at the database layer through PostgreSQL Row-Level Security (RLS) so tenant boundaries hold even if application filters evolve.
2. Sub-15ms Cosine Semantic Caching
Repeated operational queries rarely share identical wording. Before invoking the LangGraph pipeline, Meridian embeds the normalized query and checks the cache at cosine similarity >= 0.92. Cache hits bypass retrieval and LLM inference entirely, returning grounded responses in under 15ms while still passing through output PII redaction.
3. Corrective Loop, Guardrails, and HITL Checkpoints
- CRAG Grading & Rewriting: If retrieved documents score below
0.7, the graph rewrites the query (capped at MAX_CRAG_RETRIES=2) instead of generating from irrelevant context.
- Deterministic Input/Output Guardrails: Incoming prompts are screened for prompt-injection, SQL-injection, and role-hijacking patterns before graph entry; outgoing text redacts emails, API keys, SSNs, phone numbers, and credit card numbers.
- Protocol-Native Tools & HITL:
FastMCP (mcp>=1.27) exposes tools over stdio and /v1/mcp/tools JSON-RPC. High-risk tool calls suspend via interrupt_before, persist thread state to PostgreSQL, and resume via POST /v1/chat/resume.
4. Continuous RAGAS Evaluation
Rather than relying on manual spot checks, Meridian ships /v1/eval/run and a CLI runner (python -m meridian eval --dataset examples/qa_pairs.json) backed by ragas to measure Faithfulness, Context Precision, and Answer Relevancy across regression suites and persist historical scores at /v1/eval/results.