How to Build RAG for Contract Q&A

For Legal operations teams · Based on Jangid RAG Zero-to-Hero Pipeline

// TL;DR

Legal operations teams can use the Jangid RAG Zero-to-Hero Pipeline to answer questions like 'What are the termination clauses?' across long contracts without feeding entire documents to an LLM. Paragraph-aware chunking with overlap preserves clause meaning, TF-IDF and FAISS retrieve the right passages, and a precise 'answer only from context' prompt prevents hallucinated legal advice. Advanced RAG's query rewriting and reranking filter boilerplate so only the relevant clauses reach the model. Use it when accuracy, traceability, and avoiding fabricated terms are non-negotiable.

Why is pasting a whole contract into an LLM the wrong approach?

Feeding a 50-page contract directly into a prompt is called 'the worst way to do RAG.' It wastes tokens, buries the answer in noise, and often exceeds context limits. Worse, the LLM may hallucinate clauses that don't exist — a catastrophic risk in legal work. The Jangid RAG Zero-to-Hero Pipeline retrieves only the relevant chunks, so the model sees the termination language and nothing else.

How do you chunk a contract without breaking clauses?

Contracts are dense and clause-driven, so chunking strategy matters. Start by splitting on newline characters to get natural paragraphs and sub-clauses, then merge them into chunks of around 200 characters with a 50-character overlap. The overlap carries the end of one clause into the next, so a retriever never sees a fragment like 'subject to the conditions' without knowing which conditions. Critically, apply word-boundary correction: after computing the overlap start index, walk backward to the nearest space so you never cut 'public' into 'LIC' and corrupt your vectors.

How do you make sure the answer only cites real contract language?

This is where prompt precision matters. The answer prompt must instruct the LLM: 'Answer the question using only the provided context. If the answer is not present in the context, respond with I don't know.' For legal teams, the 'I don't know' fallback is a feature, not a limitation — it prevents the model from inventing termination windows or penalty amounts that aren't in the document.

For higher accuracy, move to Advanced RAG. Apply pre-retrieval query rewriting: send 'What are the termination clauses?' to the LLM and ask it to rewrite the query for retrieval — producing something like 'termination clauses, notice periods, and conditions for ending the agreement.' Then apply post-retrieval reranking: number the retrieved chunks and ask the LLM to order them most-to-least relevant and discard boilerplate. Only the filtered, reranked clauses go into the final answer prompt.

How do you handle ambiguous or multi-part legal questions?

Questions like 'What are our rights if the counterparty breaches?' touch multiple clauses — breach, remedies, cure periods, termination. A single retrieval pass may miss some. Query fusion solves this: generate three diverse reformulations ('breach remedies and penalties', 'cure period after default', 'rights upon counterparty breach'), run a vector search for each, pool all retrieved chunks, and rerank with Reciprocal Rank Fusion (score = sum of 1/(rank + 60)). Chunks that surface across multiple angles rise to the top, so you capture the full legal picture.

What about traceability and audit trails?

Encapsulate the pipeline in a reusable class exposing a single `.ask(query)` method that returns the original query, rewritten query, retrieved context, refined context, and answer. This gives your team a complete audit trail: you can show exactly which chunks produced which answer — essential for review and compliance. Keep your LLM API keys in environment variables, never inline in shared notebooks.

Next step

Start with one representative contract. Load it, chunk it paragraph-aware with overlap and word-boundary correction, build a FAISS index, and run a Naive RAG pass on a known question. Once retrieval is clean, layer in query rewriting and reranking. Validate every answer against the source clauses before trusting the system for live review.

// FREQUENTLY ASKED QUESTIONS

Can RAG replace a lawyer reviewing contracts?

No — RAG grounds answers in your contract text and prevents hallucinated clauses, but it's a retrieval and summarisation aid, not legal judgment. Use it to surface relevant clauses quickly and trace answers back to source, then have a qualified lawyer review. The 'answer only from context' prompt and audit trail support review; they don't replace professional interpretation.

How do I prevent the system from inventing clauses that aren't in the contract?

Use a precise answer prompt instructing the LLM to answer only from the provided context and respond 'I don't know' if the answer is absent. Combine this with clean paragraph-aware chunking and reranking so only genuine clause text reaches the model. The 'I don't know' fallback is critical in legal work — it stops fabrication when the answer isn't in the document.

What chunk size works best for dense legal documents?

Around 200 characters with a 50-character overlap is a solid starting point, splitting on natural paragraphs and sub-clauses first. Dense legal text benefits from overlap to preserve cross-clause context. If clauses are long, test slightly larger chunks, but keep word-boundary correction so you never cut mid-word and corrupt retrieval.