Overview
The Insights Cache system generates AI-powered insights from query results and caches them to avoid redundant API calls. It combines:- Statistical analysis - Means, medians, outliers, trends, and performance metrics
- AI interpretation - Natural language insights generated by Gemini 2.5 Flash
- Intelligent caching - Content-based cache keys using data signatures
- Real-time analysis - Automatic insight generation for new queries
Insights are cached based on
tableName + query + rowCount + columnCount, ensuring consistent results for identical datasets.Schema
See the insightsCache table definition for complete field reference.Core Functions
generateInsights
Generate AI insights from statistical analysis results.DuckDB table name being analyzed
SQL query that produced the data
Array of statistical analysis results for each numeric column
Number of rows analyzed
Number of columns analyzed
Bypass cache and regenerate insights
Implementation
convex/insights.ts
getCachedInsights
Retrieve cached insights by cache key.Hash generated from
tableName|query|columnCount|rowCountCached insights object, or
null if not foundconvex/insights.ts
cacheInsights
Store generated insights in the cache.Cache lookup key
Table name
SQL query
Data signature hash
Generated insights array
Statistical analysis results
ID of the cached entry (creates new or updates existing)
convex/insights.ts
Cache Key Generation
The cache key is generated by hashing the data signature:convex/insights.ts
The cache key changes when:
- Table name changes
- Query changes (different WHERE clause, columns, etc.)
- Number of rows or columns changes (data updated)
Usage Example
Frontend Integration
Insight Types
The AI generates insights in five categories:Outlier
Outlier
Values that deviate significantly from the norm (>2 standard deviations)Example: “Revenue spike of $1.2M detected in Q3, 3.5x above quarterly average”
Trend
Trend
Directional patterns over time (increasing, decreasing, stable)Example: “Customer satisfaction declining 12% month-over-month since June”
Aggregation
Aggregation
Summary statistics and aggregate findingsExample: “Top 10% of customers account for 67% of total revenue”
Pattern
Pattern
Recurring behaviors or correlationsExample: “Sales peak every Monday at 2pm, suggesting optimal campaign timing”
Anomaly
Anomaly
Unusual events or data quality issuesExample: “47 orders with negative prices detected, potential data entry error”
Performance Optimization
Cache Hit Rate
Monitor cache effectiveness:Statistical Analysis Offloading
AI Model Configuration
Meridian uses Gemini 2.5 Flash for insight generation:convex/table_agent.ts
- Fast response times (under 2 seconds typical)
- Low cost per request
- Strong reasoning for data analysis
- Structured output support via
generateObject
See Also
Schema Reference
Complete insightsCache table definition
Query Log
Query execution tracking
AI SDK Docs
Vercel AI SDK documentation