Skip to content

feat(sessions): surface per-task cost estimate#3545

Merged
trunk-io[bot] merged 5 commits into
mainfrom
posthog-code/per-task-cost-estimate
Jul 17, 2026
Merged

feat(sessions): surface per-task cost estimate#3545
trunk-io[bot] merged 5 commits into
mainfrom
posthog-code/per-task-cost-estimate

Conversation

@gantoine

@gantoine gantoine commented Jul 17, 2026

Copy link
Copy Markdown
Member

Summary

Screenshot 2026-07-17 at 10 45 14 AM

Surfaces a per-task cost estimate in the session UI, gated behind a feature flag. The Claude Agent SDK already reports a per-turn total_cost_usd, which flowed into ContextUsage.cost but was never rendered anywhere. This wires it through to the UI.

  • Sum cost across turns. Each result message reports only that turn's spend, so the running session total is the sum — mirroring how the codebase already resets and re-accumulates per-turn token usage. Split cost out of the point-in-time context aggregate (used/size stay a snapshot of the newest turn; cost accrues), in both the batch extractor and the streaming append-only tracker (kept equivalent).
  • Render it. ContextUsageIndicator chip shows · $X.XX, and ContextBreakdownPopover gets an "Estimated cost" row.
  • Feature-flagged. Both readouts are gated behind posthog-code-task-cost (TASK_COST_FLAG). On in local dev via the import.meta.env.DEV fallback, matching the other UI flags. Until the flag is created/enabled in PostHog, isFeatureEnabled returns false, so the cost stays hidden in production.
  • Codex emits tokens without a cost, so cost stays null and the UI hides it regardless of the flag.

Labeled "Estimated cost" since it's the SDK's own estimate. Background task-notification turns are excluded (they don't feed the ACP usage_update that carries cost).

Rollout

Create the posthog-code-task-cost flag in the PostHog project the desktop app reads flags from, then enable for the target cohort.

Test plan

  • contextUsage.test.ts: added cases for single-turn cost, cross-turn summation, null-when-absent, append-only accumulation, and batch/tracker equivalence — 15 tests pass.
  • @posthog/core + @posthog/ui typecheck clean; Biome lint clean on all changed files (zero noRestrictedImports).

Follow-up (not in this PR)

Phase 2 — a local per-model pricing table for a per-model cost breakdown and Codex coverage.


Created with PostHog Code

The Claude Agent SDK already reports a per-turn `total_cost_usd`, which
flowed into `ContextUsage.cost` but was never rendered. Sum it across
turns (each result reports only its own spend, matching how per-turn
token usage is already accumulated) and show the running total in the
context usage indicator chip and breakdown popover.

Codex sessions emit tokens without a cost, so `cost` stays null and the
UI hides it.

Generated-By: PostHog Code
Task-Id: f6d673c3-965e-4598-a4bd-663f2651a5a5
@trunk-io

trunk-io Bot commented Jul 17, 2026

Copy link
Copy Markdown

😎 Merged successfully - details.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was skipped because it would exceed your organization's monthly flex usage limit. Raise the limit in billing settings or wait until the next billing period resets limits.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 320dd0e.

Wrap the cost readout (indicator chip suffix and breakdown popover row)
in a feature flag so it can roll out gradually. On in local dev via the
import.meta.env.DEV fallback, matching the other UI flags.

Generated-By: PostHog Code
Task-Id: f6d673c3-965e-4598-a4bd-663f2651a5a5
gantoine added 2 commits July 17, 2026 11:09
Gating the cost readout behind useFeatureFlag made the component call
useService(FEATURE_FLAGS), which throws in the test's provider-less
render. Mock the hook (matching the pattern used across the UI test
suite) and add a case covering the cost suffix when the flag is on.

Generated-By: PostHog Code
Task-Id: f6d673c3-965e-4598-a4bd-663f2651a5a5
Generated-By: PostHog Code
Task-Id: f6d673c3-965e-4598-a4bd-663f2651a5a5
@gantoine

Copy link
Copy Markdown
Member Author

/trunk merge

1 similar comment
@gantoine

Copy link
Copy Markdown
Member Author

/trunk merge

@gantoine

Copy link
Copy Markdown
Member Author

/trunk merge

@trunk-io
trunk-io Bot merged commit b092f19 into main Jul 17, 2026
32 checks passed
@trunk-io
trunk-io Bot deleted the posthog-code/per-task-cost-estimate branch July 17, 2026 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants