Skip to content

Commit 025801b

Browse files
authored
feat(billing): add posthog code token spend analysis banner (#2224)
1 parent 7cbdfb1 commit 025801b

5 files changed

Lines changed: 566 additions & 0 deletions

File tree

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

Lines changed: 32 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 {
@@ -2855,4 +2856,35 @@ export class PostHogAPIClient {
28552856
const blob = await response.blob();
28562857
return URL.createObjectURL(blob);
28572858
}
2859+
2860+
/**
2861+
* Fetch the requesting user's personal LLM spend analysis. `dateFrom` / `dateTo`
2862+
* accept absolute dates (`2026-04-23`) or relative strings (`-7d`, `-1m`), and
2863+
* default to the last 30 days. When `product` is set the tool / model / trace
2864+
* breakdowns are scoped to that `ai_product` (e.g. `posthog_code`); when omitted
2865+
* they aggregate across every product.
2866+
*/
2867+
async getPersonalSpendAnalysis(
2868+
options: { dateFrom?: string; dateTo?: string; product?: string } = {},
2869+
): Promise<SpendAnalysisResponse> {
2870+
const { dateFrom = "-30d", dateTo, product } = options;
2871+
const urlPath = `/api/llm_analytics/@me/spend/`;
2872+
const url = new URL(`${this.api.baseUrl}${urlPath}`);
2873+
url.searchParams.set("date_from", dateFrom);
2874+
if (dateTo) {
2875+
url.searchParams.set("date_to", dateTo);
2876+
}
2877+
if (product) {
2878+
url.searchParams.set("product", product);
2879+
}
2880+
const response = await this.api.fetcher.fetch({
2881+
method: "get",
2882+
url,
2883+
path: urlPath,
2884+
});
2885+
if (!response.ok) {
2886+
throw new Error(`Failed to fetch spend analysis: ${response.status}`);
2887+
}
2888+
return (await response.json()) as SpendAnalysisResponse;
2889+
}
28582890
}

0 commit comments

Comments
 (0)