A Practical Guide to Building Legal Domain LLMs

    This article explains why general-purpose LLMs struggle with legal workflows and how to build a legal domain model instead. It covers a two-stage approach combining domain intermediate training with reinforcement learning post-training, three core workflows (contract review, evidence analysis, and compliance planning), evaluation metrics that go beyond a single accuracy score, and a two-layer architecture for rendering structured model output into document revisions. It also outlines common pitfalls and a phased adoption path.

    General-purpose LLMs typically run into three problems in legal settings. First, they struggle to preserve structure across long documents. Contracts often run to tens of thousands of words, and once a cross-clause reference breaks, any conclusion loses its basis. Second, terminology and reasoning chains do not line up: a model can restate a legal concept but cannot complete the full “fact to norm to conclusion” derivation. Third, the output is not verifiable. Legal work does not call for fluent prose; it calls for revision suggestions that can be located, traced, and modified.

    The core goal of a legal domain model is therefore not to “talk better” but to embed model capability into a real workflow: the input is contracts, case files, and policy documents, and the output is located revision suggestions, risk lists, and compliance plans.

    2. Two-Stage Construction: Domain Intermediate Training and Post-Training

    2.1 Lightweight Base Model Plus Domain Intermediate Training

    Choosing a base model with a moderate parameter count is a trade-off between inference cost and response latency. Legal work is largely interactive review, a single request carries an extremely long context, and cost rises non-linearly with model size. The goal of the intermediate training stage is to make the model “read and understand” domain corpora: contracts, judgments, regulatory rules, and internal corporate policies. This stage usually continues with a self-supervised objective, focusing on improving the distribution of domain vocabulary, long-document structure modeling, and cross-paragraph coreference.

    A schematic approach to organizing the data looks like this (pseudocode that expresses only the layering and ratio logic):

    1. corpus = {
    2. "contracts": 0.35, # contracts and business text
    3. "judgments": 0.25, # court judgment text
    4. "regulations": 0.20, # regulatory and compliance rules
    5. "internal_docs": 0.15, # corporate policies and processes
    6. "general": 0.05, # general corpus to prevent capability loss
    7. }
    8. for domain, ratio in corpus.items():
    9. trainer.add_stage(DomainStage(data=load(domain), ratio=ratio))

    Keeping a small amount of general corpus is common practice. The purpose is to suppress catastrophic forgetting and prevent the model from losing basic expression and commonsense reasoning while its domain capability improves.

    2.2 Reinforcement Learning Post-Training: From Usable to Deliverable

    The post-training stage addresses preference alignment. The preferences in legal practice are clear-cut: conclusions must be grounded, uncertainty must be stated explicitly, and edits must be minimal without distorting the original meaning. The common approach is to first run supervised fine-tuning on high-quality demonstration data, then train a reward model on preference data, and finally optimize the policy with reinforcement learning. The reward signal is usually a combination of three automatically checkable elements: whether a citation actually exists, whether an edit lands on the correct clause, and whether the format can be parsed by downstream tools.

    3. Capability Breakdown: Three Typical Workflows

    • Contract review: First segment clauses and identify roles (Party A obligations, Party B responsibilities, breach and termination), then assign a risk level and alternative wording to each clause. The key engineering point is “location”: every suggestion is bound to a text span offset in the original, making it easy to generate revisions and comments in the document.
    • Evidence analysis: This handles timeline alignment and contradiction detection across multiple heterogeneous sources. The model needs to output the correspondence between evidence and facts to be proven, and flag gaps, rather than issuing a conclusive judgment directly.
    • Compliance planning: Map business actions to applicable rules and output an executable checklist with responsibility assignments. This scenario is highly sensitive to timeliness and usually requires an external retrieval connection, with the latest version of the rule base as the source of truth.

    4. Evaluation: Why a Single Accuracy Score Is Not Enough

    Legal tasks have two main-line metrics: task pass rate and overall accuracy. The former measures the proportion of tasks that are usable in a single delivery; the latter measures the average degree of correctness. The two can differ sharply: a model may have a decent average accuracy yet almost never get every part of a task right on the first attempt, and practice demands exactly the latter. Evaluation should therefore report both metrics and break them down by task type, so that a total score does not mask weak spots.

    For a self-built evaluation set, three layers are recommended: clause level (location and rewriting), document level (cross-clause consistency), and process level (final delivery quality after multiple interaction turns).

    5. Engineering Integration: From Model to Document Revision

    For deployment in office documents, a two-part architecture is recommended: the model outputs a structured intermediate layer, and the document layer renders it.

    1. {
    2. "target": {"clause_id": "C-07", "span": [1024, 1187]},
    3. "risk": "high",
    4. "suggestion": "Cap the liquidated damages at five percent of the total contract value",
    5. "rationale_ref": ["R-2024-113"],
    6. "mode": "track_changes"
    7. }

    Four points need engineering attention. First, chunking and recall for long contexts, to avoid losing information across chunks. Second, citation validation: any rationale that cannot be traced back to the original text should be blocked. Third, permissions and auditing: model calls, revision suggestions, and human adoption should be logged across the full chain. Fourth, human-in-the-loop collaboration: high-risk clauses require mandatory human confirmation. For a cloud-based approach, object storage can hold corpora, a message queue can smooth traffic spikes, a container platform can host inference services, and logging and monitoring with alerting can track latency and failure rates.

    6. Common Pitfalls and Adoption Recommendations

    The first pitfall is treating a domain model as a replacement for a rule base. In fact, the two are complementary: rules guarantee determinism, while the model handles ambiguity and the long tail. The second is overlooking data compliance. Legal corpora contain large amounts of sensitive information and require redaction, isolation, and least-privilege access control. The third is looking only at demo results while lacking regression evaluation, which makes capability regressions after a model update hard to detect.

    The recommended path forward is to start with a single high-frequency scenario (such as standard contract review) to establish an evaluation baseline, then gradually expand the task types, and finally connect to the document workflow to close the loop. The value of a domain model lies not in replacing legal professionals but in compressing repetitive review into a manageable time cost, so that professional judgment is concentrated where people are genuinely needed.