Building a Legal Domain Large Language Model: Architecture and Engineering Practices
This article explains why general-purpose large language models struggle with legal practice and outlines an engineering approach to building a legal vertical model. It covers a lightweight base model with two-stage training, domain intermediate training for legal corpora, post-training for evidence-based behavior, a dual-dimension evaluation system, integration into document revision and annotation workflows, and security, compliance, and cost considerations. Readers will find practical guidance on data organization, training samples, evaluation design, and human-in-the-loop delivery for auditable legal AI systems.
1. Why General-Purpose LLMs Fall Short in Legal Practice
General-purpose LLMs typically exhibit three types of problems in legal scenarios. First, their understanding of long-document structure is unstable: cross-references, exceptions, and priority relationships between contract clauses are easily flattened. Second, terminology and reasoning chains lack rigor, producing outputs that seem plausible but lack verifiable support. Third, they do not operate as a workflow, so they cannot turn conclusions into real deliverables such as document revisions, annotations, and version comparisons.
As a result, the goal of a legal vertical model is not to be “better at chatting,” but to embed professional capability into an auditable, traceable, and deliverable process. This dictates that the technical approach must cover four layers at once: data, training, evaluation, and integration.
2. Overall Technical Approach: Lightweight Base + Two-Stage Training
A widely adopted approach in the industry is to select a base model with moderate parameter size and controllable inference cost, inject knowledge through domain intermediate training, shape behavior through supervised fine-tuning and preference alignment, and finally connect to business systems in a tool-based manner. The value of a lightweight base lies in low cost per request and good concurrency, making it suitable for long documents such as contracts and high-frequency invocation scenarios.
It is worth emphasizing that capability gains come from a closed loop of “data mix + training strategy + evaluation feedback,” not from simply piling on parameters.
3. Domain Intermediate Training: Turning Legal Knowledge into Model Priors
The goal of intermediate training is to adapt the model to the distribution characteristics of legal corpora. Engineering efforts usually focus on four points:
- Corpus composition: Layered ratios of laws and regulations, judicial documents, contract templates, compliance manuals, and academic literature, avoiding bias from a single source.
- Cleaning and desensitization: Removing invalid templates, unifying numbering schemes, and desensitizing personal information and trade secrets.
- Long-context organization: Splitting by clause hierarchy while preserving heading paths, so the model learns the structural prior of “chapter–article–paragraph.”
- Ratio experiments: Determining domain proportions through small-scale controlled experiments, judged jointly by validation perplexity and downstream task metrics.
The following is an illustrative data organization logic (pseudocode, expressing structure only):
def build_domain_corpus(docs):samples = []for doc in docs:for node in doc.section_tree(): # preserve chapter/article/paragraph hierarchytext = node.render(with_path=True)if quality_check(text) and desensitize(text):samples.append({"text": text, "meta": node.path})return rebalance(samples, ratio={"law":0.4, "contract":0.3, "compliance":0.2, "academic":0.1})
4. Post-Training: From “Answering” to “Answering by the Rules”
Post-training addresses behavioral consistency. In the supervised fine-tuning stage, “instruction–evidence–conclusion” triplet samples are constructed to force the model to provide its basis before its conclusion. In the preference optimization stage, paired samples distinguish “answers with citations” from “answers without citations,” suppressing overconfident expression.
A simplified training sample shape:
{"instruction": "Review the risk of this clause and provide revision suggestions","evidence": ["Clause 7.2: Late payment incurs a penalty of 0.05% per day"],"output": {"risk": "The penalty rate is too high", "suggestion": "Adjust to 0.03% and set a cap", "basis": "Clause 7.2"}}
Engineering practice also requires refusal and clarification strategies: when evidence is insufficient, the model should output “additional materials required” rather than fabricating clauses. This is the baseline for usability in legal scenarios.
5. Evaluation System: Dual Dimensions Beat a Single Score
Legal model evaluation is best served by two complementary metric types. The first is accuracy-type metrics, which measure whether answers are correct. The second is task pass rate, meaning the proportion of tasks in which format, citations, conclusions, and risk warnings are all compliant in a single pass. The latter is closer to real delivery, because in practice “partially correct” is often unusable.
The evaluation set should cover task families such as contract review, evidence organization, and compliance planning, and distinguish zero-shot from few-shot settings. A human review step should also be retained, with spot checks on citation authenticity and clause localization to avoid inflated metrics.
6. Embedding into Real Workflows: Engineering Document Revision and Annotation
The last mile of model capability is the delivery format. A viable approach is to use the model as an inference node in the document processing pipeline: parse the document structure, locate target clauses, generate revision suggestions and annotations, then write them back as standard revision marks for human confirmation and acceptance or rejection.
Document parsing -> Clause location -> Risk identification -> Suggestion generation -> Revision/annotation write-back -> Human confirmation -> Version traceability
Key design points include: keeping dual versions of the original and revised text for auditing; attaching the basis location in annotations; and never auto-accepting changes by default, ensuring a human is always in the loop.
7. Security, Compliance, and Cost
Legal data is highly sensitive, so encryption in transit and at rest, fine-grained permissions, and operation auditing should be enabled by default; training and inference data require clear authorization boundaries. On cost, a lightweight base combined with caching, batching, and tiered routing (simple tasks to a small model, complex tasks escalated) can significantly improve unit cost. If cloud-based general capabilities are used, common categories such as object storage, logging services, and monitoring and alerting can support data retention and observability.
8. Conclusion
The core competitiveness of a legal vertical LLM lies not in how impressive a single demo is, but in whether domain data, training strategy, evaluation loop, and workflow integration form a sustainable engineering system for iteration. Starting from a lightweight base and ending with auditable delivery is the robust path for adoption in professional scenarios.
