Skip to content

feat: add per-organization AI spend endpoint - #32202

Merged
SasSwart merged 4 commits into
mainfrom
aigov-766/user-spend-org-breakdown
Nov 24, 2026
Merged

SasSwart merged 4 commits into
mainfrom
aigov-766/user-spend-org-breakdown

Conversation

@SasSwart

@SasSwart SasSwart commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Adds GET /api/v2/users/{user}/ai/spend/organizations, which returns a user's AI budget and current-period spend in each of their organizations. The avatar menu uses it to show spend to users who belong to more than one organization (#32204).

The response lists each organization the user is a current member of and the caller can read, sorted by name. Each entry has the effective group, budget, and spend that AI Gateway resolves for a request in that organization. When no group budget applies, the effective group is the organization's Everyone group. organization_id names the organization of the user's site-wide effective group, so the menu opens on the numbers the user sees today. It's null when that organization isn't in the list.

GET /users/{user}/ai/spend is unchanged. Its API docs now say that it resolves the site-wide effective group, and point to the new endpoint for the per-organization breakdown.

dbauthz filters the user's organizations to those the caller can read, so an organization-scoped admin only sees the organizations they administer. The handler queries spend only for those organizations. A new query, GetUserAISpendByEffectiveGroupsSince, reads the spend for all of them in one call. It reads existing tables, so there's no migration, and dbauthz checks it the same way as GetUserAISpendSince: the caller must be able to read the user.

Refs AIGOV-766

Decisions
  • Separate endpoint. An earlier version of this PR added organizations and organization_id to GET /users/{user}/ai/spend. Following review, the breakdown moved to its own route (AIGOV-766 comment). Because the breakdown depends on who is asking, a dedicated route keeps the existing endpoint's response the same for every caller.
  • organization_id on the new response. The comment's schema left it out, but its frontend notes and its test rely on it, so the response includes it.
  • Existing endpoint left as on main. An organization-scoped admin can still see a user's site-wide effective group, budget, and spend through GET /users/{user}/ai/spend, even when that group is in another organization. The earlier version of this PR hid those fields; this version keeps the existing behavior, as the review asked.
  • Authorization check. dbauthz post-filters GetOrganizationsByUserID with read on each organization. The endpoint itself requires read on the user, as the existing spend endpoint does.
  • Empty result. A caller who can read the user but none of their organizations gets 200 with an empty organizations list and a null organization_id, so the response doesn't reveal that memberships exist.
  • Per-organization spend. Each entry reports spend on the user's current effective group in that organization, which is what budget enforcement compares.

Generated by Coder Agents on behalf of @SasSwart.

@SasSwart
SasSwart temporarily deployed to main November 14, 2026 12:46 — with GitHub Actions Inactive
@linear-code

linear-code Bot commented Nov 14, 2026

Copy link
Copy Markdown

AIGOV-766

@github-actions

Copy link
Copy Markdown
Contributor

Docs preview

Links open this pull request's preview at https://pr-32202.docs.cdr-preview.dev, which updates with each push.

Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here.

Preview sites for each built commit (1)

Every built commit keeps its own preview, newest first. Previews aren't indexed by search engines and are deleted when this pull request is merged or closed.

@SasSwart SasSwart left a comment

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.

Self Review

-- Total spend for user_id per effective group in @effective_group_ids on or
-- after period_start until NOW. Groups with no spend return 0. The
-- period_start parameter is normalized to its UTC calendar day.
SELECT

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.

put this through an explain and link the results.

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.

ai_user_daily_spend has its primary key on (user_id, effective_group_id, day), so I'd expect the join to use it. An EXPLAIN would confirm.

@SasSwart
SasSwart temporarily deployed to main November 14, 2026 15:48 — with GitHub Actions Inactive
@SasSwart
SasSwart marked this pull request as ready for review November 16, 2026 08:18
@SasSwart
SasSwart temporarily deployed to main November 16, 2026 08:18 — with GitHub Actions Inactive

@mollymaefraser mollymaefraser 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.

Only had praise/Qs/nits, so going to approve 👍

Comment thread enterprise/coderd/aibridge.go Resolved
Comment thread enterprise/coderd/aibridge.go Resolved
Comment thread enterprise/coderd/aibridge.go Outdated
// so it can land in an organization the caller cannot read. Report it only
// when its group is in the caller-visible breakdown, which also already
// holds its spend for the period.
if entry, found := findOrganizationAISpendByGroup(resp.Organizations, effectiveGroup.GroupID); ok && found {

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.

For an org admin, the top-level fields are now null and current_spend_micros is 0 when the group is in another org. Is anything (frontend or API users) relying on them always being set?

AND effective_group_id = @effective_group_id
AND day >= ((@period_start::timestamptz) AT TIME ZONE 'UTC')::date;

-- name: GetUserAISpendByEffectiveGroupsSince :many

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.

nit: could GetUserAISpendByEffectiveGroupsSince get a querier_test.go test like TestGetUserAISpendSince, for example groups with no spend returning 0, and the start-of-period boundary?"

-- Total spend for user_id per effective group in @effective_group_ids on or
-- after period_start until NOW. Groups with no spend return 0. The
-- period_start parameter is normalized to its UTC calendar day.
SELECT

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.

ai_user_daily_spend has its primary key on (user_id, effective_group_id, day), so I'd expect the join to use it. An EXPLAIN would confirm.

entries := make([]codersdk.UserOrganizationAISpend, 0, len(orgs))
groupIDs := make([]uuid.UUID, 0, len(orgs))
for _, org := range orgs {
group, ok, err := budget.ResolveUserEffectiveGroup(ctx, api.Database, userID, uuid.NullUUID{UUID: org.ID, Valid: true}, budgetPolicy)

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.

nit: this does about three budget queries per org, plus three for the site-wide group. Fine for a few orgs. Is a user in many orgs realistic?

@DanielleMaywood DanielleMaywood 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.

was tagged as agents-frontend code owner and changes to agents page looks fine 👍

// the user. Listing organizations as the caller is what keeps an
// organization-scoped admin from seeing the budget and spend of the user's
// other organizations.
orgs, err := api.Database.GetOrganizationsByUserID(ctx, database.GetOrganizationsByUserIDParams{

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.

Since the endpoint returns all organizations, two users with different org permissions can get a different response for the same user, because the response depends on who's asking. For example, an org admin only sees the organizations they can read, and the top-level effective group is empty when its organization is one they can't read. This seems a bit off 🤔

AFAIK this doesn't follow the patterns we have in the codebase. For example, workspace quota moved from a user-level route to a per-org one.

Should we keep /users/{user}/ai/spend as is, resolving the site-wide effective group (and make that more explicit in the API docs if it isn't yet), and add a separate per-org endpoint, /organizations/{organization}/members/{user}/ai/spend? That way, authorization is simpler, coming from the existing org and member middleware, and the response is the same for every caller who can read the organization. Wdyt?

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.

As discussed internally, given that the UI needs all orgs and to avoid making multiple requests, a better option might be to keep users/{user}/ai/spend as is, and introduce a new one specific for organizations, e.g., users/{user}/ai/spend/organizations

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.

Resolved in 6f261d1

SasSwart and others added 4 commits November 23, 2026 11:02
GET /api/v2/users/{user}/ai/spend now returns organization_id, the
organization of the top-level effective group, and organizations, the
effective group, budget and current-period spend in each live
organization the user belongs to, sorted by name.

Spend for all organizations is read with one new query,
GetUserAISpendByEffectiveGroupsSince. The organization list uses the
system context because the other queries authorize on the user, and a
caller-scoped list hides organizations from organization-scoped admins.

Refs AIGOV-766

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-organization breakdown listed the user's organizations with the
system context. The budget and spend queries only check that the caller
can read the user, so an organization-scoped admin could see the budget
and spend of the user's other organizations. List organizations as the
caller so the breakdown only includes organizations the caller can read.

Refs AIGOV-766

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The top-level effective group is resolved across all of the user's
organizations, and its queries only check that the caller can read the
user. An organization-scoped admin could therefore see the group,
budget, spend, and now organization_id of another organization.

Report the top-level fields only when the group is in the caller-visible
per-organization breakdown, and take its spend from that entry instead
of a second query.

Refs AIGOV-766

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…point

Per review on #32202, GET /users/{user}/ai/spend is unchanged from main
apart from its docs, which now state that it resolves the site-wide
effective group.

The per-organization breakdown and organization_id move to a new
GET /users/{user}/ai/spend/organizations. Its response is filtered to
organizations the caller can read, and organization_id is null when the
site-wide group's organization is not among them.
@SasSwart
SasSwart force-pushed the aigov-766/user-spend-org-breakdown branch from 4db8bd3 to 6f261d1 Compare November 23, 2026 11:56
@SasSwart SasSwart changed the title feat: add per-organization breakdown to user AI spend feat: add per-organization AI spend endpoint Nov 23, 2026
@SasSwart
SasSwart merged commit c236edc into main Nov 24, 2026
38 of 39 checks passed
@SasSwart
SasSwart deleted the aigov-766/user-spend-org-breakdown branch November 24, 2026 08:08
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 24, 2026

This branch had an error being deployed

1 failed and 1 inactive (outdated) deployments
docs-preview — 6f261d1c Deployed Nov 24, 2026 by SasSwart via cleanup #384
main — 4db8bd3b Deployed Nov 16, 2026 by SasSwart via cla #257781
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.

4 participants