Platform & Cloud
Freelance client
AI-Powered Legal Document Analysis API
FastAPI · OpenAI · Supabase · Redis · Nginx · pdfplumber · asyncio

99.95%API Uptime Over 6 Months
01
The Challenge
Legal documents are large and structurally complex. Some case files reached 300 pages with deeply nested section numbering, cross-references between paragraphs, and footnotes that reference other footnotes. Sending the full text to the OpenAI API in a single call hit token limits and cost $2 to $3 per document. The attorneys needed results in under 10 seconds, but a single summarization call was taking 30 to 45 seconds. Naive PDF text extraction using PyPDF2 stripped all structural information: paragraph numbers disappeared, section headers merged into body text, and footnotes appeared inline where they made no sense. When the LLM received this unstructured blob, it could not distinguish between the holding of a case and a footnote citing a tangentially related statute. The resulting summaries were frequently misleading. The previous system had no caching, so attorneys querying the same landmark case ten times in a week generated ten identical $2.80 API calls. There was no rate limiting either, and during a busy trial week, one paralegal accidentally triggered 4,000 API calls in an hour with a batch script.
02
The Approach
The backend has four layers: extraction, summarization, caching, and access control. The extraction layer uses pdfplumber because it exposes the underlying PDF page layout, including character positions, line objects, and table structures. I wrote a custom extractor that identifies section headers by font size, maps paragraph numbers by indentation pattern, and builds a footnote reference table by matching superscript characters to footnote text. The output is a structured JSON document where each section is a separate object with number, title, body text, and footnote references. The summarization layer uses a map-reduce pattern. Each section runs as a separate task concurrently using
asyncio.gather with a semaphore limiting concurrency to 12 simultaneous OpenAI API calls. Section summaries combine in a reduce step producing the final analysis. Total latency dropped from 40 seconds to 8 seconds. Per-document cost dropped from $2.80 to $0.35. The caching layer uses Redis with content-addressed keys (SHA-256 hash of the PDF) and a 24-hour TTL. Cache hits return in under 50 milliseconds. Access control uses JWT tokens signed with RS256. Three roles (attorney, paralegal, admin) have different rate limits enforced via sliding window in Redis. The FastAPI application runs behind Nginx with SSL termination, request buffering for large uploads, and gzip compression. Structured JSON logging with correlation IDs enables end-to-end request tracing.Interactive Demo
Walk through the system design and data flow for this project step by step.
Legal Document Processing Pipeline
04
Overview
Built the backend API for a legal research platform that processes case files up to 300 pages and returns structured summaries in under 8 seconds. The system uses pdfplumber for structure-preserving PDF extraction that maintains paragraph numbers, section headers, and footnote cross-references. A map-reduce summarization pipeline chunks documents by section, runs parallel OpenAI API calls using
asyncio.gather, and combines the section summaries into a final analysis. Redis caches analysis results with a 24-hour TTL so repeated queries on the same document are instant. JWT authentication with RS256 signing supports three roles (attorney, paralegal, admin), each with different rate limits enforced through a sliding window algorithm.05
Business Impact
The API handles roughly 800 document analyses per day with a p99 latency of 9.2 seconds for a 200-page case file. Before this system, lawyers spent an average of 45 minutes per case file doing manual read-throughs. The map-reduce approach reduced per-document API costs from $2.80 to $0.35 (chunked parallel calls with smaller context windows). Redis caching eliminates repeat processing entirely, and cache hit rate averages seventy two percent. The API maintained 99.95 percent uptime over six months. Structured JSON logging with correlation IDs allowed the team to trace a production bug to a specific pdfplumber edge case within 20 minutes.
06
Technical Highlights
- pdfplumber extraction preserving paragraph numbers, section headers, numbered clauses, and footnote-to-reference mappings
- Map-reduce summarization with asyncio.gather running up to 12 parallel OpenAI API calls per document, reducing latency from 40 seconds to 8 seconds
- Redis caching with 24-hour TTL and document content hash keys, achieving 72% cache hit rate on repeated case law queries
- JWT authentication with RS256 signing and three roles: attorney (200 req/min), paralegal (50 req/min), admin (unlimited)
- Sliding window rate limiting per role implemented in Redis, returning 429 with Retry-After header on throttle
- Supabase PostgreSQL with PgBouncer connection pooling for document metadata, user sessions, and analysis history
- Structured JSON logging with correlation IDs on every request, enabling end-to-end request tracing across async workers
- Health check endpoint verifying database connectivity, Redis availability, and OpenAI API status with degraded mode support
- Webhook notifications via POST callback when long-running analyses (300+ page documents) complete asynchronously
- Nginx reverse proxy with SSL termination, request body buffering for large PDF uploads, and gzip compression on API responses
Want results like this for your infrastructure?
I specialize in taking complex AI pipelines and cloud setups from concept to high-availability production. Let's discuss how to optimize your workloads, secure your environment, and reduce cloud costs.