@openinsure/analytics package provides portfolio KPI computation, Workers AI-powered predictive modeling, AI cost anomaly detection, and event tracking via Tinybird. It is consumed by the API worker’s analytics routes and by the Finance/Carrier portals.
Modules
Portfolio KPIs (compute.ts)
computeMGAKPIs(policies, claims, submissions)
Computes the full MGA portfolio metrics snapshot:
| Field | Description |
|---|---|
grossWrittenPremium | Sum of grossPremium across active/issued/renewed policies |
earnedPremium | GWP × 0.85 (earned premium factor) |
totalIncurred | Sum of paidAmount + reserveAmount across all claims |
lossRatio | totalIncurred / earnedPremium |
expenseRatio | 12% of earned premium (MGA default) |
combinedRatio | lossRatio + expenseRatio |
gwpByLob | GWP breakdown by line of business |
gwpByState | GWP breakdown by state |
submissionCount | Total submissions in pipeline |
boundRate | Submissions with status = 'bound' / total |
producerRankings | Top producers by GWP contribution |
computeCaptiveKPIs(policies, claims, members)
Captive-specific fund analysis:
| Field | Description |
|---|---|
fundBalance | Net fund position |
memberContributions | Total premium contributions |
claimReserves | Active claim reserve total |
ibnrEstimate | Case reserves × 15% |
stopLossPenetrations | Members approaching specific stop-loss attachment |
premiumToSurplusRatio | Regulatory solvency indicator |
summarizeClaims(claims)
Claims summary for dashboard widgets:
computeSubmissionPipelineKPIs(submissions)
Submission funnel metrics:
Predictive Modeling (predict.ts)
Workers AI-powered financial forecasting. Calls the CF Workers AI binding with a structured JSON schema for deterministic output.
predictLossRatio(input: LossRatioPredictionInput)
Forecasts loss ratio for a given LOB using historical performance and macro factors:
scorePricingAdequacy(currentPremium, predictedLossRatio, expenses)
Evaluates whether current pricing is adequate:
Retention & Portfolio Health (predictive.ts)
scoreRetention(policy, claims, paymentHistory)
Predicts renewal retention risk:
low (80+) · medium (60–79) · high (40–59) · critical (<40)
findCrossSellopportunities(insured, policies)
Identifies cross-sell opportunities based on current coverages and industry:
computePortfolioHealth(policies)
Portfolio concentration and at-risk premium analysis:
herfindahlIndex > 0.25 signals dangerous concentration in a single LOB or state.
AI Spend Monitoring (ai-spend.ts)
detectSpendAnomaly(dailyCosts, multiplier?)
Detects AI API cost spikes using a rolling 7-day average:
3.0 — anything 3× the rolling average triggers an anomaly alert. Used by the cron job (apps/api/src/cron/portfolio-sweep.ts) to emit a queue event when AI costs spike.
Event Tracking
Tinybird Integration
ANALYTICS_ENGINE binding is configured in wrangler.toml under [analytics_engine_datasets].
Analytics Engine (CF Native)
For high-throughput events (policy bound, claim opened, payment processed), the API writes directly to the CF Analytics Engine binding — zero latency, no HTTP call:API Routes
The analytics package powers these API endpoints:| Route | Description |
|---|---|
GET /v1/analytics/mga | MGA portfolio KPIs |
GET /v1/analytics/captive | Captive fund KPIs |
GET /v1/analytics/pipeline | Submission funnel metrics |
GET /v1/analytics/entities | Entity-level analytics |
GET /v1/analytics/compliance | Filing status breakdown |
GET /v1/analytics/claims | Claims frequency and severity |
?period=ytd|qtd|mtd and ?orgId= filters.
Adding Custom KPIs
Extendcompute.ts by adding a new function:
apps/api/src/routes/analytics.ts and update the Tinybird datasource schema if new event types are needed.