Repository navigation
feat: add organization AI spend overview queries - #31711
Merged
Merged
Conversation
ssncferreira
force-pushed
the
ssncf/ai-spend-overview-queries
branch
from
November 3, 2026 20:10
b32e812 to
fa956d9
Compare
ssncferreira
force-pushed
the
ssncf/ai-spend-overview-queries
branch
from
November 5, 2026 13:30
c72d6a2 to
5d31a34
Compare
ssncferreira
marked this pull request as ready for review
November 5, 2026 13:34
ssncferreira
requested review from
SasSwart,
ibetitsmike and
johnstcn
as code owners
November 5, 2026 13:34
mtojek
approved these changes
Nov 6, 2026
mtojek
left a comment
Member
There was a problem hiding this comment.
LGTM 👍 Checked the SQL against the schema - existing (effective_group_id, day) index covers all three queries, no new indexes needed.
ssncferreira
force-pushed
the
ssncf/ai-spend-overview-queries
branch
from
November 11, 2026 12:10
83cece9 to
1ebe59d
Compare
ssncferreira
force-pushed
the
ssncf/ai-spend-overview-queries
branch
from
November 12, 2026 10:20
1ebe59d to
1187570
Compare
ssncferreira
temporarily deployed
to
docs-preview
November 13, 2026 10:03 — with
GitHub Actions
Inactive
ssncferreira
added a commit
that referenced
this pull request
Nov 13, 2026
## Description
Adds the experimental `GET
/api/experimental/organizations/{organization}/ai/spend` endpoint that
backs the new Overview tab of the organization AI spend page ([Figma
design](https://www.figma.com/design/6TkixpkUIM5Qv6I7qhM3xZ/AI-Gov---user-spend?node-id=12-5152&t=YlCC7RGSMNrzMxVo-4)).
It returns the organization's spend summary, daily spend, and a page of
users with their per-group spend, using the queries from #31711.
## Changes
- Add the `organizationAISpend` handler and
`ExperimentalClient.OrganizationAISpend`:
- Reuses the period handling of `/ai/spend/users`: `period_start` and
`period_end` must be provided together and span at most 31 days, and the
default is the current budget period.
- Also requires both bounds at 00:00:00 UTC, since the daily spend
aggregate holds whole UTC days.
- Is not limited by the AI Gateway data retention window, since the
daily spend aggregate is not purged. `/ai/spend/users` and the export
keep the retention check, now in `aiSpendRetainedPeriod`.
- Returns `count`, `totals` (`spend_micros`, `median_spend_micros`),
`intervals` (daily spend, one interval per UTC day, zeros included), and
`users` (paginated with `limit`/`offset`, most spend first, with
per-group spend).
- Echoes the applied period, and reads every value in one read-only
repeatable-read transaction so the values agree.
- Extract `parseOrganizationAISpendPage`, shared with `/ai/spend/users`.
- Add handler tests.
Closes AIGOV-728
> [!NOTE]
> Generated by Coder Agents on behalf of @ssncferreira.
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds 3 database queries for the organization AI spend overview. They read the daily spend aggregate (
ai_user_daily_spend), scoped to the organization through each spend row's effective group, over a[period_start, period_end)range of UTC days.GetOrganizationAISpendTotals: the number of users with spend, the total spend, and the median of the users' total spend.ListOrganizationAISpendPerUser: paginated users, most spend first, with one row for each group their spend was attributed to.GetOrganizationAISpendPerDay: the spend for every UTC day in the range, including days without spend.Note: Only users with spend above zero are included, so users with only unpriced usage are not listed.
Changes
/ai/spend/users(read on organization group members).Related to AIGOV-728
Note
Generated by Coder Agents on behalf of @ssncferreira.