Skip to content
mlmentorship

Design a RAG system for legal documents

Legal RAG amplifies every standard RAG concern: precise citations, no hallucinations, regulated domain, dense documents with structure. The senior answer addresses each.

Published · 7 min read ·Role-specific ·Intermediate

ML system design · active recall

Practice before you read

40 minutes. Scope first; then cover data, baseline, model, evaluation, serving, monitoring, and iteration.

ML system design · closed-book attempt

Scope first; then cover data, baseline, model, evaluation, serving, monitoring, and iteration.

Problem

Context. A hypothetical organization searches legal documents that vary by jurisdiction, effective date, structure, and access permissions. Users need answers with source support, including when documents conflict or the evidence is insufficient. Task. Specify ingestion, versioning, retrieval, answer generation, citation verification, abstention, and evaluation. Explain how a reviewer can trace a claim to the applicable document version and passage. Constraints. Enforce access permissions before evidence reaches generation, preserve provenance, and distinguish superseded material from current authority. Do not present unsupported synthesis as verified legal advice; define escalation and audit requirements.

40:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Asked in: legal-tech, fintech, and regulated-domain LLM interviews.

Design a document-grounded legal information service whose answers can be checked against the correct authorized source passages.

Problem

Context. A hypothetical organization searches legal documents that vary by jurisdiction, effective date, structure, and access permissions. Users need answers with source support, including when documents conflict or the evidence is insufficient.

Task. Specify ingestion, versioning, retrieval, answer generation, citation verification, abstention, and evaluation. Explain how a reviewer can trace a claim to the applicable document version and passage.

Constraints. Enforce access permissions before evidence reaches generation, preserve provenance, and distinguish superseded material from current authority. Do not present unsupported synthesis as verified legal advice; define escalation and audit requirements.

Domain-specific concerns vs generic RAG

What changes for legal:

  1. Citation precision is non-negotiable. Every claim must trace to a verifiable passage. A hallucinated case citation is a malpractice risk.
  2. Documents are highly structured. Statutes, contracts, case law have hierarchical structure (titles, sections, paragraphs, sub-clauses). Chunking that strips structure loses retrieval signal.
  3. Vocabulary is specialized. General-purpose embeddings underperform; BM25 with legal-jargon-aware tokenization often outperforms naive embeddings on exact-match queries (case names, statute numbers, defined terms).
  4. Refusal is a feature. “I can’t answer this from the available sources” is the right answer when retrieval is uncertain. Better than a confident wrong answer.
  5. Audit trails are required. Every retrieved passage and every generated answer needs to be logged for review.
A resolvable citation can still fail to support its claimBoth citations resolve to documents. Only citation A supports the exact claim; citation B must not pass merely because its link works.Every required gate must pass on thesame route+resolves+supportsCitation A resolves and supports theexact claim.+resolvesxsupportsCitation B resolves but cannotsupport this claim.
Derivation
A resolvable citation can leave the claim unsupportedFictional jurisdiction J authority version 2026-08-15 section four contains P-017 requiring filing within ten days and P-042 saying the clerk records receipt. C1 repeats the ten-day deadline and is supported. C2 infers guaranteed approval from receipt and is held. Release separately requires current jurisdiction/time applicability and required attorney approval.Fictional jurisdiction J; not legal adviceAuthority V2026-08-15 > section 4 >P-017"Notice must be filed within 10 days."P-042: "The clerk records receipt."C1: notice deadline is 10 days.P-017 directly supplies the bound:SUPPORTC2: receipt guarantees approval.P-042 says nothing about approval:HOLDRelease only supported C1, IFjurisdiction + effective-time checkspassAND required attorney approval isrecordedC2 is removed or escalated, not rescued byits citation.Keep query, exact passages, version, verdicts,and reviewer.
P-017 supplies C1's exact bound. P-042 does not establish approval, so C2 is removed or escalated. Support, applicability, and release authority remain separate. This is fictional, not legal advice.
Check: Which exact source span supports C1? Why is a resolvable P-042 citation insufficient to release C2?
Read it this way: Both citations resolve to documents. Only citation A supports the exact claim; citation B must not pass merely because its link works.
Does a resolvable but irrelevant citation clear exact-claim support?

What an L5 answer sounds like

“I’d build the standard 7-component RAG architecture (chunking, embedding, lexical index, query understanding, hybrid retrieval, reranking, generation), with these legal-specific changes:

Chunking: heading-aware semantic chunking. Each chunk inherits parent headings (Statute name, Section, Subsection) as a prefix. Sliding window with overlap to handle definitions split across boundaries.

Hybrid retrieval: BM25 weighted heavier than embedding similarity. Legal queries often look up specific cited authorities; lexical exact-match dominates.

Reranker: a strong cross-encoder, possibly fine-tuned on legal relevance judgments.

Generation: structured output with required citations. Every claim in the answer must include [passage_id]. Post-generation verification: an LLM-judge that takes (claim, cited passage) and verifies entailment. Flag or strip unsupported claims.

Refusal: explicit instruction in the prompt: ‘If the answer is not supported by the retrieved passages, respond: I cannot answer this from the available sources.’ Tune retrieval thresholds: if top-K passages are below a similarity floor, route to refusal directly without generation.

Eval: separate retrieval metrics (recall at K on legal Q&A pairs) from generation metrics (citation accuracy, faithfulness). Domain-expert review on a stratified sample.”

What an L6 answer adds

“…considerations for the regulated context:

Audit trail: every query, every retrieval, every generated answer logged with full lineage. Required for regulatory review and for incident investigation.

Versioned source corpus: legal texts change (amendments, new case law). The corpus is a tracked artifact with versioning; answers should cite the version of the source they used.

Domain-fine-tuned embeddings: out-of-the-box embeddings underperform on legal vocabulary. Fine-tune on (legal-query, relevant-passage) pairs, possibly synthetic via an LLM, possibly mined from real usage logs.

Hierarchical retrieval for long documents: a contract or case law document can be hundreds of pages. First retrieve at the document level (which contract is relevant), then at the section level within. Reduces irrelevant chunks polluting the context.

Adversarial robustness: users will ask questions designed to elicit confident wrong answers. Test against adversarial query sets; add refusal training data targeted at these patterns.

Human-in-the-loop for high-stakes outputs: route legal-research outputs to attorney review; route contract-analysis outputs to a confidence tier (auto-accept, review, reject) based on retrieval and verifier confidence.”

Tells that get you a strong-hire vote

  • You bring up structure-aware chunking (statutes have hierarchy, contracts have sections).
  • You weight BM25 heavier than dense embeddings for legal vocabulary.
  • You build in mandatory citations + verifier.
  • You make refusal a first-class output.
  • You discuss audit trails and source versioning as regulatory requirements.

Tells that get you down-leveled

  • Generic RAG with no domain-specific changes.
  • Embedding-only retrieval (will fail on case names, statute numbers).
  • No mention of citation verification.
  • Treating refusal as a failure mode rather than a valid output.

Common follow-up

“How would you measure citation accuracy?”

The L6 answer:

“Two-step. First, decompose each generated answer into atomic claims. Second, for each claim, verify against its cited passage using a separate LLM judge calibrated on a labeled set. Report ‘fraction of claims supported by their citations’ as the citation-accuracy metric. Track separately from answer-quality metrics: a model can have high answer quality and bad citations, or vice versa, and they have different remediation paths.”


Related: Designing a RAG system that actually works, How do you handle hallucinations in production?, LLM Evals essay.