← Back to Use Cases

Legal Contract Analysis

Build contract analysis with entity extraction, clause classification, and semantic search - all without data leaving the firm.

Legal Contract Analysis

Build contract analysis with entity extraction, clause classification, and semantic search - all without data leaving the firm.

Category: Industry Solution

The Problem

Law firms process thousands of contracts containing sensitive client information, privileged communications, and proprietary deal terms. Sending this content to cloud AI services raises attorney-client privilege concerns, may create malpractice exposure if confidentiality is not adequately protected, and may breach confidentiality agreements. Yet manual contract review is slow, expensive, and error-prone.

This is a common challenge for teams building modern applications. Traditional approaches either compromise on privacy (by sending data to cloud APIs), require complex server infrastructure (adding cost and maintenance burden), or sacrifice functionality (by avoiding AI entirely). LocalMode provides a fourth option: run the AI locally in the browser.

The Solution

Build a complete contract analysis pipeline running entirely in the browser. Extract party names, dates, and monetary values with BERT-NER. Classify clauses (indemnification, termination, non-compete) with zero-shot classification. Build a searchable contract database with semantic search. Encrypt all stored vectors with AES-256-GCM. PII redaction strips sensitive data before any processing. No data ever leaves the device.

Why Local-First?

Building this feature with on-device inference provides three structural advantages over cloud-based alternatives:

  1. Zero marginal cost - After the initial model download, every inference operation is free. No per-token fees, no monthly API bills, no surprise invoices. This matters especially for features used frequently or by many users.
  2. Architectural privacy - User data never leaves the device. This is not a policy promise ("we won't look at your data") but an architectural guarantee: the data physically cannot reach any server because the processing happens in the browser tab.
  3. Offline capability - Once models are cached in IndexedDB, the entire feature works without internet. This is critical for field deployments, mobile apps with spotty connectivity, and enterprise environments with restricted networks.

Technology Stack

PackagePurpose
@localmode/coreextractEntities(), classifyZeroShot(), VectorDB, encrypt()
@localmode/transformersBERT-NER, DeBERTa, BGE-small models
@localmode/pdfjsPDF contract text extraction

Install the required packages:

npm install @localmode/core @localmode/transformers @localmode/pdfjs

Implementation

import { extractEntities, classifyZeroShot, embed, createVectorDB } from '@localmode/core';
import { transformers } from '@localmode/transformers';

const nerModel = transformers.ner('Xenova/bert-base-NER');
const zeroShot = transformers.zeroShot('Xenova/nli-deberta-v3-xsmall');

// Extract parties, dates, and monetary values
const { entities } = await extractEntities({ model: nerModel, text: clauseText });

// Classify clause type without training data
const { labels, scores } = await classifyZeroShot({
  model: zeroShot,
  text: clauseText,
  candidateLabels: ['indemnification', 'termination', 'non-compete', 'confidentiality', 'liability'],
});
const label = labels[0]; // top-ranked label

How This Works

The code above demonstrates the complete pipeline. Let us walk through the key decisions:

  • Model selection - The models referenced in this example are chosen for their balance of size, speed, and quality for this specific use case. Smaller models load faster and use less memory; larger models produce better results. Start with the recommended models and upgrade only if quality is insufficient for your users.
  • Browser APIs - LocalMode uses IndexedDB for persistent storage (vectors, model cache), Web Workers for background processing (keeping the UI responsive during inference), and the Web Crypto API for optional encryption.
  • Error handling - All LocalMode functions throw typed errors (ModelLoadError, StorageError, ValidationError) with actionable hints. Wrap calls in try/catch and use the error's hint property to display user-friendly messages.
  • Cancellation - Pass an AbortSignal to any long-running operation. This lets users cancel searches, embeddings, or generation without waiting for completion.

Production Considerations

When deploying this solution to production, consider these factors:

Model preloading: Download models during user onboarding or application setup, not on first use. Use preloadModel() with an onProgress callback to show download progress. This avoids the poor experience of a loading spinner on the first AI interaction.

Storage management: IndexedDB has browser-specific quotas (up to 60% of total disk size per origin on Chrome, more restrictive on iOS Safari). Use getStorageQuota() to check available space and navigator.storage.persist() to request persistent storage that survives browser storage pressure.

Device adaptation: Not all users have the same hardware. Use detectCapabilities() and recommendModels() to select models appropriate for each user's device - call recommendModels(caps, { task }) with the detected capabilities. A desktop with a discrete GPU can handle 3GB models; a mobile phone with 3GB RAM should use models under 300MB.

Error boundaries: Wrap AI-powered components in error boundaries. If model loading fails (network error, storage quota exceeded, incompatible browser), fall back gracefully - show the non-AI version of the feature rather than crashing the page.

Frequently Asked Questions

LocalMode provides information extraction tools, not legal advice. The extracted entities and classifications should be reviewed by qualified legal professionals. The tool accelerates review - it doesn't replace human judgment.

Can it handle multiple languages?

The NER model works best with English text. For multilingual contracts, use paraphrase-multilingual-MiniLM (50+ languages) for semantic search and embeddings. Classification accuracy may vary by language.

Further Reading

Methodology

Code examples were verified against the exported functions in @localmode/core (v2.x) and @localmode/transformers source. classifyZeroShot return shape ({ labels, scores }) was confirmed against packages/core/src/classification/types.ts. The IndexedDB quota figure was verified against MDN Web Docs and web.dev (Google). Attorney-client privilege statements are general information only and are not legal advice - consult a qualified attorney for your specific situation.

Sources