Back to Systems
Agentic RAG System

ClauseGuard

A production-grade agentic RAG system for legal contract risk analysis — multi-source, multi-agent, multi-modal query support.

Problem

Legal teams and businesses dealing with contracts face a slow, manual process: reading through dozens of pages to identify risk clauses, check compliance, and answer specific questions about contract terms. This doesn't scale.

The core challenge: contracts are long, dense, and domain-specific. Standard keyword search misses contextual meaning. Generic LLMs hallucinate clauses that don't exist. A production system needs grounded retrieval — only using facts present in the actual document.

Solution

ClauseGuard is a multi-agent agentic RAG system where a LangGraph orchestrator routes user queries through specialized agents — risk scanner, Q&A agent, and summarizer — each backed by a document-scoped vector retrieval layer. Every answer is grounded in retrieved document chunks with page-level citations.

Architecture

Agent Pipeline
QueryPlannerRetrievalRiskAnalysisSynthesis

The QueryPlanner classifies intent and routes to the appropriate agent branch. Retrieval fetches scoped chunks. RiskAnalysis maps content to risk categories. Synthesis assembles the final grounded response.

Chunking Strategy

Clause-aware smart chunking with a parent-child hierarchy. Parent chunks preserve full clause context; child chunks enable precise retrieval. Chunk boundaries follow legal structure (sections, clauses, subclauses) rather than fixed token counts.

Embedding Architecture

Language-based model routing: BGE embeddings for English contracts, multilingual E5 for bilingual/non-English content. Both stored in Qdrant Cloud with document-scoped collection isolation — no cross-document retrieval leakage.

RAG Pipeline

01
Document ingestion

PDF parsed, clause boundaries detected, chunks generated with parent-child links.

02
Embedding & indexing

Language detected → model routed → embeddings generated → stored in Qdrant with document ID scoping.

03
Query processing

User query classified by QueryPlanner agent. Intent determines which agent branch handles it.

04
Retrieval

Semantic similarity search scoped to the active document. Top-k chunks retrieved with page metadata.

05
Generation

Agent constructs prompt with retrieved context only. LLM instructed to answer from provided chunks, cite page numbers, flag if insufficient context.

06
Response synthesis

Synthesis agent assembles structured output: answer, citations, risk flags (if risk scan mode).

Query Modes

Risk Scan

Automatically scans the full contract across 8 risk categories: liability, indemnification, termination, IP, confidentiality, dispute resolution, payment terms, governing law.

Q&A with Citations

Answer specific questions about the contract. Every answer includes the source page number and clause reference — no hallucination.

Contract Summary

Generate a structured executive summary of the contract: parties, term, key obligations, risk flags, and notable clauses.

Challenges

Challenge

Cross-document retrieval leakage

Solution

Qdrant collection scoping per document upload session — retrieval never crosses document boundaries.

Challenge

Hallucination in legal context

Solution

System prompt instructs the LLM to answer strictly from provided chunks; responds with 'not found in document' if context is insufficient.

Challenge

Bilingual contracts

Solution

Language detection at ingestion triggers model routing — multilingual E5 handles non-English content, BGE handles English.

Challenge

Legal clause boundary detection

Solution

Custom chunker parses structural markers (numbered sections, 'WHEREAS', 'IN WITNESS WHEREOF') rather than using sentence or token splits.

Tech Stack

Agent Orchestration
LangGraphLangChain
Vector Database
Qdrant Cloud
Embeddings
BGE (English)Multilingual E5 (HuggingFace)
Backend
PythonFastAPI
Frontend
Next.jsTailwind CSS
Deployment
Render (API)Vercel (UI)HuggingFace Inference API

Future Improvements

  • Reranker layer (Cohere / cross-encoder) for improved retrieval precision
  • Streaming responses via Server-Sent Events for long risk scan outputs
  • Multi-document comparison mode — diff two contracts clause by clause
  • Fine-tuned embedding model on legal domain corpus
  • User annotation layer — mark, flag, and export clauses