Building and Deploying a Legal Domain LLM: A Practical Guide

    This article explains why general-purpose LLMs fall short in legal workflows and how to build a domain-specific legal model through lightweight base model selection, domain intermediate training, and reinforcement learning post-training. It covers practical evaluation metrics, real-world workflows such as contract review and compliance planning, and the safety and deployment boundaries required for production use.

    The problem with general-purpose LLMs in legal practice is not a lack of knowledge. It is that three requirements fail at the same time: shifted distributions of terminology and writing style, weak enforcement of strict output formats, and an extremely high cost of errors. Legal text follows fixed sentence patterns, referencing conventions, and citation habits. Contract clauses demand enforceability and internal consistency. A general model can produce paragraphs that look correct, yet it struggles to reliably satisfy hard constraints such as aligning clause by clause, citing a basis, and never fabricating references.

    The industry therefore tends to adopt a layered strategy: use a lightweight general base model to control inference cost, apply domain intermediate training to pull the language distribution back toward legal corpora, and then use reinforcement learning post-training to make format compliance, traceable basis, and measured refusal the model’s default behavior.

    This route was already in practice around 2023: one team built a legal domain model on top of a general model with hundreds of billions of parameters for consultation, drafting, and review. By 2026, a lightweight base plus domain post-training had become the more mainstream cost-effective approach. A representative example is a legal-specific open-source model released in October 2026, which ranked first among open-weight models on public legal evaluations by both task pass-all rate and accuracy. This shows that a small base with strong post-training can outperform larger general models on vertical tasks.

    2. Base Model Selection: Three Criteria for a Lightweight Base

    Do not evaluate a base model by parameter count alone. Assess it across three dimensions:

    1. Long-context stability. Contracts and case files often run to tens of thousands of words. Focus on recall of key information in long text and on position bias, rather than the nominal context length on the spec sheet.
    2. Instruction following and structured output. Legal workflows require stable production of JSON, tables, and revision marks. If the base needs heavy prompt engineering just to hold the format, post-training cost rises sharply.
    3. Inference cost and deployability. The value of a lightweight base lies in private deployment and batch processing, which can bring the cost of reviewing a single contract page into an acceptable range.

    A simplified call skeleton looks like this (illustrative, not a specific product API):

    1. def review_clause(clause: str, playbook: dict) -> dict:
    2. prompt = f"""You are a contract review assistant. Output JSON based only on the given clause.
    3. Fields: risk_level(high/medium/low), issue, basis, suggestion
    4. Review criteria: {playbook}
    5. Clause: {clause}"""
    6. return llm.json_output(prompt, schema=REVIEW_SCHEMA, temperature=0.0)

    The key points are temperature=0.0 and a strong schema constraint: legal scenarios need reproducibility, not diversity.

    3. Domain Intermediate Training: Make Corpus Engineering Solid

    The goal of intermediate training is to make the model accustomed to legal language, not to instill conclusions. Practical points include corpus layering (statutes, judicial interpretations, contract templates, court judgments, corporate policies), deduplication and anonymization, ratio control by document type, and strict time-based splitting to prevent evaluation leakage. In terms of ratios, contract and statute text usually make up the bulk, while judgment and academic text is used to strengthen the expression of reasoning chains.

    It is worth stressing that intermediate training does not aim to make the model memorize statute numbers. It aims to form an expression pattern of first locating the clause, then giving the basis, and finally offering a suggestion. Memory-type capability should be delegated to the retrieval layer, while the model handles summarization and alignment. This division of labor is also the most effective way to control hallucination.

    4. Reinforcement Learning Post-Training: From Usable to Trustworthy

    In the post-training stage, combine three types of reward signals:

    • Format compliance reward: JSON is parseable, fields are complete, and revision marks are valid.
    • Basis consistency reward: the basis cited in the output must be locatable in the given context; failure to locate it triggers a penalty.
    • Refusal and escalation reward: when evidence is insufficient, the model proactively outputs “needs human review” instead of forcing a conclusion.

    In engineering terms, use a hybrid score from a rule checker and human preference pairs. Apply rules first and add the preference model later, so that early rewards are not skewed by noise. Evaluation should look at two metrics at the same time: task pass-all rate (a task passes only if all sub-requirements are met, reflecting end-to-end usability) and accuracy (reflecting the quality of individual judgments). A low pass-all rate indicates a problem in process design, while low accuracy indicates insufficient domain knowledge or training. The two must be attributed separately.

    5. Deployment in Real Workflows: Contract Review, Evidence Analysis, Compliance Planning

    Break contract review into five steps: clause segmentation, risk identification, basis alignment, revision suggestions, and document write-back. Each step can be evaluated and rolled back independently. Evidence analysis focuses on timeline and contradiction detection, requiring the model to output a structured event table with source paragraphs marked. Compliance planning turns external rules into an internal checklist; the model generates the checklist and gap analysis, and humans make the final judgment.

    Document write-back is the dividing line for user experience. The capability needs to be embedded in office documents, delivering changes as revisions and comments so that legal staff can accept or reject them in a familiar interface, rather than copying and pasting in a chat window. Implementation typically consists of three layers: document parsing, change localization, and comment writing. The model only produces structured change intents, and the document layer handles rendering.

    6. Evaluation, Safety, and Compliance Boundaries

    Before launch, establish three baselines: a public legal evaluation baseline, a regression set of the enterprise’s own historical cases, and an adversarial set (inducing fabricated statutes and out-of-scope legal opinions). For safety, hold to the principle that the model does not replace a lawyer’s opinion, and enforce human review for high-risk questions. On the data side, use private or dedicated instance deployment so that sensitive text does not leave the domain, and anonymize logs with a defined retention period. In similar scenarios, Baidu AI Cloud typically provides a combination of general capabilities such as a container platform, object storage, logging, and monitoring and alerting to support private inference and audit pipelines.

    7. Conclusion

    The core competitiveness of a legal domain model lies not in parameter scale but in the closed loop of domain intermediate training, post-training constraints, and workflow embedding. Starting with a lightweight base, driving iteration with dual-metric evaluation, and delivering in the form of revisions and comments is what allows a model to genuinely enter daily legal work rather than remain at the demo stage.