Data Strategy & Personalization
Loop Health’s platform is built on a unified patient data architecture that enables true 1:1 personalization and generates valuable real-world evidence for clinical research.
Overview
Every piece of health data you generate—lab results, wearable metrics, dose logs, conversations with Luna AI—flows into a unified Patient Graph. This comprehensive health profile enables personalized recommendations, tracks your progress over time, and contributes to aggregate research insights.
Core Capabilities
✅ Currently Implemented
The following data collection and personalization features are live in production:
- Unified Patient Identity
- Longitudinal Data Collection
- 1:1 Personalization Engine
- Real-World Evidence Generation
🚧 In Development
Enhanced data preservation and research capabilities are being implemented. See Future Enhancements below.
1. Unified Patient Identity
Problem: A single patient exists across multiple systems—Clerk (authentication), BigCommerce (orders), Rimo Health (prescriptions), Stripe (payments), lab providers, wearable devices. How do we know they’re the same person?
Solution: Identity Resolution System
How It Works
The identity_mappings table links all identifiers to one canonical patient:
Patient "Will" =
├─ clerk_user_id: user_2xyz...
├─ email: will@loop.health
├─ bigcommerce_customer_id: 12345
├─ stripe_customer_id: cus_abc...
├─ hubspot_contact_id: 67890
└─ phone: +1234567890Key Features:
- Automatic linking: When you place an order, your BigCommerce ID is linked to your Clerk account
- Conflict resolution: If an email appears in two systems, identities are merged
- Cross-system queries: “Show me all orders for this patient” works across platforms
Implementation:
// packages/patient-graph/src/identity/repository.ts
await identityRepo.resolveIdentity({
type: 'email',
value: 'will@loop.health'
});
// Returns: clerk_user_id that links to all other systemsStatus: ✅ Fully Implemented
- Identity resolution working in production
- Supports Clerk, BigCommerce, Stripe, HubSpot, phone, email
- Automatic merge on conflicts
- Used by all API endpoints
2. Longitudinal Data Collection
Problem: Health optimization requires tracking changes over time—weeks, months, years. One-time snapshots aren’t enough.
Solution: Comprehensive timeline tracking across all health data types.
What We Track
Clinical Data
| Data Type | Frequency | Retention | Details |
|---|---|---|---|
| Lab Results | As uploaded | Forever | Parsed biomarkers + reference to PDF |
| Protocol Doses | Daily | Forever | Scheduled doses + adherence logs |
| Adherence Events | Per dose | Forever | Completed/skipped with reasons |
| Weekly Check-ins | Weekly | Forever | Subjective outcomes, side effects |
| Patient Events | Continuous | Forever | Complete clinical timeline |
Biometric Data
| Data Type | Frequency | Retention | Source |
|---|---|---|---|
| Wearable Readings | Continuous | 2 years hot, forever cold | Oura, Whoop, Dexcom, Libre |
| Daily Aggregates | Daily | Forever | Sleep, HRV, recovery scores |
| Glucose Readings | Every 5 min | Forever | CGM devices |
Behavioral Data
| Data Type | Frequency | Retention | Details |
|---|---|---|---|
| Luna AI Conversations | Per interaction | Forever | Full transcripts + tool calls |
| Protocol Changes | On update | Forever | Who changed, when, what |
| Subscription Events | On change | Forever | Started, paused, cancelled |
Timeline Reconstruction
At any point, we can reconstruct a patient’s complete health state:
// Get patient state on January 15, 2025
const timeline = await patientGraph.reconstructTimeline(
customerId,
new Date('2025-01-15')
);
// Returns:
{
activeProtocols: [...],
recentLabs: [...],
wearableMetrics: {...},
conversationHistory: [...]
}Status: ✅ Fully Implemented
- All clinical data timestamped and preserved
- Complete event timeline with patient_events table
- Wearable data stored with recordedAt timestamps
- Conversation history preserved across channels
Known Gaps (see Future Enhancements):
- Lab PDFs not yet uploaded to permanent storage
- Wearable syncs currently disabled (jobs exist, need to be enabled)
- Protocol edit history not versioned
- Some data can be hard-deleted
3. 1:1 Personalization Engine
Problem: Generic health advice doesn’t work. Recommendations must account for YOUR specific labs, medications, conditions, goals, and history.
Solution: Patient context service that loads complete health profile before making recommendations.
How Luna AI Uses Your Data
When you ask Luna a question, here’s what happens:
// 1. User sends message
"Should I take BPC-157 for my knee pain?"
// 2. Luna loads your complete patient context
const context = await getPatientContext(userId);
// Returns:
{
profile: {
age: 34,
biologicalSex: 'male',
subscriptionTier: 'premium'
},
currentMedications: ['metformin', 'lisinopril'],
knownConditions: ['type 2 diabetes', 'hypertension'],
recentLabResults: [
{ code: 'A1C', value: 6.2, status: 'normal', date: '2025-01-15' },
{ code: 'creatinine', value: 1.1, status: 'normal', date: '2025-01-15' }
],
activeProtocols: [
{ title: 'Blood Sugar Management', compounds: ['berberine'] }
],
emergencyFlags: [] // Critical lab values or urgent events
}
// 3. Luna uses this context to give PERSONALIZED advice
// - Checks drug interactions (BPC-157 + your current meds)
// - Reviews contraindications (your diabetes, hypertension)
// - References your recent labs (kidney function OK for peptides)
// - Considers your active protocols (already using berberine)
// - Avoids generic responsesPersonalization Features
Protocol Recommendations
- Based on YOUR lab results, not population averages
- Considers YOUR medications for interaction checking
- Accounts for YOUR conditions and contraindications
- References YOUR goals and preferences
Health Scoring
- Weighted algorithm using YOUR data:
- 40% wearable metrics (YOUR sleep, HRV, recovery)
- 30% lab results (YOUR biomarkers vs optimal)
- 20% adherence (YOUR dose logging consistency)
- 10% check-ins (YOUR subjective feedback)
Dose Optimization
- Tracks YOUR adherence patterns
- Suggests timing based on YOUR schedule
- Adjusts for YOUR side effects
- Monitors YOUR progress over time
Implementation:
// apps/luna/src/services/patient-context.ts
export async function getPatientContext(userId: string): Promise<PatientContext> {
const repos = createRepositories(db);
// Load complete patient profile
const profile = await repos.customerProfiles.findByExternalId(userId);
const labs = await repos.labResults.list({ customerId, limit: 50 });
const protocols = await repos.protocols.list({ customerId, status: 'active' });
const events = await repos.patientEvents.list({ customerId, limit: 100 });
// Extract medications, conditions, emergency flags
const medications = protocols.flatMap(p => p.items.map(i => i.compound));
const emergencyFlags = detectCriticalLabs(labs);
return {
customerId,
profile,
medications,
conditions,
labResults,
activeProtocols,
emergencyFlags
};
}Status: ✅ Fully Implemented
- Patient context service operational
- Luna AI uses patient data for all responses
- Protocol recommendations personalized
- Health scoring operational
- Drug interaction checking enabled
4. Real-World Evidence Generation
Problem: Clinical trials are expensive, slow, and often don’t reflect real-world usage. Pharma companies need data on how their compounds perform in actual patients.
Solution: Aggregate de-identified patient data to generate population-level insights.
Cohort Analysis Capabilities
Currently Available Queries:
// Example: Semaglutide effectiveness analysis
const cohort = await analytics.getCohort({
compound: 'semaglutide',
minDuration: 365, // 1 year
minAdherence: 0.80, // 80%+ adherence
includeOutcomes: ['weight', 'A1C', 'adherence']
});
// Returns:
{
totalPatients: 8247,
avgWeightLoss: -14.2, // kg
avgA1CReduction: -1.3,
avgAdherence: 0.87,
dropoutRate: 0.12,
sideEffects: {
'nausea': 0.34,
'fatigue': 0.18,
...
}
}Real Example - AI Correlations:
Loop already analyzes aggregate data to find patterns:
"Users taking BPC-157 + TB-500 for joint pain report 40% better
outcomes than BPC-157 alone."
Sample size: 247 users
Timeframe: 90 days
Confidence: HighWhat This Enables
For Pharma Partners (e.g., Novo Nordisk):
- “Here’s 10,000 patients on compounded semaglutide for 1+ year”
- “Average weight loss: 14.2 kg at 12 months”
- “Adherence rate: 87% (vs 50% in clinical trials)”
- “Side effect profiles by demographic cohort”
- “Dose titration patterns and outcomes”
- “Real-world dropout predictors”
For Researchers:
- Compound combination synergies
- Protocol optimization patterns
- Biomarker trends over time
- Adherence predictors
- Outcomes by demographic groups
Privacy & Ethics:
- Minimum 100 patients per cohort
- All data de-identified
- Aggregate insights only, never individual records
- Patient consent for research use
Implementation:
// Current: AI Correlations Feature
// docs/ai-correlations-feature.md
- Weekly GPT-4 analysis of aggregate data
- Stored in Payload CMS for review
- Human approval required before publication
// Future: Pharma Partnership API
// Will provide structured queries for authorized research partnersStatus: ✅ Partially Implemented
- AI correlations feature live (weekly insights)
- Adherence analytics API operational
- Cohort query capabilities exist
- De-identification and privacy controls in place
Gaps (see tickets):
- Data completeness (lab PDFs, wearable syncs)
- Immutability guarantees (soft deletes needed)
- Formal data retention policy
Future Enhancements
The following improvements are prioritized for pharma-readiness and research partnerships:
🚧 In Active Development
Critical Data Preservation (4-6 weeks)
- Lab PDF permanent storage (Vercel Blob/S3)
- Raw wearable API response storage
- Soft delete pattern for all clinical data
- Enable wearable sync jobs
- Protocol version history tracking
- Data retention and archival policy
See Linear Tickets for detailed implementation plans.
Why This Matters:
- Ensures data completeness for longitudinal analysis
- Prevents accidental data loss
- Enables reprocessing with improved algorithms
- Meets regulatory retention requirements
- Builds confidence with pharma partners
📋 Planned
Advanced Analytics (Q2 2026)
- Cohort survival analysis (Kaplan-Meier curves)
- Propensity score matching for comparisons
- Time-series forecasting for outcomes
- Causal inference (protocol effect estimation)
Pharma Partnership API (Q2 2026)
- Structured query interface for research partners
- Real-time cohort analysis dashboard
- Automated report generation
- Regulatory-compliant data export
Enhanced Personalization (Q3 2026)
- Predictive adherence modeling
- Outcome forecasting based on similar patients
- Automated protocol optimization suggestions
- Real-time intervention triggers
Data Flow Diagram
┌─────────────────────────────────────────────────────────────┐
│ DATA COLLECTION │
├─────────────────────────────────────────────────────────────┤
│ │
│ Patient Actions External Systems │
│ ├─ Dose logging (mobile app) ├─ Oura Ring API │
│ ├─ Lab PDF upload ├─ Whoop API │
│ ├─ Weekly check-ins ├─ Dexcom CGM │
│ ├─ Luna AI conversations ├─ BigCommerce orders │
│ └─ Protocol adherence └─ Rimo prescriptions │
│ │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ IDENTITY RESOLUTION │
├─────────────────────────────────────────────────────────────┤
│ │
│ Link all identifiers to one canonical patient: │
│ Clerk ID → Email → BigCommerce → Stripe → Rimo → Phone │
│ │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ PATIENT GRAPH │
│ (Unified Data Storage) │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌──────────────────┐ │
│ │ Clinical Data │ │ Biometric Data │ │
│ ├─────────────────┤ ├──────────────────┤ │
│ │ • Lab results │ │ • Wearable │ │
│ │ • Protocols │ │ readings │ │
│ │ • Adherence │ │ • Daily stats │ │
│ │ • Check-ins │ │ • CGM glucose │ │
│ │ • Events │ │ │ │
│ └─────────────────┘ └──────────────────┘ │
│ │
│ ┌─────────────────┐ ┌──────────────────┐ │
│ │ Behavioral Data │ │ Commercial Data │ │
│ ├─────────────────┤ ├──────────────────┤ │
│ │ • Conversations │ │ • Orders │ │
│ │ • Protocol │ │ • Subscriptions │ │
│ │ changes │ │ • Prescriptions │ │
│ └─────────────────┘ └──────────────────┘ │
│ │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ PERSONALIZATION LAYER │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1:1 Patient Context → Personalized Responses: │
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ │
│ │ Luna AI │ │ Protocol │ │ Health Scoring │ │
│ │ (full │ │ Recommenda- │ │ (YOUR metrics) │ │
│ │ context) │ │ tions │ │ │ │
│ └─────────────┘ └──────────────┘ └─────────────────┘ │
│ │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ AGGREGATE RESEARCH LAYER │
├─────────────────────────────────────────────────────────────┤
│ │
│ De-identified cohort analysis: │
│ │
│ • 10,000 patients on semaglutide for 1+ year │
│ • Average outcomes, adherence, side effects │
│ • Protocol optimization patterns │
│ • Real-world evidence for pharma partners │
│ │
└─────────────────────────────────────────────────────────────┘Technical Implementation
Key Packages
@loop/patient-graph- Data access layer with repositories@loop/shared- Drizzle schema and Zod validations@loop/database- Supabase client@loop/core- Result type, error handling, logging@loop/biomarker-parser- Lab PDF parsing@loop/health-score- Health score calculation@loop/correlations- Aggregate analysis
Architecture Patterns
Append-Only Event Sourcing
adherence_events- immutable dose logspatient_events- complete timelineconversation_history- full transcripts- Never update, only insert new records
Timestamp Everything
- Every record has
created_atandupdated_at - Can reconstruct state at any point in time
- Enables longitudinal analysis
RBAC & Audit Logging
- Every data access logged in
rbac_logs - Who accessed what, when, why
- HIPAA compliance audit trail
See Also
- Patient Graph Architecture - Complete data model
- Luna AI Tools - How AI uses patient context
- Applications - Apps that consume this data
- Data Preservation Tickets - Upcoming improvements