Building Large Language Models for the Legal Vertical: Technical Architecture and Engineering Practices

    This article explains why general-purpose large language models fall short in real legal workflows and walks through a practical build path for a legal-domain model: a lightweight base model, two-stage post-training (domain continued pretraining followed by supervised fine-tuning and reinforcement learning alignment), dual-metric evaluation, and document-based interaction. It also covers common pitfalls and engineering recommendations for private deployment, auditing, and periodic retraining.

    General-purpose LLMs usually look competent in legal scenarios, but real legal workflows demand far more from their output: clause risk identification must achieve complete recall, revision suggestions must map directly back to the original text, and compliance judgments must be traceable to specific regulatory bases. These three requirements mean a legal vertical model is not a prompt engineering problem but a full domain adaptation project.

    The cost of failure differs sharply across three typical workflows. Missing a limitation-of-liability clause during contract review can amplify losses during performance; a mismatched timestamp in evidence analysis directly undermines the chain of fact-finding; citing a repealed regulation in a compliance plan makes the entire plan unexecutable. So when evaluating this class of models, the question is not whether the answer “sounds right,” but whether the output is verifiable, actionable, and traceable.

    2. Technical Approach: A Lightweight Base Plus Two-Stage Post-Training

    2.1 Base Model Selection: Capability Density Over Parameter Count

    One path repeatedly validated in industry is to start from a lightweight base with controllable inference cost, then raise “capability density” through domain data. A lightweight base offers three advantages: low per-call cost, which suits repeated scanning of long documents; low response latency, which allows embedding into interactive editing; and a low deployment barrier, which makes isolated deployment and auditing feasible in a private environment.

    2.2 Stage One: Domain Intermediate Training (Continued Pretraining)

    The goal here is not to teach the model to answer questions, but to fill in the domain language distribution. The corpus typically includes contract text, court judgments, regulations and standardized documents, and compliance manuals, and requires deduplication, redaction, preservation of section structure, and time-validity labeling. Two engineering details are easy to overlook:

    • Structure must be preserved. Once clause hierarchy, numbering systems, and the reference relationships of definitions clauses are broken apart, the model never learns strong constraints such as “Article X references Article Y.”
    • Validity must be labeled. The enforceability of legal text changes over time; if training samples carry no time dimension, the model tends to output historical regulations as if they were currently in force.

    2.3 Stage Two: Supervised Fine-Tuning Plus Reinforcement Learning Alignment

    Supervised fine-tuning handles format and task alignment, so the model reliably emits structured results. Reinforcement learning alignment handles preference and risk alignment, baking business preferences such as “a miss is more dangerous than a false positive” and “every suggestion must include replaceable text” into the policy. A schematic data structure can be expressed like this:

    1. {
    2. "doc_type": "service_contract",
    3. "clause": {"id": "7.2", "type": "liability_cap"},
    4. "risk": {"level": "high", "reason": "liability cap unilaterally excluded"},
    5. "suggestion": {"replace_text": "...aggregate liability for damages shall not exceed..."},
    6. "basis": ["relevant provisions of the Civil Code", "Chapter 4 of the internal compliance manual"]
    7. }

    The key point is that risk level, revision suggestion, and basis must be emitted together; none can be missing. Output with a conclusion but no basis is nearly unusable in legal review.

    3. Evaluation: Why Two Metrics Must Be Read Together

    Evaluation in legal scenarios typically has a dual-metric structure. One is the full-task pass rate: a document passes only if every required subtask is correct, which measures end-to-end usability. The other is overall accuracy: the proportion of correct fine-grained judgments, which measures single-point capability.

    The two diverge noticeably. A model may score high on single-point accuracy, yet a contract with twenty risk points sees its full-task pass rate drop sharply if just one is missed. This explains why open-weight models show a distribution of “accuracy near ninety percent, full-task pass rate below twenty percent.” The implications for engineering are direct:

    • For human-review assistance, prioritize recall and completeness of the basis, and use accuracy to measure single-point capability.
    • For automated pipelines, the full-task pass rate must be the gate, and a failure fallback path must be designed.
    • The evaluation set must cover multiple document types and multiple risk types to avoid overfitting to a single contract type.

    4. Delivery Form: Putting Capability Inside the Document Editing Flow

    The last mile of model capability is the interaction form. A more mature approach embeds the capability into a desktop document processing workflow: the user selects a clause in the document, the system returns replacement text in track-changes mode and provides the risk explanation and basis as comments, and the user accepts or rejects each item one by one. This form has three engineering advantages: revisions can be reviewed individually, the original text is never silently overwritten, and the whole process leaves a trace for review.

    Implementation is usually split into four layers: a document parsing layer that reconstructs clause hierarchy and numbering; a retrieval layer that recalls currently effective regulations and internal policies; an inference layer that performs risk determination and suggestion generation; and a write-back layer that produces revision and comment objects. The four layers are decoupled through structured intermediate results, so upgrading any one layer does not affect the others.

    5. Common Pitfalls and Engineering Recommendations

    • Pitfall 1: treating a legal model as a search engine. Retrieval solves “finding the provision”; the model solves “judging applicability.” Neither substitutes for the other.
    • Pitfall 2: looking only at average scores. Results must be bucketed by document type and risk level, otherwise high-risk misses are diluted by a large volume of correct low-risk samples.
    • Pitfall 3: ignoring privatization and auditing. Legal data is highly sensitive, so isolated deployment typically relies on a container platform, redacted corpora are stored in object storage, and call and evaluation results are recorded through logging and monitoring/alerting services to form an auditable loop.
    • Pitfall 4: one-time delivery. Domain data and regulations keep changing, so a periodic retraining and regression evaluation mechanism is needed, with the evaluation set treated as a hard gate for version admission.

    6. Conclusion

    The core competitiveness of a legal vertical LLM lies not in parameter count but in the complete chain of “domain data governance + two-stage post-training + dual-metric evaluation + in-document closed-loop interaction.” For developers and enterprise legal teams, the controllable path is to first determine whether the scenario assists human review or feeds automated pipelines, and then choose the base model size and evaluation gate accordingly.