Skip to content

AI & Analytics

import { Aside } from ‘@astrojs/starlight/components’;

The @biosync-io/analytics package provides built-in health data analytics that power AI-assisted coaching. It includes a correlation engine, anomaly detector, and an LLM context builder designed to produce structured data for AI assistants.

Automatically discovers relationships between health metrics using statistical analysis.

  1. Aggregates daily metric values over a configurable window (7–365 days)
  2. Computes Pearson (linear) and Spearman (rank-based) correlation coefficients for every metric pair
  3. Filters for statistical significance: |r| > 0.3 and p < 0.05
  4. Persists results to the metric_correlations table for trend tracking
POST /v1/users/:userId/analytics/correlations

Request body:

{
"days": 90
}

Response:

{
"data": [
{
"metricA": "resting_heart_rate",
"metricB": "sleep_score",
"pearson": -0.62,
"spearman": -0.58,
"pValue": 0.001,
"sampleSize": 87,
"strength": "moderate",
"direction": "negative"
}
],
"count": 12
}
get_correlations(userId, minStrength?, days?)

Returns discovered correlations with optional minimum strength filter.

Multi-method anomaly detection identifies unusual health patterns using three approaches.

MethodDescriptionThreshold
Z-ScoreStandard deviation from the mean> 2.5σ
IQRInterquartile range outlier detection1.5 × IQR beyond Q1/Q3
ClinicalHard-coded medical thresholdsSee table below
MetricThresholdSeverity
SpO₂< 92%critical
Heart Rate> 120 bpm (resting)critical
Heart Rate< 40 bpm (resting)critical
Temperature> 39.5°Ccritical
Temperature< 35.0°Cwarning
Blood Glucose> 11.1 mmol/Lwarning
Blood Glucose< 3.9 mmol/Lcritical
Respiratory Rate> 25 breaths/minwarning
Respiratory Rate< 8 breaths/mincritical
POST /v1/users/:userId/analytics/anomalies

Request body:

{
"lookbackDays": 1
}

Response:

{
"data": [
{
"metricType": "blood_oxygen",
"value": 89,
"method": "clinical_threshold",
"severity": "critical",
"message": "SpO2 89% is below clinical threshold of 92%",
"recordedAt": "2025-06-15T03:00:00.000Z"
}
],
"count": 1
}

When anomalies are detected during background analytics processing, the worker automatically enqueues notification jobs with category anomaly and the appropriate severity. Users who have configured notification rules matching the anomaly category will receive alerts through their configured channels.

get_anomaly_alerts(userId, severity?, status?, limit?)

The buildLLMContext() function produces a comprehensive biological context package optimized for AI assistants.

SectionContent
Baselines30-day rolling averages for all key metrics
TrendsDirection and magnitude of recent metric changes
AnomaliesActive anomaly alerts with severity and detection method
CorrelationsTop metric correlations with strength and direction
Health ScoresLatest composite scores (overall, sleep, activity, cardio, recovery)
SummaryNatural language paragraph summarizing the user’s current health state
GET /v1/users/:userId/analytics/context

Response:

{
"data": {
"baselines": {
"resting_heart_rate": { "mean": 58.2, "stdDev": 3.1, "sampleSize": 30 },
"sleep_score": { "mean": 78.5, "stdDev": 8.2, "sampleSize": 28 }
},
"trends": [
{ "metric": "resting_heart_rate", "direction": "decreasing", "changePercent": -4.2 }
],
"anomalies": [],
"correlations": [
{ "metricA": "sleep_score", "metricB": "recovery_score", "pearson": 0.72 }
],
"healthScores": {
"overall": 82, "sleep": 78, "activity": 85, "cardio": 80, "recovery": 84
},
"summary": "Overall health is good. Resting heart rate has been trending down over the past 2 weeks. Sleep quality and recovery scores are strongly correlated. No anomalies detected."
}
}
get_health_context(userId)

This is the recommended first tool call when an AI assistant needs to understand a user’s health state. The structured context + natural language summary provides everything needed for informed health coaching responses.

When connected via MCP, an AI assistant can use the context like this:

User: "How am I doing health-wise this week?"
AI calls: get_health_context(userId)
AI receives: structured baselines, trends, anomalies, scores, summary
AI responds with personalized, data-driven health insights

Composite health scores are computed from multiple underlying metrics.

ScoreDerived from
OverallWeighted average of all sub-scores
SleepSleep duration, efficiency, stage balance, consistency
ActivitySteps, active minutes, calories, workout frequency
CardioResting HR, HRV, VO₂ max trends
RecoveryRecovery score, strain balance, readiness
get_health_scores(userId, from?, to?, limit?)

Analytics processing runs on dedicated BullMQ queues:

QueueConcurrencyPurpose
analytics3Correlation computation, health score calculation
notifications8Dispatching anomaly alerts + other notifications

The analytics queue processes jobs after each sync completes, ensuring correlations and health scores are always up to date.