Building a Data Model Validation System for Large-Scale AI Agent Projects

The follow-up to my user flow validation system: How I keep my data models in sync with business requirements using chunked processing for AI agents with limited memory.


The Problem: When Your Data Models Can’t Keep Up

In my previous post, I figured out how to fix incomplete user flow documentation. But that led me to a whole new problem: data model drift.

As my user flows got better and my validation scores went up, I noticed my Django data models were falling behind. Important business entities from my flows were nowhere to be found in my database schema. The relationships I’d described in my workflows weren’t modeled correctly, and status values from my flows didn’t have matching database enums.

Here’s the bottom line: with over 80 user flows, 3 user types, and a massive Django data model covering more than 15 domains, trying to keep everything in sync by hand was a nightmare. To make matters worse, most AI agents just couldn’t handle the amount of context required to do a thorough validation.

The Solution: Chunked Data Model Reconciliation

I came up with a memory-efficient validation system that lets any AI agent—even one with a small context window—systematically check data models against user flows. Here’s a look at how it works and how you can build it yourself.

My Design Principles

The Architecture: A Six-Phase Approach

My system breaks down the massive job of data model validation into six manageable phases:

Phase 1: Extracting Entities and Mapping Domains

Instead of reading huge TOML files, I use targeted searches to find entities:

# Extract core business entities across all flows
python3 scripts/flow_manager.py search "Client" | head -20
python3 scripts/flow_manager.py search "Invoice" | head -20
python3 scripts/flow_manager.py search "Job" | head -20

From these search results, I group entities into logical domains:

This approach uses ~15k tokens instead of 200k+ for reading raw files.

Phase 2: Domain-Specific Deep Analysis

For each domain, I select 3-5 representative flows and extract detailed requirements:

# Billing domain analysis
python3 scripts/flow_manager.py search "invoice" | head -5
python3 scripts/flow_manager.py show create_invoice
python3 scripts/flow_manager.py show send_invoice

Sample extraction from send_invoice flow:

# Entities mentioned: Invoice, Email, Template, PDF, Client
# Status values: 'SENT', 'FAILED', 'BOUNCED'
# Relationships: Invoice belongs_to Client, Invoice has_one EmailDelivery
# Business rules: Invoice must have PDF attachment, Email requires template

This targeted analysis uses ~25k tokens per domain instead of processing everything.

Phase 3: Data Model Verification

I validate requirements against Django models using targeted file reading:

# Extract only relevant model sections
def extract_billing_models(file_content):
    return extract_section_between(
        content,
        "### Billing Models",
        "### Email Models"
    )

Validation example:

DOMAIN RECONCILIATION: Billing

✅ COVERED ENTITIES:
- Invoice model exists (InvoiceStatus.SENT matches flow requirement)
- InvoiceItem model exists with proper relationships

❌ MISSING ENTITIES:
- EmailDelivery model (mentioned in send_invoice flow)

✅ COVERED RELATIONSHIPS:
- Invoice -> Client (ForeignKey)
- Invoice -> InvoiceItem (reverse ForeignKey)

❌ MISSING FIELDS:
- Invoice.email_sent_at (flow tracks delivery confirmation)
- Invoice.delivery_status (flow requires status tracking)

Phase 4: Cross-Domain Integration Analysis

I verify that data flows correctly between domains:

# Find flows spanning multiple domains
python3 scripts/flow_manager.py dependencies create_invoice
python3 scripts/flow_manager.py show new_client_to_first_payment

This ensures entities can properly reference each other across domain boundaries.

Phase 5: Generate Implementation Plan

I aggregate findings into prioritized actions:

DATA MODEL RECONCILIATION - IMPLEMENTATION PLAN

CRITICAL PRIORITY (Breaks Core Workflows):
□ Add Invoice.delivery_status field (affects 8 high-score flows)
□ Add EmailDelivery model for send_invoice flow

HIGH PRIORITY (Quality Impact):
□ Add Client.preferred_communication_method choices
□ Add unique constraint (user, client.company_name)

MEDIUM PRIORITY (Enhancement):
□ Add Invoice.custom_fields JSONField for flexibility

Phase 6: Testing and Migration Guidance

I provide concrete next steps for implementation:

# Example migration for missing field
class Migration(migrations.Migration):
    operations = [
        migrations.AddField(
            model_name='invoice',
            name='delivery_status',
            field=models.CharField(
                max_length=20,
                choices=DeliveryStatus.choices,
                default=DeliveryStatus.PENDING
            ),
        ),
    ]

🛠️ Implementation: Building Your Own System

Step 1: Create a Flow Query Interface

Build a command-line tool for targeted flow searches:

# flow_manager.py
def search_flows(keyword):
    """Search flows without loading entire files"""
    matches = []
    for file_path in flow_files:
        with open(file_path) as f:
            content = f.read()
            if keyword.lower() in content.lower():
                # Extract just the flow names and descriptions
                matches.append(extract_flow_summary(content, keyword))
    return matches

Step 2: Define Domain Boundaries

Map your entities to logical domains:

# domain_mapping.yml
domains:
  user_management:
    entities: [User, UserProfile, Session, Permission]
    flows: [register, login, profile_update]

  content_management:
    entities: [Post, Comment, Tag, Category]
    flows: [create_post, moderate_comment]

Step 3: Build Targeted Model Extraction

Read only relevant sections of your data model:

def extract_domain_models(file_path, domain_name):
    """Extract only models for specific domain"""
    with open(file_path) as f:
        content = f.read()

    start_marker = f"## {domain_name} Models"
    end_marker = f"## {next_domain} Models"

    return extract_section(content, start_marker, end_marker)

Step 4: Create Domain Validation Templates

Standardize your validation reports:

# Domain Validation Template

## Domain: {{domain_name}}
**Primary Entities**: {{entities}}
**Key Flows Analyzed**: {{flows}}

### Entity Coverage
**Found**: {{existing_entities}}
❌ **Missing**: {{missing_entities}}

### Field Coverage
**Implemented**: {{existing_fields}}
❌ **Missing**: {{missing_fields}}

### Relationship Validation
**Correct**: {{valid_relationships}}
❌ **Issues**: {{relationship_problems}}

### Priority Actions
{{prioritized_todo_list}}

Step 5: Implement Progressive Processing

Handle limited context by processing domains sequentially:

def validate_all_domains(max_context_tokens=135000):
    """Validate domains within context limits"""
    results = []

    for domain in domains:
        # Clear context between domains
        context_usage = estimate_tokens()
        if context_usage > max_context_tokens * 0.8:
            save_progress(results)
            clear_context()

        domain_result = validate_domain(domain)
        results.append(domain_result)

    return aggregate_results(results)

🎯 Real-World Results

Validation Effectiveness

I tested the system on my 80-flow, 15-domain project:

Before Validation:

After Validation:

Memory Efficiency Proven

Testing with different AI context limits:

Processing Time

📊 Key Metrics We Track

Model Completeness Score

(Implemented Entities / Required Entities) × 100%
Current: 95% (38/40 entities)

Flow Coverage Accuracy

(Flows with Complete Model Support / Total High-Score Flows) × 100%
Current: 92% (23/25 flows with score ≥ 95)

Cross-Domain Integrity

(Valid Cross-Domain References / Total Cross-Domain References) × 100%
Current: 100% (18/18 relationships)

🔧 Advanced Techniques

Automated Gap Detection

Use flow dependencies to find missing integration points:

# Find flows that reference entities across domains
python3 scripts/flow_manager.py dependencies create_invoice
# Output: Requires Client, TimeEntry, PaymentMethod models

Intelligent Prioritization

Weight missing elements by flow validation scores:

def prioritize_missing_elements(gaps, flows):
    """Priority = sum of validation scores for affected flows"""
    priorities = {}
    for gap in gaps:
        affected_flows = find_flows_using_entity(gap.entity)
        priority_score = sum(f.validation_score for f in affected_flows)
        priorities[gap] = priority_score
    return sorted(priorities.items(), key=lambda x: x[1], reverse=True)

Incremental Validation

Track changes to avoid re-validating everything:

# validation_state.json
{
  "last_validated": "2025-09-22",
  "domain_checksums": {
    "user_management": "abc123",
    "billing": "def456"
  },
  "validation_results": {...}
}

🚀 Getting Started with Your Project

Minimal Implementation (Day 1)

  1. Create flow search function - Extract entities without reading full files
  2. Define 3-4 core domains - Group related entities
  3. Build basic model extraction - Read targeted file sections
  4. Manual validation - Check 1 domain completely

Week 1 Implementation

  1. Automate entity extraction across all domains
  2. Create validation report templates
  3. Build prioritization logic based on flow importance
  4. Test with limited context AI (e.g., Claude Haiku)

Production Implementation

  1. Add CI/CD integration - Validate on model changes
  2. Implement incremental validation - Only check changed domains
  3. Create dashboard - Track completeness metrics over time
  4. Setup automated alerts - Notify on new gaps detected

💡 Lessons Learned

What Worked Well

What I’d Do Differently Next Time

My Biggest Wins

What’s Next?

Advanced Model Analysis

More Integrations

Specialized AI Agents

My Complete Implementation Resources

My Go-To Scripts

# Core validation commands
python3 scripts/flow_manager.py search "entity_name"
python3 scripts/flow_manager.py show flow_id
python3 scripts/flow_manager.py dependencies flow_id

Validation Templates

# Domain validation checklist
[domain.user_management]
entities_required = ["User", "UserProfile", "Session"]
relationships_required = ["User->UserProfile", "User->Session"]
status_enums_required = ["UserStatus", "SessionStatus"]

Automation Examples

# Automated gap detection
def find_model_gaps():
    flows = extract_all_flows()
    models = extract_all_models()

    required_entities = extract_entities_from_flows(flows)
    existing_entities = extract_entities_from_models(models)

    return required_entities - existing_entities

🎯 Key Takeaways

  1. Chunk Everything: Break overwhelming validation into domain-sized pieces
  2. Target Extraction: Smart queries beat reading massive files
  3. Progressive Processing: Build understanding incrementally
  4. Prioritize Ruthlessly: Fix flow-breaking issues first
  5. Automate Relentlessly: Manual validation doesn’t scale
  6. Measure Continuously: Track completeness and accuracy over time

🚀 Ready to Build Your System?

Start by identifying your core domains and building a simple flow search interface. The investment in automated data model validation pays massive dividends in preventing implementation delays and costly rework.

Next Steps:

  1. Map your entities to logical domains
  2. Build targeted extraction for your most critical domain
  3. Validate 5 high-priority flows manually
  4. Automate the process and expand to all domains

What data model validation challenges have you faced? How do you keep large schemas synchronized with evolving requirements? Share your experiences in the comments!


This post is part of my series on building robust validation systems for AI-driven development. Check out my previous post on user flow validation and stay tuned for my upcoming post on automated test generation from validated flows.