Legal AIRAG ArchitectureCase Study

AI-Powered Legal Document Analysis Platform — LPA Analyzer

How I built a production AI system that processes 150-200 page Limited Partnership Agreements in minutes, extracting 6 critical clause types with RAG-powered analysis and a Microsoft Word Add-in — eliminating 70%+ of manual review time for an investment fund law practice.

By Nic Chin12 min read

Investment fund lawyers spend 4–8 hours per LPA manually reviewing 150–200 page documents, hunting for key clauses buried across dozens of sections. They cross-reference definitions scattered throughout the agreement, compare terms against market standards from memory, and flag risks based on years of pattern recognition. When a fund is raising capital, a single lawyer might review 10–20 LPAs in a month — that's 40–160 hours of dense, repetitive analytical work.

The firm needed a system that could read an entire LPA, extract the clauses that matter, analyse them against market standards, and surface risks — without the lawyer ever leaving Microsoft Word. Not a generic document chatbot. A purpose-built legal analysis engine that understands investment fund terminology, cross-references, and the specific clause categories that drive deal negotiations.

What I Built

A production AI legal analysis platform that transforms how lawyers review Limited Partnership Agreements. Two interfaces — a web dashboard for document management and a Microsoft Word Add-in for in-document analysis — powered by a unified RAG backend that learns from the firm's own document library.

The core challenge: Legal documents aren't like blog posts or emails. A single clause can span 3 pages, reference 15 defined terms from other sections, and contain nested exceptions that completely change its meaning. The system had to parse document structure intelligently, chunk without breaking legal context, and deliver analysis that a senior fund lawyer would trust enough to use in client negotiations.

The 3 Core Systems

1. Intelligent Document Processing Engine

Takes a raw .docx LPA (150–200 pages), parses its structure, and extracts every instance of 6 critical clause categories with confidence scoring.

The pipeline starts with TOC extraction using 4 regex patterns to map the document's structure. A structure analyser identifies section boundaries, headings, and clause-type hints. Smart chunking then splits the document into ~4,000-character segments that respect section boundaries — never cutting a clause mid-sentence. Each chunk is queued as an independent background job via QStash, enabling 100+ chunks to process in parallel across serverless functions.

A two-pass extraction strategy maximises recall: Phase 1 uses TOC navigation for targeted extraction from likely sections (high confidence). Phase 2 performs comprehensive fallback extraction for anything missed, handling oversized sections (>15,000 chars) that Phase 1 intentionally skips for speed.

Result: Upload a 200-page LPA. Walk away. Come back in a few minutes to find every Key Person clause, every GP Removal provision, every Waterfall distribution term — extracted, categorised, and ready for analysis. No manual searching. No missed clauses buried on page 147.

2. RAG-Powered Clause Analysis

Takes any extracted clause and generates a 4-part legal analysis — using the firm's entire LPA library as context.

When a lawyer clicks “Analyse” on a clause, the system checks the analysis cache (SHA-256 hash of clause text, 7-day TTL). On cache miss, it calls a Dify workflow that queries the Knowledge Base (top-k: 10 chunks from the firm's LPA corpus). GPT-4o generates a structured analysis with cross-document context, parsed into 4 sections and cached for future use.

The 4-part analysis framework was designed around how senior fund lawyers actually think:

  • Summary: 2–3 sentences in plain English with section citations — what a partner would say in a 30-second elevator brief
  • Detailed Analysis: Thresholds, triggers, consequences, and fully unpacked cross-references — the substance a senior associate needs
  • Key Considerations: Market standard comparison (e.g., “GP removal typically requires 50–75% LP vote”), LP vs GP favourability assessment, negotiation leverage points, and red flags
  • Definitions: Every defined term referenced in the clause, with full definitions and source locations from the LPA

Each clause type has calibrated focus areas. For Waterfall clauses, the system benchmarks against typical 8% hurdle rates and 80/20 LP/GP carried interest splits. For Key Person clauses, it evaluates time commitment requirements and replacement procedures. For GP Removal, it flags voting thresholds outside the 50–75% market standard.

What this means in practice: You don't just see that a GP Removal clause exists — you see that it requires an 80% LP vote (above market standard), has no cure period (unusual), and the consequences section doesn't address carried interest on existing deals (a gap the other side's counsel will exploit). In seconds, not hours.

3. Microsoft Word Add-in

Brings the full power of the analysis platform directly into the lawyer's document — no tab switching, no copy-pasting, no workflow disruption.

Built with Office.js and React, the Add-in installs as a sidebar panel in Microsoft Word. When a lawyer opens an LPA, they click “Analyse” and the plugin extracts the full document text, sends it to the backend, and renders results in an organised panel grouped by clause category.

Each clause category gets a distinct colour:

  • Key Person (Yellow): Time commitments, named individuals, replacement triggers
  • GP Removal (Blue): Voting thresholds, cause definitions, cure periods
  • Term & Termination (Green): Partnership term, extension mechanics, dissolution events
  • Waterfall (Purple): Distribution priorities, hurdle rates, catch-up provisions
  • Clawback (Red): Trigger events, calculation methods, escrow requirements
  • LPAC (Grey): Committee composition, conflict resolution, advisory powers

Click any clause in the sidebar, and the plugin navigates to its exact location in the document with highlighting applied. Expand a clause for one-click access to AI analysis, drafting suggestions, and cross-document context — all rendered inline without leaving Word.

Architecture

The web dashboard and Word Add-in share the same API layer and RAG backend. When a document is uploaded through the dashboard, it's simultaneously indexed in the Dify Knowledge Base, making its content available for cross-document analysis via the Word plugin. Every query — whether from the web UI or Word — passes through the same multi-tenant isolation layer, analysis cache, and quality validation pipeline.

QStash handles the heavy lifting asynchronously: document uploads queue parallel chunk-processing jobs that execute across Vercel's serverless infrastructure, with real-time progress updates written atomically to the database. The lawyer sees a live progress bar while 100+ extraction jobs run in parallel behind the scenes.

Technical Decisions Worth Noting

Why parallel chunk processing over sequential: A 200-page LPA produces ~50 chunks at 4,000 chars each. Sequential processing would take 25–50 minutes (30–60s per Dify workflow call). Parallel processing via QStash completes the same work in minutes. The trade-off is infrastructure complexity — atomic progress tracking, deduplication logic, and rate-limit management — but the user experience difference is dramatic.

Why a Microsoft Word Add-in over a standalone web app: Lawyers live in Word. An analysis tool that requires copying text into a browser, waiting for results, then switching back is dead on arrival. The Add-in keeps everything in context — the clause, the surrounding language, the analysis, and the drafting suggestions — in one view. Adoption becomes frictionless because the tool meets the user where they already are.

Why TOC-aware chunking over naive splitting: Naive text splitting (every N characters) routinely breaks clauses mid-sentence or separates a provision from its definitions. TOC-aware chunking maps the document's logical structure first, then creates chunks that respect section boundaries. The result: each chunk contains a complete legal thought, and the AI never analyses half a clause with no context for the other half.

Why multi-tenant isolation at the database level: For a system handling confidential legal documents across multiple law firm teams, I chose explicit organisation_id enforcement on every table, every query, and every index. Composite indexes starting with organisation_id ensure tenant-scoped queries use index scans, not sequential scans. CASCADE deletes ensure clean data removal when a tenant leaves. Defence-in-depth for data that cannot leak.

Why Dify Cloud over a custom RAG pipeline: Dify provides workflow orchestration, knowledge base management, model routing, and zero-retention processing out of the box. Building this from scratch would add 4–6 weeks to the timeline for infrastructure that doesn't differentiate the product. The value is in the legal domain logic — the prompts, the clause-type calibration, the market-standard benchmarking — not the vector database plumbing.

Key Results

70%+ manual LPA review time eliminated. 4–6 hours per document reduced to minutes with parallel processing.

6 critical clause types extracted automatically — Key Person, GP Removal, Waterfall, Clawback, Term & Termination, LPAC — with confidence scoring and page references.

100+ parallel chunk processing jobs via QStash across Vercel serverless — two-pass extraction with TOC-aware parsing for maximum recall.

RAG pipeline with Dify Cloud + pgvector retrieves cross-document context from the firm's full LPA corpus (top-k: 10 chunks).

~60% API cost reduction through 7-day intelligent caching with SHA-256 clause hashing on repeat queries.

AI-generated drafting suggestions with issue identification, recommended language, and legal rationale — prioritised by severity (critical, high, medium, low).

Zero-retention AI processing across all LLM providers — no client documents stored by third-party AI services, meeting law firm data governance requirements.

Security & Compliance

Multi-tenant data isolation with organisation-scoped queries on all 11 tables. Zero-retention configuration across all LLM providers. Clerk authentication with SSO, MFA, and organisation management. Full audit trail with user, resource, and timestamp tracking. TLS in transit, encrypted at rest. Role-based access control (admin, member, viewer) per organisation. QStash signature verification and CORS enforcement on all API routes.

Why This Matters for Your Business

This project demonstrates a production-grade approach to AI in regulated industries. The same architecture applies to:

  • Any document-heavy practice that reviews long-form agreements (M&A, real estate, insurance, compliance)
  • Any regulated industry requiring zero-retention AI processing and full audit trails
  • Any professional services firm where analysts spend hours on pattern recognition that AI can accelerate
  • Any team that needs AI intelligence embedded into existing tools (Word, Excel, Outlook) rather than forcing adoption of new platforms

Investment range: $8,500–$15,000 depending on clause complexity and integration depth. Monthly operating cost: ~$150–300 for all AI APIs, database, and hosting combined. ROI: 4–6 hours saved per LPA review × 10–20 LPAs/month = 40–120 hours recovered. At $300–500/hour associate rates, that's $12,000–60,000/month in recovered capacity.

Tech Stack

  • AI/LLM: Dify Cloud (GPT-4o, multi-model routing), OpenAI Embeddings (ada-002), pgvector
  • Framework: Next.js 15, React 19, TypeScript, App Router
  • Database: Neon PostgreSQL, pgvector (1536-dim), multi-tenant schema
  • UI: Tailwind CSS, Radix UI, Framer Motion, next-themes
  • Word Plugin: Office.js, React, TypeScript, Webpack
  • Auth: Clerk (SSO, organisation management, webhook sync)
  • Background Jobs: QStash (Upstash) with retry logic and rate limiting
  • Document Processing: Mammoth (.docx parser), custom structure analyser, targeted extractor
  • Deployment: Vercel (edge + serverless), Neon (managed PostgreSQL)
  • Security: Zero-retention AI, multi-tenant isolation, audit logging, HTTPS-only

Related reading: For a deep-dive into the RAG architecture patterns used in this system, see my production guide to RAG architecture. For how this kind of AI system fits into a broader business strategy, read AI automation ROI for enterprises.

Ready to discuss your AI project?

Book a free 30-minute discovery call to explore how AI can transform your business.

Book Discovery Call