How to Build a Legal AI Assistant That Cites Its Sources: Lessons From 1.2 Million Documents

Short answer: A legal AI assistant you can trust is not a chatbot with a clever prompt. It is a data pipeline with a language model at the end. You collect laws from official sources, digitise and classify them, index them for both exact and semantic search, and have the model answer only from retrieved passages, with citations. This is retrieval-augmented generation (RAG). It is the architecture Shrijan Tech used to build a legal assistant for a European LegalTech startup, covering 120+ countries and more than 1.2 million documents.
Full project details are in our LegalTech AI case study. This article explains how the system works and what we would tell anyone building something similar.
Why can't you just use ChatGPT for legal research?
General-purpose models are impressive at language and unreliable on law. During this project we saw the same failure modes the industry keeps running into:
- Misreading legal language. Terms of art, defined terms and cross-references mean specific things that a general model often blurs.
- Mixing jurisdictions. A rule that holds in one country is presented as if it applies everywhere.
- No source, no accountability. An answer without a citation can't be checked, and in legal work an answer you can't check is useless.
- Stale knowledge. Laws change. A model's training data does not.
The fix is to separate knowing from writing. The knowledge lives in a curated, current, searchable corpus. The model's job is to read the relevant parts and write a clear answer that points back to them.
The architecture, layer by layer
1. Collection: scrapers for 120+ official sources
Legal data was scattered across more than 120 government bodies, each with its own website structure, file formats and languages. We built Python scrapers to collect and parse both structured pages and unstructured documents from these official portals, running continuously to pick up new and amended laws.
Lesson: treat scrapers as production software. Government sites change layout without notice, so every scraper needs monitoring and alerting when its output volume or structure suddenly changes.
2. Digitisation: OCR for scanned documents
A large share of legal material exists only as scanned PDFs. An OCR pipeline converts those into machine-readable text before anything else can happen.
3. Classification: 25+ legal domains
Every document was tagged by jurisdiction and by legal domain (more than 25 categories, including corporate law, intellectual property and compliance) using text-classification models. Extraction, classification and metadata tagging reached about 99% accuracy.
Lesson: metadata is what makes retrieval precise. "Data protection rules in Germany" should only search German data-protection material, and you can only do that if every document carries reliable jurisdiction and domain tags.
4. Knowledge layer: hybrid search
We indexed the corpus in Elasticsearch for keyword search and Pinecone for semantic vector search. Using both matters in law:
- Vector search finds passages that mean the same thing as the question, even with different wording.
- Keyword search finds exact things vectors handle poorly, such as section numbers, statute names and defined terms.
Combining the two gives better recall than either alone, and makes verified citations possible.
5. Reasoning: a domain-tuned model with LangChain
We trained a domain-specific legal language model on proprietary data to improve context retention, accuracy and multilingual understanding. We orchestrated it with LangChain and OpenAI APIs. For each question the system retrieves the most relevant passages, passes them to the model with strict instructions to answer from that context, and returns the answer with references to the source documents.
6. Delivery: fast, scalable infrastructure
The platform runs as FastAPI services with a Next.js / React front end, deployed on AWS (Lambda and EC2) in Docker containers, so it scales automatically as the document count and user load grow.
What did it achieve?
- 120+ countries of legal data collected and structured.
- 1.2 million+ legal documents processed within three months of deployment.
- 25+ legal categories indexed for category-specific search and summaries.
- About 99% accuracy in extraction, classification and tagging.
- 80% faster legal research, because users get cited summaries in seconds instead of reading raw documents.
"Shrijan Tech didn't just build us an AI, they built a complete legal intelligence ecosystem. The precision, speed, and scalability have completely transformed how our users research and interpret global laws." (CEO and Founder, LegalTech AI Startup)
Five lessons for anyone building a domain AI assistant
- Most of the work is data, not the model. Collection, cleaning, OCR and classification took far more engineering than the chat interface. Budget for it.
- Citations are a feature, not a nice-to-have. Professionals adopt AI tools they can verify. Design the answer format around sources from day one.
- Use hybrid search. Pure vector search misses exact references. Pure keyword search misses meaning. You need both.
- Metadata beats prompt engineering. Good jurisdiction and topic tags narrow retrieval more reliably than any instruction to the model.
- Build an evaluation set early. Keep a fixed list of real questions with known correct answers and sources, and re-run it after every change to the pipeline or model.
Does this approach work outside law?
Yes. The same pattern fits any field where answers must come from a large, changing body of documents and be checkable: regulations and compliance, insurance policies, contracts, product and technical manuals, medical guidelines, research libraries and internal knowledge bases. Only the sources and classification scheme change. If you are weighing options, our guide Chatbot vs. AI Assistant vs. RAG explains when this level of system is worth it.
Frequently asked questions
What is retrieval-augmented generation (RAG)?
RAG is a way of using a large language model where the system first searches a trusted document collection for passages relevant to the question, then asks the model to answer using those passages. It keeps answers current and grounded in real sources, and lets the system show citations.
How do you stop a legal AI from hallucinating?
Ground every answer in retrieved source text, instruct the model to answer only from that text and to say when the sources don't cover the question, show citations so users can verify, and test continuously against a set of questions with known answers. Hallucinations can't be eliminated completely, but this design makes them rare and easy to catch.
How long does it take to build a legal AI assistant?
It depends mainly on the number and messiness of data sources. In this project, more than 1.2 million documents were ingested and structured within three months of deployment. A narrower assistant over a single jurisdiction or a company's own documents can reach a working pilot much faster.
Which tech stack did Shrijan Tech use for the legal AI assistant?
Python for scraping and NLP pipelines, OCR for scanned documents, LangChain with OpenAI APIs and a custom-trained legal model, Elasticsearch and Pinecone for hybrid search, PostgreSQL, FastAPI, Next.js/React, and AWS (Lambda, EC2) with Docker.
Can Shrijan Tech build a RAG assistant on my company's documents?
Yes. This is the core of our Generative AI & LLM services. We usually start with a short scoping exercise covering your sources, how accurate answers must be, and where the data can live, then build a pilot on a representative slice of documents. Tell us about your use case.



