Skip to main content
ahad.works

Meridian: Agentic Orchestration & Corrective RAG Server

Self-contained FastAPI and LangGraph 1.2 agent server with Corrective RAG (CRAG) query rewriting, hybrid PostgreSQL tsvector BM25 + pgvector RRF retrieval, <15ms cosine semantic caching, FastMCP tool discovery, Postgres-checkpointed HITL resume flows, guardrails, and automated RAGAS evaluation.

PythonFastAPILangGraphFastMCPpgvectorRAGASPostgreSQLDocker

Overview

Meridian is a deployable AI agent orchestration server built around FastAPI 0.136, LangGraph 1.2, FastMCP 1.27, and PostgreSQL pgvector. It combines Corrective RAG (CRAG) self-grading, hybrid lexical-plus-dense retrieval, low-latency semantic caching, deterministic input/output guardrails, human-in-the-loop checkpoints, and per-query RAGAS evaluation behind a unified HTTP and SSE API.

Live deployment: meridian.ahad.works

System Architecture

Loading diagram...

Core Subsystems

1. Corrective RAG (CRAG) State Graph

The LangGraph StateGraph executes six deterministic stages:

  1. Classify — A fast-path classifier (~2ms) separates conversational turns from corpus-grounded queries.
  2. Retrieve — Executes PostgreSQL full-text search (tsvector BM25) alongside pgvector dense similarity (all-MiniLM-L6-v2) and merges rankings with Reciprocal Rank Fusion (k = 60).
  3. Grade — Scores retrieved chunks on [0, 1]; documents below 0.7 trigger query rewriting.
  4. Rewrite — Rewrites ambiguous or under-specified queries up to MAX_CRAG_RETRIES=2 before re-retrieving.
  5. Generate — Produces citations strictly constrained to verified context chunks.
  6. Hallucination Check — Validates answer grounding against retrieved evidence prior to streaming.

2. Semantic Caching & Hybrid RRF

  • Semantic Cache: Queries with cosine similarity >= 0.92 against prior embeddings return in under 15ms, skipping retrieval and LLM inference.
  • RRF Fusion: Combines lexical precision for exact identifiers with semantic recall:
text
1RRF_score(d) = 1 / (60 + rank_bm25(d)) + 1 / (60 + rank_vector(d))

3. Protocol-Native Tooling, HITL & Guardrails

  • FastMCP Tool Discovery: Exposes tools over stdio and /v1/mcp/tools JSON-RPC (mcp>=1.27).
  • Human-in-the-Loop (HITL): High-risk tool executions pause via interrupt_before, persist thread state to PostgreSQL with Row-Level Security (RLS) multi-tenant isolation, and resume through POST /v1/chat/resume.
  • Guardrails & RAGAS Evals: Screens incoming payloads for prompt injection, SQL injection, and role hijacking; redacts emails, API keys, SSNs, phone numbers, and credit card patterns from outputs; tracks faithfulness, context precision, and answer relevancy via ragas.

API Surface

MethodEndpointPurpose
POST/v1/chatExecute agent turn (JSON or text/event-stream SSE)
POST/v1/chat/resumeResume a HITL-suspended thread after approval
GET/v1/mcp/toolsMCP JSON-RPC tool discovery endpoint
POST/v1/ingestIngest Markdown and PDF documents into pgvector + tsvector
POST/v1/eval/runTrigger automated RAGAS evaluation over Q&A datasets
GET/v1/eval/resultsFetch historical faithfulness and precision scores
GET/healthEmbedder, LangGraph, and database readiness probe