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
- Memory Efficiency Above All: It has to work within a 135k token limit (I tested it with DeepSeek).
- Domain-Driven Validation: I process related entities together instead of trying to do everything at once.
- Progressive Discovery: The system builds its understanding step-by-step, rather than loading everything upfront.
- Targeted Extraction: I use smart queries instead of reading huge files.
- Actionable Output: It generates clear implementation plans, not just reports.
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:
- Client Management: Client, Category, ClientProfile
- Project Management: Job, Task, TaskStatus
- Time Tracking: TimeEntry, Timer, ActiveTimer
- Billing: Invoice, InvoiceItem, Payment, Currency
- API Service: APIClient, APIKey, APIUsage, Webhook
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:
- 🔴 12 missing entities referenced in flows
- 🔴 34 missing fields mentioned in high-score flows
- 🔴 8 broken cross-domain relationships
- 🔴 No systematic way to track model completeness
After Validation:
- ✅ 100% entity coverage across all domains
- ✅ 98% field coverage for validation_score ≥ 95 flows
- ✅ All cross-domain relationships properly modeled
- ✅ Automated detection of future model drift
Memory Efficiency Proven
Testing with different AI context limits:
- DeepSeek (135k tokens): Successfully validated all 6 domains ✅
- Claude Haiku (200k tokens): Completed validation with room for optimization ✅
- GPT-3.5 (16k tokens): Required additional chunking but completed ✅
Processing Time
- Manual validation: 8+ hours across all domains
- Automated system: 45 minutes with human review
- Maintenance: 15 minutes per new flow added
📊 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)
- Create flow search function - Extract entities without reading full files
- Define 3-4 core domains - Group related entities
- Build basic model extraction - Read targeted file sections
- Manual validation - Check 1 domain completely
Week 1 Implementation
- Automate entity extraction across all domains
- Create validation report templates
- Build prioritization logic based on flow importance
- Test with limited context AI (e.g., Claude Haiku)
Production Implementation
- Add CI/CD integration - Validate on model changes
- Implement incremental validation - Only check changed domains
- Create dashboard - Track completeness metrics over time
- Setup automated alerts - Notify on new gaps detected
💡 Lessons Learned
What Worked Well
- Domain-based chunking was a lifesaver. It kept the validation process from becoming overwhelming.
- Targeted extraction cut down my context usage by a whopping 85%.
- Progressive processing worked well with different AI models, regardless of their capabilities.
- Concrete action plans meant I could act on the results right away.
What I’d Do Differently Next Time
- I’d start with domain mapping before I even think about building flows.
- I’d include field type validation (think VARCHAR vs. TEXT).
- I’d add automated testing for the validation logic itself.
- For bigger teams, I’d create domain ownership assignments.
My Biggest Wins
- I prevented some major data modeling errors before they could cause any real damage.
- I got rid of manual reconciliation, which took me from 8 hours down to just 45 minutes.
- I could finally develop with confidence, knowing that my models supported all of my flows.
- I created a sustainable process for keeping everything validated over the long term.
What’s Next?
Advanced Model Analysis
- Performance impact validation: I want to check for things like missing indexes.
- Migration safety checks: It would be great to validate breaking changes automatically.
- Database constraint verification: I want to make sure my business rules are being enforced at the database level.
More Integrations
- API schema validation: I want to ensure my endpoints always match my models.
- Frontend type generation: I’d love to auto-generate TypeScript interfaces from my models.
- Test data generation: It would be cool to create fixtures directly from my model schemas.
Specialized AI Agents
- Domain-specific agents: I’m thinking about creating specialized validation agents for each domain.
- Continuous monitoring: I want to get to a point where I can detect gaps in real-time.
- Predictive modeling: How cool would it be if the system could suggest missing entities before I even write the flows?
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
- Chunk Everything: Break overwhelming validation into domain-sized pieces
- Target Extraction: Smart queries beat reading massive files
- Progressive Processing: Build understanding incrementally
- Prioritize Ruthlessly: Fix flow-breaking issues first
- Automate Relentlessly: Manual validation doesn’t scale
- 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:
- Map your entities to logical domains
- Build targeted extraction for your most critical domain
- Validate 5 high-priority flows manually
- 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.