Skip to content

Commit 4c7e039

Browse files
committed
feat(billing): add posthog code token spend analysis banner
Surfaces a self-serve spend analysis inside Settings -> Plan & usage. Calls the new /api/llm_analytics/posthog_code_spend/ endpoint, renders totals + breakdowns by ai_product / tool / model + top traces, and generates inline heuristic suggestions on where to optimise. Footer links to PostHog LLM analytics docs and the exploring-llm-costs skill. Gated behind the posthog-code-spend-analysis feature flag (always on in dev). Plan & usage section itself is also forced on in dev so the banner is reachable without billing-flag setup. Generated-By: PostHog Code Task-Id: f9d5d152-49c6-46cf-8fde-079105ba2e67
1 parent 677f026 commit 4c7e039

6 files changed

Lines changed: 505 additions & 1 deletion

File tree

apps/code/src/renderer/api/posthogClient.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { SpendAnalysisResponse } from "@features/billing/types/spend-analysis";
12
import { isSupportedReasoningEffort } from "@posthog/agent/adapters/reasoning-effort";
23
import type { PermissionMode } from "@posthog/agent/execution-mode";
34
import {
@@ -2854,4 +2855,24 @@ export class PostHogAPIClient {
28542855
const blob = await response.blob();
28552856
return URL.createObjectURL(blob);
28562857
}
2858+
2859+
/** Fetch the requesting user's PostHog Code token spend analysis. */
2860+
async getPostHogCodeSpendAnalysis(
2861+
days: number = 30,
2862+
): Promise<SpendAnalysisResponse> {
2863+
const urlPath = `/api/llm_analytics/posthog_code_spend/`;
2864+
const url = new URL(`${this.api.baseUrl}${urlPath}`);
2865+
url.searchParams.set("days", String(days));
2866+
const response = await this.api.fetcher.fetch({
2867+
method: "get",
2868+
url,
2869+
path: urlPath,
2870+
});
2871+
if (!response.ok) {
2872+
throw new Error(
2873+
`Failed to fetch token spend analysis: ${response.statusText}`,
2874+
);
2875+
}
2876+
return (await response.json()) as SpendAnalysisResponse;
2877+
}
28572878
}

0 commit comments

Comments
 (0)