A production-grade agentic RAG system for legal contract risk analysis — multi-source, multi-agent, multi-modal query support.
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.
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.
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.
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.
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.
PDF parsed, clause boundaries detected, chunks generated with parent-child links.
Language detected → model routed → embeddings generated → stored in Qdrant with document ID scoping.
User query classified by QueryPlanner agent. Intent determines which agent branch handles it.
Semantic similarity search scoped to the active document. Top-k chunks retrieved with page metadata.
Agent constructs prompt with retrieved context only. LLM instructed to answer from provided chunks, cite page numbers, flag if insufficient context.
Synthesis agent assembles structured output: answer, citations, risk flags (if risk scan mode).
Automatically scans the full contract across 8 risk categories: liability, indemnification, termination, IP, confidentiality, dispute resolution, payment terms, governing law.
Answer specific questions about the contract. Every answer includes the source page number and clause reference — no hallucination.
Generate a structured executive summary of the contract: parties, term, key obligations, risk flags, and notable clauses.
Cross-document retrieval leakage
Qdrant collection scoping per document upload session — retrieval never crosses document boundaries.
Hallucination in legal context
System prompt instructs the LLM to answer strictly from provided chunks; responds with 'not found in document' if context is insufficient.
Bilingual contracts
Language detection at ingestion triggers model routing — multilingual E5 handles non-English content, BGE handles English.
Legal clause boundary detection
Custom chunker parses structural markers (numbered sections, 'WHEREAS', 'IN WITNESS WHEREOF') rather than using sentence or token splits.