Skip to content

feat(site): add chat cost analytics frontend#23037

Merged
ibetitsmike merged 13 commits intomainfrom
mike/cost-analytics-frontend
Mar 13, 2026
Merged

feat(site): add chat cost analytics frontend#23037
ibetitsmike merged 13 commits intomainfrom
mike/cost-analytics-frontend

Conversation

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@ibetitsmike ibetitsmike commented Mar 13, 2026

Add UI components for viewing and managing LLM chat cost analytics.

Changes

  • UserAnalyticsDialog: personal cost summary with 30-day date range
  • ChatCostSummaryView: shared component for cost breakdowns by model and chat
  • ConfigureAgentsDialog: admin Usage tab with deployment-wide cost rollup
  • Storybook stories for all new and existing components
  • Replace ModelsSection.test.tsx, DashboardLayout.test.tsx, AuditPage.test.tsx with Storybook stories
  • Cost-related API client methods and React Query hooks
  • Analytics utilities for formatting microdollar values

Backend: #23036

@coder-tasks
Copy link
Copy Markdown
Contributor

coder-tasks Bot commented Mar 13, 2026

Documentation Check

Updates Needed

  • docs/ai-coder/agents/platform-controls.md - The "Usage controls and analytics" section currently describes this as future work ("We plan to give platform teams visibility..."). Now that basic cost analytics are shipping (admin Usage tab + per-user analytics dialog), this section should be updated to describe what is available today rather than what is planned.

    ⚠️ Marked as addressed in a prior run, but no corresponding documentation changes are present in this PR — platform-controls.md still reads as future-tense.

New Documentation Needed

  • docs/ai-coder/agents/platform-controls.md or a dedicated page — Document the new Usage tab in the admin Configure Agents dialog (deployment-wide spend rollup by user) and the personal usage stats dialog accessible from the Agents sidebar. Users and admins need to know these views exist and how to access them.

    ⚠️ Marked as addressed in a prior run, but no corresponding documentation changes are present in this PR.


Automated review via Coder Tasks

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b2b5502d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread site/index.html
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-backend branch from 3309c60 to 1f68feb Compare March 13, 2026 14:54
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-frontend branch from ec92d61 to 159eecd Compare March 13, 2026 14:54
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-backend branch from 1f68feb to ed75f06 Compare March 13, 2026 15:16
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-frontend branch 3 times, most recently from 33ba6be to bad63ee Compare March 13, 2026 15:38
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-backend branch from ed75f06 to d5ea139 Compare March 13, 2026 16:12
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-frontend branch 2 times, most recently from fa10d9b to 81e1a44 Compare March 13, 2026 16:34
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-backend branch from 59a5383 to 9e5df3c Compare March 13, 2026 17:04
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-frontend branch 2 times, most recently from 1f01cd2 to cb597cc Compare March 13, 2026 17:16
ibetitsmike added a commit that referenced this pull request Mar 13, 2026
Add cost tracking for LLM chat interactions with microdollar precision.

## Changes
- Add `chatcost` package for per-message cost calculation using
`shopspring/decimal` for intermediate arithmetic
- **Ceil rounding policy**: fractional micros round UP to next whole
micro (applied once after summing all components)
- Database migration: `total_cost_micros` BIGINT column with historical
backfill and `created_at` index
- API endpoints: per-user cost summary and admin rollup under
`/api/experimental/chats/cost/`
- SDK types: `ChatCostSummary`, `ChatCostModelBreakdown`,
`ChatCostUserRollup`
- Fix `modeloptionsgen` to handle `decimal.Decimal` as opaque numeric
type
- Update frontend pricing test fixtures for string decimal types

## Design decisions
- `NULL` = unpriced (no matching model config), `0` = free
- Reasoning tokens included in output tokens (no double-counting)
- Integer microdollars (BIGINT) for storage and API responses
- Price config uses `decimal.Decimal` for exact parsing; totals use
`int64`

Frontend: #23037
Base automatically changed from mike/cost-analytics-backend to main March 13, 2026 17:30
Add UI components for viewing and managing LLM chat cost analytics.

- Add UserAnalyticsDialog with cost summary for personal usage
- Add ChatCostSummaryView shared component for cost breakdowns
- Add ConfigureAgentsDialog with Usage tab for admin cost rollup
- Add Storybook stories for all new and existing components
- Replace deleted test files with comprehensive Storybook stories
  (ModelsSection, DashboardLayout, AuditPage)
- Add cost-related API client methods and React Query hooks
- Add analytics utilities for formatting microdollar values
@ibetitsmike ibetitsmike force-pushed the mike/cost-analytics-frontend branch from 94d6861 to 72f0ac2 Compare March 13, 2026 17:32
Copy link
Copy Markdown
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

PR Review Summary

Architecture is solid — shared ChatCostSummaryView, clean query factories, good Storybook coverage. Comments below are ordered roughly by impact. The highest-priority items are the key prop animation bug, missing debounce, accessibility gaps, and the NaN guard in formatCostMicros.

Comment thread site/src/pages/AgentsPage/AgentsPageView.tsx
Comment thread site/src/pages/AgentsPage/ConfigureAgentsDialog.tsx Outdated
Comment thread site/src/pages/AgentsPage/ConfigureAgentsDialog.tsx
Comment thread site/src/pages/AgentsPage/ChatCostSummaryView.tsx
Comment thread site/src/api/queries/chats.ts Outdated
Comment thread site/src/pages/AgentsPage/ChatCostSummaryView.tsx
Comment thread site/src/pages/AgentsPage/ChatCostSummaryView.tsx
Comment thread site/src/pages/AgentsPage/UserAnalyticsDialog.tsx
Comment thread site/src/api/queries/chats.ts Outdated
Comment thread AGENTS.md
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f41cb655c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread site/src/pages/AgentsPage/ChatCostSummaryView.tsx Outdated
Comment thread site/src/api/queries/chats.test.ts Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike ibetitsmike enabled auto-merge (squash) March 13, 2026 18:55
@ibetitsmike ibetitsmike merged commit eacabd8 into main Mar 13, 2026
26 checks passed
@ibetitsmike ibetitsmike deleted the mike/cost-analytics-frontend branch March 13, 2026 18:59
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants