Skip to main content

What you’ll build

You’ll build an intelligent document compliance workflow that analyzes contracts and legal documents against your compliance policies, flags issues, and generates structured review reports. The system uses RAG (Retrieval-Augmented Generation) to ground compliance checks in your actual policies and optionally leverages private models for sensitive documents that never leave your infrastructure. This workflow demonstrates how to:
  • Analyze contracts for compliance with company policies and regulations
  • Use parallel agents for comprehensive document analysis
  • Implement RAG for accurate compliance policy lookup
  • Deploy private models for sensitive legal documents
  • Build human-in-the-loop approval for flagged documents
  • Generate structured compliance reports with clear audit trails

What the system analyzes

Document types:
  • Vendor contracts and MSAs (Master Service Agreements)
  • Customer agreements and SLAs
  • Employment contracts
  • NDAs (Non-Disclosure Agreements)
  • Data Processing Agreements (DPAs)
  • Partnership agreements
  • License agreements
Compliance checks:
  • Company policy compliance (payment terms, liability limits, IP rights)
  • Regulatory compliance (GDPR, HIPAA, SOX, industry-specific)
  • Standard clause requirements
  • Risk assessment (financial, legal, operational)
  • Missing or non-standard clauses

Prerequisites

Before you begin, ensure you have:
  • MagOneAI instance with workflow builder and knowledge base
  • Compliance policy documents:
    • Company contract standards
    • Regulatory guidelines (GDPR, HIPAA, etc.)
    • Standard clause libraries
    • Approved clause language templates
    • Risk assessment frameworks
  • LLM provider configured:
    • Cloud option: GPT-4, Claude 3.5 Sonnet (for non-sensitive documents)
    • Private option: Self-hosted model (Llama 3, Mistral, Qwen) for sensitive contracts
  • Email tool configured for notifications
  • Document management integration (optional) - SharePoint, Google Drive, etc.
For sensitive legal documents, we strongly recommend deploying a private model on your own infrastructure. This ensures contracts never leave your network and maintains attorney-client privilege.

Architecture

The compliance review workflow uses parallel analysis with RAG-powered policy checks:

Why this architecture works

Accurate Compliance

RAG grounds compliance checks in actual policies, preventing hallucinated requirements

Comprehensive Analysis

Three parallel specialist agents analyze different dimensions simultaneously

Private & Secure

Deploy on your infrastructure to maintain confidentiality and attorney-client privilege

Audit-Ready Reports

Structured reports with clause citations and policy references for compliance audits

Step-by-step build

1

Create compliance policy knowledge base

Build a knowledge base of your compliance policies and standards.Gather policy documents:
  1. Company Contract Standards
    • Payment terms policy (e.g., Net 30, no upfront payments >20%)
    • Liability limits (e.g., cap at contract value, no unlimited liability)
    • IP ownership requirements
    • Termination clause requirements
    • Indemnification standards
    • Data protection requirements
    • Warranty limitations
  2. Regulatory Compliance Guides
    • GDPR compliance checklist for contracts
    • HIPAA requirements for healthcare data
    • SOX requirements for financial controls
    • Industry-specific regulations (finance, healthcare, etc.)
    • Data residency requirements
    • Cross-border data transfer requirements
  3. Standard Clause Library
    • Approved confidentiality clauses
    • Standard limitation of liability language
    • IP assignment clauses
    • Termination for convenience clauses
    • Dispute resolution clauses
    • Force majeure clauses
  4. Risk Assessment Framework
    • Financial risk thresholds
    • Legal risk categories (low, medium, high, critical)
    • Operational risk indicators
    • Reputational risk factors
    • Risk mitigation requirements
Create the knowledge base:
  1. Navigate to Knowledge BasesCreate New
  2. Name: “Contract Compliance Policies”
  3. Upload documents:
    • Company_Contract_Standards.pdf
    • GDPR_Compliance_Checklist.pdf
    • HIPAA_Contract_Requirements.pdf
    • Standard_Clauses_Library.pdf
    • Risk_Assessment_Framework.pdf
    • IP_and_Confidentiality_Policy.pdf
  4. Configure chunking:
    • Strategy: Automatic or semantic (clause-based)
    • Chunk size: 500-1000 tokens
    • Overlap: 100 tokens
  5. Add metadata:
    • Policy type (e.g., “payment”, “liability”, “data-protection”)
    • Regulatory authority (e.g., “GDPR”, “HIPAA”, “internal”)
    • Effective date
    • Severity (e.g., “mandatory”, “recommended”, “best-practice”)
  6. Wait for processing to complete
Effective policy document structure:
This structure helps RAG retrieve specific requirements and their severity.
2

Create the document extraction agent

Build an agent to extract and structure contract content.Agent configuration:Name: Contract Extraction Agent Model: GPT-4, Claude 3.5 Sonnet, or private model (Llama 3, Qwen)Persona:
Configuration:
  • Temperature: 0.1 (very low for accurate extraction)
  • Structured output: Enabled (JSON schema)
  • Max tokens: 4000 (for long contracts)
  • Timeout: 60 seconds
3

Create the three specialist analysis agents

Build agents for clause analysis, compliance checking, and risk assessment.

1. Clause Analysis Agent

Name: Clause Categorization Agent Model: GPT-4, Claude 3.5 Sonnet, or private modelPersona:

2. Compliance Check Agent (with RAG)

Name: Compliance Verification Agent Model: GPT-4, Claude 3.5 Sonnet, or private model Knowledge Base: Contract Compliance Policies (RAG enabled)Persona:
Configuration:
  • RAG enabled: Yes
  • Knowledge base: Contract Compliance Policies
  • Retrieval: 10 chunks, threshold 0.75
  • Temperature: 0.2

3. Risk Assessment Agent

Name: Contract Risk Assessor Model: GPT-4, Claude 3.5 Sonnet, or private modelPersona:
4

Create the review synthesis agent

Build an agent to compile all analysis into a comprehensive report.Name: Compliance Review Coordinator Model: GPT-4 or Claude 3.5 SonnetPersona:
Configuration:
  • Temperature: 0.3
  • Max tokens: 3000
  • Structured output: Both Markdown and JSON
5

Build the compliance review workflow

Construct the complete workflow with all agents.Node 1: Trigger
  • Type: Manual Trigger or API Trigger
  • Inputs:
    • contract_document (PDF or DOCX file)
    • contract_name (text)
    • counterparty (text)
    • contract_type (dropdown: vendor, customer, partner, NDA, DPA, other)
    • contract_value (number, optional)
    • reviewer_name (text)
    • reviewer_email (text)
Node 2: Document Extraction Agent
  • Agent: Contract Extraction Agent
  • Input: {{trigger.contract_document}}
  • Output variable: extraction
Node 3: Parallel Analysis
  • Branch 1: Clause Analysis Agent
    • Input: {{extraction}}
    • Output: clause_analysis
  • Branch 2: Compliance Check Agent (with RAG)
    • Input: {{extraction}}
    • Enable RAG: Yes
    • Output: compliance_check
  • Branch 3: Risk Assessment Agent
    • Input: {{extraction}}
    • Output: risk_assessment
Node 4: Review Synthesis Agent
  • Agent: Compliance Review Coordinator
  • Inputs:
  • Output variable: review_report
Node 5: Condition - Issues Found?
  • Condition:
  • True branch: Human review required
  • False branch: Auto-approve (or lower-level approval)
Node 6a (True branch): Human Task - Legal Review
  • Title: Contract Compliance Review Required
  • Description:
  • Task type: Approval
  • Assignee: A legal reviewer’s user ID (a template like {{system.user_id}} also works; assign one or more reviewers)
  • Context fields: Surface the extracted contract data and the compliance review report for the reviewer (the previous activity output is included automatically)
  • Decision question: A choice question with options Approve, Reject, and Request Changes, wired to approval branches
Node 6b (False branch): Auto-Approve Notification
  • Email Tool
  • To: {{trigger.reviewer_email}}
  • Subject: Contract Compliance Review Complete - Approved
  • Body:
Node 7: Final Notification
  • Email Tool (both branches converge here)
  • To: {{trigger.reviewer_email}}, CC: Legal team (if reviewed)
  • Subject:
  • Body:
  • Attachments:
    • Compliance review report (PDF)
    • Original contract (PDF)
6

Test with sample contracts

Validate the workflow with various contract scenarios.Test Case 1: Compliant vendor contract
  • Sample: Standard vendor MSA with Net 30 terms, reasonable liability cap, clear IP rights
  • Expected:
    • Extraction: All key clauses identified correctly
    • Compliance: All policies met
    • Risk: LOW risk rating
    • Outcome: Auto-approved
  • Verify:
    • ✅ Accurate extraction
    • ✅ Compliance checks reference correct policies
    • ✅ Risk assessment is reasonable
    • ✅ Report is clear and actionable
    • ✅ Auto-approval workflow triggers
Test Case 2: Non-compliant contract (payment terms)
  • Sample: Contract with Net 90 payment terms (exceeds policy)
  • Expected:
    • Extraction: Payment terms extracted correctly
    • Compliance: Flagged as non-compliant (Net 90 > Net 45 max)
    • Policy citation: “Payment Terms Policy, Section 2.1”
    • Outcome: Escalated to CFO approval
  • Verify:
    • ✅ Payment terms flagged correctly
    • ✅ Policy citation is accurate
    • ✅ Severity marked appropriately
    • ✅ Human Task created with context
Test Case 3: High-risk contract (unlimited liability)
  • Sample: Contract with unlimited liability clause
  • Expected:
    • Extraction: Liability clause extracted
    • Compliance: Non-compliant with liability policy
    • Risk: CRITICAL risk (unlimited liability)
    • Outcome: Escalated to legal review
  • Verify:
    • ✅ Unlimited liability flagged as critical
    • ✅ Risk assessment explains exposure
    • ✅ Mitigation recommendations provided
    • ✅ Human Task assigned to legal team
Test Case 4: GDPR data processing agreement
  • Sample: DPA with GDPR requirements
  • Expected:
    • Extraction: Data protection clauses identified
    • Compliance: GDPR checklist verification
    • Risk: Data breach liability assessed
    • Outcome: Privacy officer approval required
  • Verify:
    • ✅ GDPR requirements checked against KB
    • ✅ Missing clauses identified (if any)
    • ✅ Data residency requirements verified
    • ✅ Appropriate routing to privacy officer
Test Case 5: Missing standard clauses
  • Sample: Contract missing force majeure, dispute resolution clauses
  • Expected:
    • Extraction: Identifies missing sections
    • Compliance: Flags missing standard clauses
    • Risk: MEDIUM risk (missing protections)
    • Outcome: Legal review for clause additions
  • Verify:
    • ✅ Missing clauses detected
    • ✅ Recommended additions listed
    • ✅ Explanation of why clauses are important
For legal documents, accuracy is paramount. Always have a legal professional review AI-generated compliance reports initially to ensure they align with your legal standards and risk tolerance.
7

Deploy private model for sensitive contracts (optional)

For maximum security and confidentiality, deploy a private model.Why private models for legal documents:
  • Contracts never leave your infrastructure
  • Maintain attorney-client privilege
  • Comply with data residency requirements
  • Control over model and data
  • No vendor dependencies
Recommended private models:
  • Llama 3 70B (strong reasoning, good for legal analysis)
  • Qwen 2.5 72B (excellent multilingual, good for contracts)
  • Mistral Large (competitive with GPT-4, self-hostable)
Deployment approach:
  1. Infrastructure: Deploy on your own GPU servers or private cloud
  2. Model serving: Use vLLM, TensorRT-LLM, or HuggingFace TGI
  3. Integration: Connect to MagOneAI via API endpoint
  4. Configuration: Select private model in agent settings
  5. Testing: Validate performance on legal documents
Performance considerations:
  • Latency: Self-hosted may be slower (acceptable for async workflows)
  • Accuracy: Test thoroughly against your compliance policies
  • Cost: Higher upfront infra cost, lower per-usage cost
  • Maintenance: Requires ML ops expertise
You can use a hybrid approach: cloud models for low-sensitivity contracts, private models for high-value or highly confidential agreements.

Key concepts demonstrated

RAG with Policy Documents

Ground compliance checks in actual policies using knowledge base retrieval

Private Model Deployment

Deploy models on your infrastructure for maximum security and confidentiality

Parallel Analysis

Run three specialist agents simultaneously for comprehensive contract review

Human-in-the-Loop

Escalate high-risk or non-compliant contracts to legal experts with full context

Structured Compliance Reporting

Generate audit-ready reports with clause citations and policy references

Conditional Routing

Route based on compliance status and risk level for appropriate handling

Customization ideas

Extend your contract compliance workflow:
Automatically suggest contract edits:Redlining Agent:
  • Receives: Non-compliant clauses
  • Retrieves: Approved clause language from knowledge base
  • Generates: Suggested redline changes (track changes format)
  • Outputs: Marked-up document with proposed revisions
Implementation:
  • Add Redlining Agent after compliance check
  • Use RAG to retrieve approved clause templates
  • Generate Word document with track changes
  • Include justifications for each change
  • Attach to legal review task
Benefits:
  • Accelerate contract negotiation
  • Ensure consistent clause language
  • Reduce back-and-forth with legal team
  • Educate stakeholders on compliance requirements
Connect to your document repositories:Document management integration:
  • SharePoint, Google Drive, Box, Dropbox
  • Auto-trigger when contract uploaded to specific folder
  • Save compliance reports back to document library
  • Tag documents with compliance status and risk level
  • Create version history with review checkpoints
Workflow enhancements:
  • Trigger: Document uploaded to “Contracts Under Review” folder
  • Process: Run compliance review automatically
  • Output: Save report to same folder, tag document
  • Notification: Alert legal team if review required
  • Archive: Move to “Approved Contracts” folder after clearance
Benefits:
  • Seamless integration with existing processes
  • Centralized contract repository with compliance metadata
  • Easy audit trail and document tracking
Track and analyze contract changes:Version Comparison Workflow:
  • Input: Original contract + Amendment/revised version
  • Extract: Changes between versions
  • Analyze: Impact of changes on compliance and risk
  • Flag: Material changes requiring re-review
  • Report: Side-by-side comparison with risk assessment
Implementation:
  • Add Document Comparison Agent
  • Use diff algorithms to identify changes
  • Re-run compliance checks on changed clauses only
  • Highlight: New risks or compliance issues introduced
  • Output: Amendment impact report
Use cases:
  • Contract amendments and addenda
  • Renewal with modified terms
  • Vendor-proposed changes during negotiation
  • Annual contract updates
Tailor compliance checks by industry:Healthcare (HIPAA):
  • Business Associate Agreement (BAA) verification
  • PHI handling requirements
  • Data breach notification timelines
  • Subcontractor HIPAA compliance
  • Audit and inspection rights
Finance (SOX, PCI-DSS):
  • SOX controls for financial data
  • PCI-DSS for payment data handling
  • Audit rights and record retention
  • Third-party risk management
  • Financial controls verification
Technology (SaaS):
  • SLA verification (uptime, support response times)
  • Data portability and export rights
  • API access and integration rights
  • Scaling and usage limits
  • Open source licensing compliance
Implementation:
  • Create industry-specific knowledge bases
  • Add specialized compliance agents per industry
  • Route based on contract type or industry tag
  • Include industry best practices and benchmarks
Extend beyond initial review to full lifecycle:Pre-signature:
  • Compliance review (this workflow)
  • Redlining and negotiation tracking
  • Approval routing
Post-signature:
  • Extract key dates (renewal, termination notice deadlines)
  • Set calendar reminders for important dates
  • Monitor compliance with ongoing obligations
  • Track deliverables and SLA compliance
Renewal management:
  • Alert 90 days before renewal
  • Re-review contract for continued compliance
  • Compare with current policies (may have updated)
  • Recommend renewal, renegotiation, or termination
Implementation:
  • Add workflow triggers for contract milestones
  • Integrate with calendar and task management
  • Create compliance monitoring sub-workflows
  • Build renewal review automation
Improve extraction accuracy with trained models:Approach:
  • Train a clause classification model on your contracts
  • Fine-tune on your specific contract types and language
  • Achieve higher accuracy than generic LLMs
Implementation:
  • Collect 100-500 sample contracts (annotated)
  • Label key clauses and clause types
  • Fine-tune a model (BERT, RoBERTa, or LegalBERT)
  • Deploy as pre-processing before LLM analysis
  • Use extracted clauses as structured input to analysis agents
Benefits:
  • More accurate clause identification
  • Faster processing (lighter model for extraction)
  • Better handling of non-standard contract formats
  • Improved consistency across reviews
Tools:
  • LegalBERT (legal domain pre-trained)
  • Longformer (handles long documents)
  • Custom fine-tuned models
Create negotiation guidance based on past deals:Contract Playbook KB:
  • Past negotiation outcomes (what we accepted/rejected)
  • Fallback positions by clause type
  • Approved alternative clause language
  • Decision trees for common scenarios
  • Escalation thresholds
Playbook Agent:
  • Receives: Non-compliant or risky clause
  • Retrieves: Past similar situations and outcomes
  • Suggests: Negotiation strategy and alternative language
  • Estimates: Likelihood of acceptance based on history
Implementation:
  • Create knowledge base of negotiation playbooks
  • Add Negotiation Advisor Agent
  • Enable RAG on playbook KB
  • Include in compliance report: suggested negotiation approach
  • Track outcomes to improve playbook over time
Benefits:
  • Consistent negotiation positions
  • Faster deal cycles (clear guidance)
  • Capture institutional knowledge
  • Reduce legal bottlenecks

Example compliance report

Here’s what a complete compliance review looks like:

Measuring success

Track these metrics to demonstrate value: Efficiency metrics:
  • Time to complete compliance review (AI vs. manual)
  • Number of contracts reviewed per week
  • % of contracts auto-approved (low-risk)
  • Legal team time saved
Quality metrics:
  • Compliance issue detection rate
  • False positive rate (flagged but not actually issues)
  • Consistency of reviews across reviewers
  • Audit findings related to contracts
Risk metrics:
  • % of contracts with critical risks identified
  • Number of non-compliant contracts prevented from signature
  • Financial exposure avoided (liability caps negotiated)
  • Regulatory compliance rate
Business impact:
  • Contract cycle time reduction
  • Legal bottleneck reduction
  • Consistency across business units
  • Knowledge capture and reuse
Example success metrics after 6 months:

Next steps

Now that you’ve built a contract compliance workflow, explore related cookbooks:
Need help customizing this for your specific compliance requirements, regulations, or contract types? Contact our solutions team for legal automation guidance.