:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): show cost for loop runs (#101480)
Peter Kirkham06379b9c3501
diff --git a/products/desktop/docs/SESSION_SUBSCRIPTIONS.md b/products/desktop/docs/SESSION_SUBSCRIPTIONS.mdindex 4e4a3126c524..c86db6bae5d2 100644--- a/products/desktop/docs/SESSION_SUBSCRIPTIONS.md+++ b/products/desktop/docs/SESSION_SUBSCRIPTIONS.md@@ -17,3 +17,9 @@ Verification:- Append text to a displayed task and another task; status-only views must not render again.- Change permissions, completion, or run identity; the displayed status must update.- Mount two file-summary consumers and verify that they share results, including after tool updates, history replacement, a same-length reconcile, and eviction.++## Task cost++The session cost control fetches estimated model and compute costs for user-created tasks and for loop tasks, including workflow-backed loops.+It uses the existing task usage query and polls once per minute while the session is focused.+The task cost visibility flag controls the inline amount; when the flag is off, cost stays in the context breakdown.diff --git a/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.stories.tsx b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.stories.tsxnew file mode 100644index 000000000000..3eb759b9725f--- /dev/null+++ b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.stories.tsx@@ -0,0 +1,68 @@+import type { TaskUsage } from "@posthog/api-client/posthog-client";+import type { ServiceContainer } from "@posthog/di/container";+import { ServiceProvider } from "@posthog/di/react";+import { TASK_COST_VISIBLE_FLAG } from "@posthog/shared";+import {+ FEATURE_FLAGS,+ type FeatureFlags,+} from "@posthog/ui/features/feature-flags/identifiers";+import type { Meta, StoryObj } from "@storybook/react-vite";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { useMemo } from "react";+import { ContextUsageIndicator } from "./ContextUsageIndicator";++const meta: Meta<typeof ContextUsageIndicator> = {+ title: "Sessions/ContextUsageIndicator",+ component: ContextUsageIndicator,+ args: {+ taskId: "loop-task-example",+ originProduct: "loop",+ focused: false,+ usage: { used: 50_000, size: 200_000, percentage: 25, breakdown: null },+ },+ parameters: { costVisible: true, layout: "padded" },+ decorators: [+ (Story, context) => {+ const { container, queryClient } = useMemo(() => {+ const flags: FeatureFlags = {+ isEnabled: (key) =>+ key === TASK_COST_VISIBLE_FLAG && context.parameters.costVisible,+ getPayload: () => undefined,+ getVariant: () => undefined,+ onFlagsLoaded: () => () => {},+ };+ const container: ServiceContainer = {+ get: () => flags,+ getAll: () => [],+ isBound: (token) => token === FEATURE_FLAGS,+ bind: () => {+ throw new Error("Story services are fixed");+ },+ };+ const queryClient = new QueryClient();+ queryClient.setQueryData<TaskUsage>(+ ["task-usage", "loop-task-example"],+ { token_cost_usd: 0.4, compute_cost_usd: 0.02, total_cost_usd: 0.42 },+ );+ return { container, queryClient };+ }, [context.parameters.costVisible]);+ return (+ <ServiceProvider container={container}>+ <QueryClientProvider client={queryClient}>+ <div className="flex justify-end">+ <Story />+ </div>+ </QueryClientProvider>+ </ServiceProvider>+ );+ },+ ],+};++export default meta;+type Story = StoryObj<typeof ContextUsageIndicator>;++export const Loop: Story = {};+export const WorkflowLoop: Story = { args: { originProduct: "workflow" } };+export const WithoutContext: Story = { args: { usage: null } };+export const CostFlagOff: Story = { parameters: { costVisible: false } };diff --git a/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.test.tsx b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.test.tsxindex 08ea03450463..d0dc251ddd5b 100644--- a/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.test.tsx+++ b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.test.tsx@@ -55,20 +55,23 @@ describe("ContextUsageIndicator", () => {expect(container.querySelector("button")).toBeNull();});- it("shows the cost on its own when usage is null", () => {- enableCost(true);- const { container } = render(- <Theme>- <ContextUsageIndicator- usage={null}- taskId="task-1"- originProduct="user_created"- />- </Theme>,- );- expect(screen.getByText("$0.42")).toBeInTheDocument();- expect(container.querySelector("button")).toBeNull();- });+ it.each(["user_created", "loop", "workflow"])(+ "shows the cost on its own when usage is null for %s",+ (originProduct) => {+ enableCost(true);+ const { container } = render(+ <Theme>+ <ContextUsageIndicator+ usage={null}+ taskId="task-1"+ originProduct={originProduct}+ />+ </Theme>,+ );+ expect(screen.getByText("$0.42")).toBeInTheDocument();+ expect(container.querySelector("button")).toBeNull();+ },+ );// The ring carries no text, so the accessible name is the only way the// numbers reach a reader — including the "/0 · 0%" an unknown window must@@ -103,19 +106,22 @@ describe("ContextUsageIndicator", () => {},);- it("shows the cost beside the ring once the visible flag is on", () => {- enableCost(true);- render(- <Theme>- <ContextUsageIndicator- usage={usage()}- taskId="task-1"- originProduct="user_created"- />- </Theme>,- );- expect(screen.getByText("$0.42")).toBeInTheDocument();- });+ it.each(["user_created", "loop", "workflow"])(+ "shows the cost beside the ring once the visible flag is on for %s",+ (originProduct) => {+ enableCost(true);+ render(+ <Theme>+ <ContextUsageIndicator+ usage={usage()}+ taskId="task-1"+ originProduct={originProduct}+ />+ </Theme>,+ );+ expect(screen.getByText("$0.42")).toBeInTheDocument();+ },+ );it("hides the cost for a task from another product", () => {enableCost(true);@@ -134,19 +140,25 @@ describe("ContextUsageIndicator", () => {);});- it("keeps the cost in the popover while the visible flag is off", () => {- enableCost();- render(- <Theme>- <ContextUsageIndicator- usage={usage()}- taskId="task-1"- originProduct="user_created"- />- </Theme>,- );- expect(screen.queryByText("$0.42")).not.toBeInTheDocument();- });+ it.each(["user_created", "loop", "workflow"])(+ "keeps the cost in the popover while the visible flag is off for %s",+ (originProduct) => {+ enableCost();+ render(+ <Theme>+ <ContextUsageIndicator+ usage={usage()}+ taskId="task-1"+ originProduct={originProduct}+ />+ </Theme>,+ );+ expect(screen.queryByText("$0.42")).not.toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: "Context usage: 25% · $0.42" }),+ ).toBeInTheDocument();+ },+ );it("renders a finite stroke offset at 0% (no NaN/Infinity)", () => {const { container } = render(diff --git a/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.tsx b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.tsxindex 911e0f8b34c7..f6ee54f00d56 100644--- a/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.tsx+++ b/products/desktop/packages/ui/src/features/sessions/components/ContextUsageIndicator.tsx@@ -34,7 +34,10 @@ export function ContextUsageIndicator({originProduct,focused = true,}: ContextUsageIndicatorProps) {- const costEnabled = originProduct === "user_created";+ const costEnabled =+ originProduct === "user_created" ||+ originProduct === "loop" ||+ originProduct === "workflow";const costVisible = useFeatureFlag(TASK_COST_VISIBLE_FLAG);const { data: fetchedTaskUsage } = useTaskUsage(taskId,