Skip to content

fix: Enable real-time analytics tracking for chat messages#40

Merged
0xGingi merged 1 commit intonanogpt-community:mainfrom
jcrabapple:main
Jan 13, 2026
Merged

fix: Enable real-time analytics tracking for chat messages#40
0xGingi merged 1 commit intonanogpt-community:mainfrom
jcrabapple:main

Conversation

@jcrabapple
Copy link
Copy Markdown

This commit fixes the broken analytics feature that was not tracking model usage statistics for chat messages in real-time.

Problem

  • Message generation was storing analytics data (tokenCount, costUsd, responseTimeMs) in individual messages, but the aggregated model_performance_stats table was never updated
  • Stats were only recalculated when users visited the analytics page or manually clicked "recalculate", making the feature appear broken

Solution

  • Added asynchronous analytics tracking after each message generation
  • Implemented incremental updates (similar to TTS/STT) to efficiently update stats without full recalculation
  • Updates include: totalMessages, totalCost, avgTokens, avgResponseTime

Implementation Details

  • Runs asynchronously in background after message completion
  • Only processes when valid analytics data is available
  • Creates new stat entries or updates existing ones
  • Uses proper running average calculation for avgTokens and avgResponseTime

Related

  • TTS endpoint already implements this pattern (src/routes/api/tts/+server.ts:120-150)
  • Fixes inconsistency between chat and audio analytics tracking

This commit fixes the broken analytics feature that was not tracking
model usage statistics for chat messages in real-time.

**Problem:**
- Message generation was storing analytics data (tokenCount, costUsd,
  responseTimeMs) in individual messages, but the aggregated
  model_performance_stats table was never updated
- Stats were only recalculated when users visited the analytics page
  or manually clicked "recalculate", making the feature appear broken

**Solution:**
- Added asynchronous analytics tracking after each message generation
- Implemented incremental updates (similar to TTS/STT) to efficiently
  update stats without full recalculation
- Updates include: totalMessages, totalCost, avgTokens, avgResponseTime

**Implementation details:**
- Runs asynchronously in background after message completion
- Only processes when valid analytics data is available
- Creates new stat entries or updates existing ones
- Uses proper running average calculation for avgTokens and avgResponseTime

**Related:**
- TTS endpoint already implements this pattern (tts/+server.ts:120-150)
- Fixes inconsistency between chat and audio analytics tracking

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@0xGingi 0xGingi merged commit 03c2b70 into nanogpt-community:main Jan 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants