Building Vertical LLMs for Legal Use: Technical Architecture and Engineering Practices
This article explains how to build a vertical large language model for legal scenarios, where general-purpose models often look capable but are not reliable enough to use. It covers base model selection, domain continued pretraining, supervised fine-tuning, and preference-based alignment, along with data governance for deduplication, time-sensitive labeling, and privacy masking. It also clarifies two evaluation metrics, accuracy and full task pass rate, and details implementation practices for contract review, evidence analysis, compliance planning, and office document integration. The article closes with risk and governance requirements such as human-in-the-loop review, traceability, data boundaries, hallucination suppression, and continuous evaluation.
1. Why Legal Scenarios Need a Vertical Model
General-purpose LLMs often look capable in legal tasks, but practitioners hesitate to rely on them. The problem is not fluency. It comes from three structural gaps:
- Long-tail terminology and writing conventions: legal text contains many fixed sentence patterns, citation structures, and nested definitions.
- Strict reasoning requirements: determining the validity of a clause depends on the joint constraints of multiple elements, such as the parties, subject matter, term, and liability for breach.
- Zero tolerance for errors: in practice, a single missed item can directly change a negotiating position or the outcome of litigation.
So the core goal of a legal vertical model is not “write better,” but “miss less, explain more clearly, and stay traceable.” This makes domain data and alignment training the main technical focus, rather than simply scaling up parameters.
2. Technical Roadmap: From a Lightweight Base to a Usable Assistant
2.1 Base Model Selection: Prioritize Inference Cost and Throughput
Legal assistance tools usually need long context, batch document processing, and low-latency interaction. A common industry approach is to start with a lightweight base model and add domain training on top. A lightweight base offers higher throughput per unit of compute and is easier to deploy as a private or dedicated instance, which helps meet data residency requirements.
2.2 Domain Continued Pretraining: Injecting “Legal Intuition”
After general pretraining, add domain continued pretraining. The corpus typically includes public court judgments, statutes and regulations, standard contract templates, compliance manuals, and de-identified internal documents. The key engineering challenge at this stage is data governance:
- Deduplication and near-duplicate detection to avoid distribution collapse caused by templated text.
- Time-sensitive labeling: attach timestamps for regulatory revisions and repeals to support version control during later retrieval.
- Privacy masking: replace names, ID numbers, account numbers, and addresses with placeholders, and keep the mapping table in a controlled environment.
2.3 Supervised Fine-Tuning: Turning “Capability” into “Actions”
Practical value comes from executable actions, not general commentary. Instruction data should be built around atomic operations for real tasks, such as “locate risky clauses,” “suggest replacement wording,” “flag missing clauses,” and “explain the reason for a change.” Each sample should follow a four-part structure: task instruction, context material, expected output, and rationale. The rationale is the foundation for later auditability.
Example training sample structure (pseudocode):
sample = {"instruction": "Review the risky clauses in the following contract and suggest revisions","context": contract_text, # De-identified full contract text"output": {"findings": [{"span": [1024, 1088],"risk": "Unequal liability for breach","suggestion": "Add a clause capping liquidated damages for both parties","reason": "Clause 7.2 only binds Party B"}]},"citations": ["Internal Review Guidelines, Chapter 4"] # Source for traceability}
2.4 Reinforcement Learning Alignment: Matching Expert Preferences
After supervised fine-tuning, use preference optimization to teach the model which phrasing experts prefer. Preference pairs can be mined from senior legal counsel’s accept/reject records: accepted revision suggestions become positive samples, while rejected suggestions or those that caused rework become negative samples. The reward design should explicitly penalize two behaviors: assertions without a basis, and speculation beyond the scope of the material.
3. How to Read Legal Model Evaluation Correctly
Two common metrics for legal models have very different meanings:
- Accuracy: the average correctness across all questions, reflecting overall usability.
- Full task pass rate: when a question contains multiple sub-judgments, it counts as passed only if all sub-items are correct, reflecting the proportion that can be delivered directly.
The gap between the two reveals where engineering effort should go. When accuracy is high but the full pass rate is low, the model does understand the task but lacks stability—one link in a multi-element task always fails. The fix is not to pile on more knowledge, but to add structured output constraints, step-by-step verification, and self-consistency checks, breaking “get it right in one shot” into “get every step right.”
4. Implementation Notes for Three Real Workflows
Contract Review
Use a four-stage pipeline: retrieve, locate, compare, and generate. First retrieve similar templates by clause type, then locate the boundaries of the clause under review, compare it item by item against internal review guidelines, and finally generate revision suggestions with reasons. Output should be traceable at the clause level, with each suggestion including the original text span so humans can verify it quickly.
Evidence Analysis
The hard part of evidence work is timeline and source consistency. In engineering terms, first structure the material (time, party, action, carrier), then build an event graph to detect time conflicts and party contradictions. Model output must distinguish three states: “explicitly stated in the material,” “inferable from the material,” and “not covered by the material,” to avoid presenting inference as fact.
Compliance Planning
Compliance is a continuous process, not a one-time generation. Map obligations, controls, and responsible roles to each other. The model identifies applicable obligations and gaps, a rules engine validates mandatory clauses, and the result is a trackable remediation checklist.
5. Integration with Office Document Workflows
Embedding review capability into the daily office environment is key to adoption. A practical approach: the document parsing layer preserves paragraph and style information, the model outputs a standardized structure of revisions and comments, and the document layer renders them as tracked changes and comment bubbles. Structure example:
{"revision": {"type": "replace", "range": [1024, 1088], "text": "……"},"comment": {"anchor": [1024, 1088], "body": "Risk: unequal liability", "severity": "high"}}
Three engineering points matter: revisions must be reversible with one click; comments must carry a severity level and a link to the basis; and for batch processing, use a message queue to smooth peaks, and store parsing results, model outputs, and human decisions separately to form a complete audit chain.
6. Risk and Governance: A Step You Cannot Skip
- Human in the loop: the model acts as a copilot, and signing authority stays with the practicing professional.
- Traceability: conclusions must be bound to the material version and the guideline version.
- Data boundaries: de-identify sensitive material before it enters the training or inference pipeline, and apply least-privilege access.
- Hallucination suppression: set a refusal policy for content without a basis; leave it blank rather than fabricate.
- Continuous evaluation: maintain a regression evaluation set and rerun the full pass rate after every model or prompt change.
7. Conclusion
The value of a legal vertical model comes from the combined effect of domain data, alignment training, and workflow integration. For developers and enterprise legal teams, success is usually determined not by model size, but by whether the evaluation criteria match delivery standards, whether outputs are auditable, and whether the model fits seamlessly into existing office and approval workflows. To validate results in your own environment, you can build an isolated evaluation and canary environment using general-purpose container platforms, object storage, and monitoring and alerting capabilities available on the cloud, starting with a small pilot before gradually scaling up.
