Skip to content

feat: filter the organization AI spend export query by provider, model, and client - #29220

Closed
ibetitsmike wants to merge 1 commit into
mike/ai-spend-query-usersfrom
mike/ai-spend-query-rollups
Closed

ibetitsmike wants to merge 1 commit into
mike/ai-spend-query-usersfrom
mike/ai-spend-query-rollups

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Second of a six-PR stack that reworks the AI spend page onto the released organization spend export, GET /api/v2/organizations/{organization}/ai/spend/export, and a sibling per-user endpoint, GET /api/v2/organizations/{organization}/ai/spend/users, instead of the deployment-wide /api/v2/ai-gateway/spend/* endpoints this stack previously proposed. Every PR in the stack was rewritten in place from current main; the head branch name predates the rework. The export's accounting rules are unchanged: token usage by created_at in [period_start, period_end), attributed to the organization through the effective budget group, so the page reconciles with the CSV and with daily budget accounting.

Adds optional provider_name, model, and client filters to ExportOrganizationAISpend, with the same predicates as ListOrganizationAISpendUsers. client matches after COALESCE(client, 'Unknown'), as the AI Gateway sessions listing does. Result columns are unchanged.

The querier tests seed one fixture for both queries and assert that, for every filter combination, the CSV rows summed per user equal the JSON rows and the report total. Removing either query's client predicate fails those tests.

Touches enterprise/coderd/aibridge.go only to name the new parameter fields for exhaustruct; the handler wires them in #29224.

Xum prepared this PR on behalf of @ibetitsmike.

@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-users branch from 2153280 to aeb88bc Compare September 11, 2026 10:01
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from d3535c6 to a58258c Compare September 11, 2026 10:01
@ibetitsmike
ibetitsmike marked this pull request as ready for review September 11, 2026 11:31
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-15T18:56:54.299594Z 4e98501 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from a58258c to 65a933d Compare September 14, 2026 08:03
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-users branch from aeb88bc to 71dfc0c Compare September 14, 2026 08:03
@ibetitsmike
ibetitsmike marked this pull request as draft September 15, 2026 18:48
@ibetitsmike
ibetitsmike removed this pull request from stack #29225 September 15, 2026 18:49
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-users branch from 71dfc0c to 7345ff5 Compare September 15, 2026 18:50
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from 65a933d to 4e98501 Compare September 15, 2026 18:51
@ibetitsmike ibetitsmike changed the title feat(coderd/database): add AI spend rollups query feat: filter the organization AI spend export query by provider, model, and client Sep 15, 2026
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

This PR was reworked in place on 2026-09-15: the stack now extends the released organization spend export (GET /api/v2/organizations/{organization}/ai/spend/export) with filters and an Accept: application/json per-user report instead of adding /api/v2/ai-gateway/spend/* endpoints, and the page shows one organization's total and per-user cost only. The description above reflects the new content; earlier review threads refer to code that no longer exists on this branch. #29222 (session counts) was closed because it has no counterpart.

@codex review

Xum posted this comment on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 4e985015d3

ℹ️ 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".

…l, and client

Add optional provider_name, model, and client parameters to
ExportOrganizationAISpend, matching the predicates ListOrganizationAISpendUsers
already applies. An empty value matches everything, and client compares
against COALESCE(client, 'Unknown') like the AI Gateway sessions list. The
export handler passes empty filters for now; exposing them as query
parameters follows separately.

Cover the filters on the export and add a reconciliation test that sums the
export per user and compares it with the user report for every filter
combination, so the two queries cannot drift apart silently.
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-users branch from 7345ff5 to ba04efe Compare September 16, 2026 11:16
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from 4e98501 to 7bbb2c6 Compare September 16, 2026 11:17

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given that this PR stack is for the User spend page, and we agreed to use a different endpoint than the CSV export, I would suggest keeping this stack focused on the user spend page and moving this PR to the stack for the CSV page.

@ibetitsmike
ibetitsmike removed this pull request from stack #29372 September 16, 2026 12:23
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 16, 2026
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Closing: this stack was reshaped to drop the CSV export dimension filters. The Spend page reads the per-user JSON report (/ai/spend/users, #29129), which keeps its provider_name, model, and client filters; the CSV export stays unfiltered as released. #29224 is closed for the same reason. The branch is left in place in case the filters are wanted later.

Xum closed this PR on behalf of @ibetitsmike.

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.

2 participants