Skip to content

feat: add organization AI spend overview endpoint - #31709

Merged
ssncferreira merged 3 commits into
ssncf/ai-spend-overview-queriesfrom
ssncf/ai-spend-overview-endpoint
Nov 13, 2026
Merged

ssncferreira merged 3 commits into
ssncf/ai-spend-overview-queriesfrom
ssncf/ai-spend-overview-endpoint

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

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). 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.

@linear-code

linear-code Bot commented Nov 3, 2026

Copy link
Copy Markdown

AIGOV-728

@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch from 19849a9 to 793d96e Compare November 3, 2026 20:03
@ssncferreira
ssncferreira changed the base branch from main to ssncf/ai-spend-overview-queries November 3, 2026 20:04
@ssncferreira
ssncferreira added this pull request to stack #31712 November 3, 2026 20:04
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch from 793d96e to af5b8bd Compare November 3, 2026 20:11
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch 21 times, most recently from 7a2ccaa to 235b03d Compare November 5, 2026 19:11
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch 8 times, most recently from 2cdfea7 to 1f7ebed Compare November 6, 2026 09:49
@ssncferreira
ssncferreira marked this pull request as ready for review November 6, 2026 09:56
Comment thread codersdk/aibridge.go
type OrganizationAISpendOverviewGroup struct {
GroupID uuid.UUID `json:"group_id" format:"uuid"`
GroupName string `json:"group_name"`
GroupDisplayName string `json:"group_display_name"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I don't recall now, is display name mandatory or can it be empty?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Display name can be empty. Like other endpoints, this returns it as stored, and the UI falls back to group_name which cannot be empty. This seems to be the pattern with other endpoints.

PeriodEnd: queryEnd,
// #nosec G115 - The limit is capped above and the offset is parsed as int32.
LimitOpt: int32(page.Limit),
// #nosec G115 - The limit is capped above and the offset is parsed as int32.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm.. copy-paste of above one?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Limit and Offset come from codersdk.OrganizationAISpendPage, which uses int, so they need to be converted to int32 here. Other paginated handlers follow the same pattern, for example https://github.com/coder/coder/blob/main/coderd/users.go#L406-L409

Comment thread codersdk/aibridge.go Outdated
Count int64 `json:"count"`
Totals OrganizationAISpendOverviewTotals `json:"totals"`
// Intervals holds the daily spend, one interval per UTC day in the period
// (omitting days after the current day). Days without spend are zero.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: you can add here that the last interval is partial since the day is not over yet

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point, added. It's only partial when the period includes the current day, so I worded it that way.

Comment on lines +1440 to +1441
{name: "period_start", value: period.start},
{name: "period_end", value: period.end},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: looking at template insights we have start_time and end_time, perhaps we should adjust here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The other AI spend endpoints already use period_start and period_end (e.g., the CSV export, /ai/spend/users and /users/{user}/ai/spend). I would prefer to keep it this way for consistency across the AI spend endpoints.

@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch 2 times, most recently from 3fe6479 to b9595cc Compare November 11, 2026 12:36
@ssncferreira
ssncferreira requested a review from mtojek November 11, 2026 12:57
// retention window. Use it for raw AI Gateway data, which is subject to data
// retention. It narrows a default period to the window, rejects an explicit
// period that starts before it, and sets retentionStart.
func (api *API) aiSpendRetainedPeriod(ctx context.Context, rw http.ResponseWriter, r *http.Request, parser *httpapi.QueryParamParser) (period aiSpendPeriod, ok bool) {

@ssncferreira ssncferreira Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

These helpers (aiSpendPeriod and aiSpendRetainedPeriod) should have their own tests. Otherwise, the endpoint tests duplicate the period and retention cases. I'll address this in a follow-up to keep this PR focused.

@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-endpoint branch from b86a6f6 to b9ff9cb Compare November 12, 2026 10:20
@ssncferreira
ssncferreira merged commit 30abd05 into main Nov 13, 2026
30 of 31 checks passed
@ssncferreira
ssncferreira deleted the ssncf/ai-spend-overview-endpoint branch November 13, 2026 10:03
@ssncferreira
ssncferreira temporarily deployed to docs-preview November 13, 2026 10:03 — with GitHub Actions Inactive

This branch was previously deployed

1 inactive deployment
docs-preview — b9ff9cbe Deployed Nov 13, 2026 by ssncferreira via cleanup #26
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.

3 participants