From c28ffbf2258f5f9cc5868f4dd389186bb2821942 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 28 Feb 2026 13:14:09 -0800 Subject: [PATCH] Add cache token data and prompt_cache_metrics to PostHog telemetry Add cacheReadTokens and cacheWriteTokens to the existing apiRequest PostHog event, and emit a new prompt_cache_metrics event with cache_hit_rate, cache_read_tokens, cache_write_tokens, total_prompt_tokens, tool_count, and model. This populates the existing Prompt Cache Performance dashboard (ID: 1310089). Co-Authored-By: Claude Opus 4.6 --- .../src/stream/streamChatResponse.helpers.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/extensions/cli/src/stream/streamChatResponse.helpers.ts b/extensions/cli/src/stream/streamChatResponse.helpers.ts index 205a76fe8..94c7e14eb 100644 --- a/extensions/cli/src/stream/streamChatResponse.helpers.ts +++ b/extensions/cli/src/stream/streamChatResponse.helpers.ts @@ -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 {} return cost;