Building an Autonomous Legal Contract Auditor with Python

Published: (January 9, 2026 at 08:55 PM EST)
4 min read
Source: Dev.to

Source: Dev.to

I built a Python‑based autonomous agent that acts as a preliminary legal auditor. It scans service agreements, cross‑references them with a set of corporate compliance policies (e.g., payment terms, liability caps), and generates a risk‑assessment report. This isn’t just a script; it’s a glimpse of how agentic workflows can tackle real‑world business bottlenecks.

Source Code:

Why a First‑Pass AI Auditor?

In my experience working with various project teams, the Legal Review phase is often where agility dies. It’s not the lawyers’ fault—they’re simply overwhelmed. I asked myself:

“What if we could have a first‑pass AI auditor that flags the obvious stuff?”

The goal isn’t to replace lawyers but to empower engineers or product managers to fix glaring issues (e.g., a missing governing‑law clause) before involving legal counsel.

What the Tool Does

A command‑line interface (CLI) that:

  1. Ingests a contract (simulated as plain text).

  2. Parses it against a “Rule Database” (e.g., Payment Terms).

    Example interaction:

    CLI: Submit contract text
    CLI->>Engine: Load rules & evaluate contract
    Engine->>DB: Retrieve rule definitions
    Engine-->>CLI: Return findings & score
    CLI->>User: Display compliance report

The agent loops through each rule, allowing granular reporting on specific failures rather than a generic “Bad Contract” error.

Project Structure

autonomous-legal-auditor/
├─ main.py                # CLI entry point
├─ compliance/
│   ├─ engine.py          # Rule evaluation logic
│   └─ rules.yaml         # Rule definitions (data, not code)
├─ models/
│   └─ rule.py            # dataclass for a compliance rule
├─ utils/
│   └─ display.py         # Rich UI helpers
└─ requirements.txt

Core Code Snippets

1️⃣ Rule Definition (dataclass)

# models/rule.py
from dataclasses import dataclass

@dataclass
class ComplianceRule:
    id: str
    category: str
    description: str
    severity: str          # "CRITICAL", "HIGH", "MEDIUM", "LOW"
    check_logic: str       # reference to a function name

2️⃣ Sample Rule Database (YAML)

# compliance/rules.yaml
- id: PAY-001
  category: Payment Terms
  description: >-
    Payment terms must not exceed 45 days upon receipt of invoice.
  severity: HIGH
  check_logic: max_days_check
# ... more rules ...

3️⃣ Analysis Engine (simplified)

# compliance/engine.py
from typing import Dict, Any, List
from models.rule import ComplianceRule

class ComplianceEngine:
    def __init__(self, rules: List[ComplianceRule]):
        self.rules = rules

    def analyze_contract(self, contract_text: str) -> Dict[str, Any]:
        results = {"findings": [], "score": 100}

        for rule in self.rules:
            # Simulation: In production, insert LLM or Regex logic here
            has_violation = self._check_rule(rule, contract_text)

            if has_violation:
                results["findings"].append({
                    "rule_id": rule.id,
                    "status": "FAIL",
                    "severity": rule.severity,
                    "detail": f"Violation detected: {rule.description}"
                })
                results["score"] -= self._get_deduction(rule.severity)
            else:
                results["findings"].append({
                    "rule_id": rule.id,
                    "status": "PASS",
                    "severity": rule.severity,
                    "detail": "Compliant."
                })

        return results

    # ---- Helper methods (stubs) ----
    def _check_rule(self, rule: ComplianceRule, text: str) -> bool:
        # Placeholder – replace with real logic/LLM call
        return False

    def _get_deduction(self, severity: str) -> int:
        return {"CRITICAL": 30, "HIGH": 20, "MEDIUM": 10, "LOW": 5}.get(severity, 0)

4️⃣ Rich UI Presentation

# utils/display.py
from rich.console import Console
from rich.table import Table
from rich.progress import Progress

def show_progress():
    console = Console()
    with Progress() as progress:
        task = progress.add_task("[cyan]Parsing natural language...", total=100)
        while not progress.finished:
            progress.update(task, advance=5)

def render_results(results):
    console = Console()
    table = Table(title="Compliance Findings", border_style="blue")
    table.add_column("Rule ID", style="magenta")
    table.add_column("Status", style="green")
    table.add_column("Severity", style="red")
    table.add_column("Detail")

    for f in results["findings"]:
        table.add_row(
            f["rule_id"],
            f["status"],
            f["severity"],
            f["detail"]
        )
    console.print(table)
    console.print(f"[bold]Overall Score:[/] {results['score']}")

5️⃣ CLI Entry Point

# main.py
import sys
from rich.console import Console
from models.rule import ComplianceRule
from compliance.engine import ComplianceEngine
from utils.display import show_progress, render_results
import yaml

def load_rules(path: str) -> list[ComplianceRule]:
    with open(path) as f:
        raw = yaml.safe_load(f)
    return [ComplianceRule(**r) for r in raw]

def main():
    console = Console()
    console.print("[bold cyan]🚀 Autonomous Legal Contract Auditor[/]")

    # Load mock contract (could be replaced with a file argument)
    contract_text = open("sample_contract.txt").read()

    # Load rules
    rules = load_rules("compliance/rules.yaml")
    engine = ComplianceEngine(rules)

    # Show progress animation
    show_progress()

    # Run analysis
    results = engine.analyze_contract(contract_text)

    # Render table
    render_results(results)

if __name__ == "__main__":
    sys.exit(main())

Getting Started

# Clone the repository
git clone https://github.com/aniket-work/autonomous-legal-auditor.git
cd autonomous-legal-auditor

# Install dependencies
pip install -r requirements.txt

# Run the auditor (uses a mock contract)
python main.py

You should see a lively terminal UI: parsing animation, rule checks, and a final dashboard of compliance stats.

Future Directions

  • Connect to a local LLM for real text extraction and nuanced rule evaluation.
  • Expand the rule set (liability caps, governing law, data‑privacy clauses).
  • Add a web UI for non‑technical stakeholders.

The PoC demonstrates that vertical AI agents—specialized for deep, domain‑specific tasks—are the future. Instead of a generic chatbot, we built a focused tool that “speaks” the language of contracts (payment terms, liability, governing law, etc.).


The views and opinions expressed here are solely my own and do not represent the views of any organization.

The views, positions, or opinions of my employer or any organization I am affiliated with. The content is based on my personal experience and experimentation and may be incomplete or incorrect. Any errors or misinterpretations are unintentional, and I apologize in advance if any statements are misunderstood or misrepresented.

Back to Blog

Related posts

Read more »

Hello, Newbie Here.

Hi! I'm falling back into the realm of S.T.E.M. I enjoy learning about energy systems, science, technology, engineering, and math as well. One of the projects I...