Merge pull request #10936 from continuedev/add-cache-telemetry-posthog

Add cache token data to PostHog telemetry events
This commit is contained in:
Nate Sesti 2026-03-01 12:41:43 -08:00 • committed by GitHub
commit a45888f5c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -367,6 +367,11 @@ export function recordStreamTelemetry(options: {
});
// Mirror core metrics to PostHog for product analytics
const cacheReadTokens =
fullUsage?.prompt_tokens_details?.cache_read_tokens ?? 0;
const cacheWriteTokens =
fullUsage?.prompt_tokens_details?.cache_write_tokens ?? 0;
try {
posthogService.capture("apiRequest", {
model: model.model,
@ -374,7 +379,21 @@ export function recordStreamTelemetry(options: {
inputTokens: actualInputTokens,
outputTokens: actualOutputTokens,
costUsd: cost,
cacheReadTokens,
cacheWriteTokens,
});
// Emit prompt_cache_metrics for the Prompt Cache Performance dashboard
if (actualInputTokens > 0) {
posthogService.capture("prompt_cache_metrics", {
model: model.model,
cache_read_tokens: cacheReadTokens,
cache_write_tokens: cacheWriteTokens,
total_prompt_tokens: actualInputTokens,
cache_hit_rate: cacheReadTokens / actualInputTokens,
tool_count: tools?.length ?? 0,
});
}
} catch {}
// Report prompt cache metrics to PostHog