Skip to content

feat: add provider, model, and client spend to the AI spend users endpoint - #32330

Merged
ssncferreira merged 5 commits into
mainfrom
ssncf/ai-spend-user-details
Nov 19, 2026
Merged

ssncferreira merged 5 commits into
mainfrom
ssncf/ai-spend-user-details

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Extends the experimental GET /api/experimental/organizations/{organization}/ai/spend/users endpoint with the per-user details behind the AI spend overview and the detailed report. Each user now returns their spend per provider, model, and client, and whether each has unpriced usage.

Changes

  • Rewrite ListOrganizationAISpendUsers with GROUPING SETS, returning one row per user and provider, model, or client:
    • Adds the user_ids and group_id filters.
    • Pages, counts, and totals users, not their rows.
    • Reads the provider display name, icon, and type from ai_providers, including deleted providers. The live provider wins when a name was reused, like GetAIProviderFilterOptions.
  • Update the SDK and handler:
    • Each user returns provider_spend, model_spend, and client_spend, most expensive first, each with has_unpriced_usage.
    • totals returns has_unpriced_usage.
    • Remove the providers, clients, and models name arrays and unpriced_usage_count.
    • Accept user_ids (comma-separated, maximum 100) and group_id.
    • Return more than 100 user_ids on the group members AI spend endpoint as a validation error, like /ai/spend/users.
  • Switch the Spend page to the new fields, with no UI changes.
  • Update the query and handler tests.
Dogfood performance
  • Benchmarked on dogfood, comparing the existing query on main with the updated query in this PR.
  • Tested 1, 7, 15 and 30-day windows, ending at November 18, 2026, 00:00 UTC, exclusive.
  • Each query ran five times per window, one at a time, with query order and window order rotated across rounds to reduce ordering bias.

The table shows PostgreSQL execution time in seconds, average (min-max).

Days Main This PR
1 0.288 (0.199-0.501) 0.223 (0.184-0.275)
7 11.413 (4.076-40.003) 4.107 (3.529-4.828)
15 9.961 (8.053-16.917) 7.253 (6.873-7.736)
30 14.979 (12.276-23.177) 8.499 (7.813-9.169)

All five runs are included. Main's first seven-day run took 40.003s with unusually high read I/O time, which raises its average. Cache and concurrent load can affect these results.

Closes AIGOV-734

Note

Generated by Coder Agents on behalf of @ssncferreira.

@ssncferreira
ssncferreira temporarily deployed to main November 17, 2026 19:35 — with GitHub Actions Inactive
@ssncferreira
ssncferreira temporarily deployed to main November 17, 2026 19:35 — with GitHub Actions Inactive
@linear-code

linear-code Bot commented Nov 17, 2026

Copy link
Copy Markdown

AIGOV-734

@ssncferreira
ssncferreira temporarily deployed to main November 17, 2026 19:39 — with GitHub Actions Inactive
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from ea504bb to faf8005 Compare November 18, 2026 12:09
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from faf8005 to e7c1913 Compare November 18, 2026 12:16
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from e7c1913 to c002260 Compare November 18, 2026 13:06
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from c002260 to d13ff55 Compare November 18, 2026 13:07
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from d13ff55 to a998290 Compare November 18, 2026 13:18
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from a998290 to 35a5197 Compare November 18, 2026 13:21
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from 35a5197 to ac3f43d Compare November 18, 2026 13:26
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from ac3f43d to 4c87384 Compare November 18, 2026 13:29
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-user-details branch from 4c87384 to 713235c Compare November 18, 2026 13:32
@ssncferreira
ssncferreira marked this pull request as ready for review November 18, 2026 13:33
query := r.URL.Query()
parser := httpapi.NewQueryParamParser()
page := parseOrganizationAISpendPage(parser, query)
userIDs := parser.UUIDs(query, nil, "user_ids")

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.

Non-blocking, might be me missing something: if a caller passes user_ids: [] from the TS client, I think getURLWithSearchParams sends it as user_ids=, since it only skips undefined and "". Then ParseCustomList would fail on uuid.Parse("") and return a 400. Nothing in the UI passes user_ids yet, so it's not an issue today, but it might be worth guarding before a filter UI uses it. That could be done client-side, or by having the server skip empty terms. WDYT?

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.

this ^

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. This follows the pattern used in the codebase, since parseQueryParamSet already handles this case. When the client sends user_ids=, the server treats an empty value the same as a missing one and returns the default (nil) before parsing, so it never reaches uuid.Parse(""). The query then applies no user filter. This is covered by the empty test case, and it's the same for every endpoint that reads a list query param.

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.

Ah, you're right, I missed that parseQueryParamSet treats an empty value as missing.

Comment thread coderd/database/db2sdk/db2sdk.go Resolved
Comment thread site/src/testHelpers/entities.ts Resolved
ELSE true
END
GROUP BY ai.initiator_id
GROUP BY GROUPING SETS (

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.

Thinking loud: Won't it be a performance issue? with limit=100 and ~40 models per user this returns thousands of rows per request, each repeating user/count/totals columns.

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.

It could become a performance problem, but I'd expect it to be uncommon for a user to use that many models, so we can revisit it if it becomes one.
Also, the dogfood performance tests showed that this query is faster than the current one (see the PR description).

AND (spend.is_provider OR spend.is_model OR spend.is_client)
-- Includes deleted providers, since usage still references them, and prefers
-- the live provider when a name was reused.
LEFT JOIN LATERAL (

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: no need to join organization_id, right?

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.

Right, providers are site-wide and not org scoped. The query is still scoped to the organization: usage is joined to groups through effective_group_id and filtered with groups.organization_id = @organization_id.

Comment thread codersdk/aibridge.go
CostMicros int64 `json:"cost_micros"`
// HasUnpricedUsage reports whether any token usage through the provider
// was recorded without a price.
HasUnpricedUsage bool `json:"has_unpriced_usage"`

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.. can't we deduce it from UnpricedModelCount and shrink the response?

@ssncferreira ssncferreira Nov 19, 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.

This is to highlight which provider, model or client has unpriced usage associated, all dimensions have this property. Initially, I added the UnpricedModelCount so the UI could easily show the unpriced models warning, but it's just as simple to check the models list for one with unpriced usage. Therefore, removed the count

{/* Spend rows list configured provider names, not provider types. */}
<ProvidersBadge
providers={user.providers}
providers={user.provider_spend.map((provider) => provider.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.

if API returns display_name, icon and type, do we still need providersByName?

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.

We don't. However, since this page will be replaced by the new one, I want to keep FE changes here minimal to what's needed so as not to break it. The new page will use the provider details returned by the endpoint.

query := r.URL.Query()
parser := httpapi.NewQueryParamParser()
page := parseOrganizationAISpendPage(parser, query)
userIDs := parser.UUIDs(query, nil, "user_ids")

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.

this ^

Comment thread codersdk/aibridge.go
// UserIDs restricts the report to these users.
UserIDs []uuid.UUID `json:"user_ids,omitempty" format:"uuid"`
// GroupID matches the effective group recorded on the token usage.
GroupID uuid.UUID `json:"group_id,omitempty" format:"uuid"`

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.

oh, does omitempty have any effect on uuid.UUID?

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.

It doesn't, since uuid.UUID is an array, so encoding/json always encodes it. Here it only makes group_id optional in the generated TS type, which seems to be the convention in codersdk.

Comment thread enterprise/coderd/aibridge.go Resolved

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

LGTM! 🚀 Small nit: the description still mentions unpriced_model_count (and counting unpriced models by name), which was removed in d1dc745 Worth updating so it matches the final API.

@mtojek mtojek left a comment

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.

Thanks, ship it!

@ssncferreira
ssncferreira merged commit 8705d80 into main Nov 19, 2026
37 of 38 checks passed
@ssncferreira
ssncferreira deleted the ssncf/ai-spend-user-details branch November 19, 2026 14:59
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026

This branch had an error being deployed

1 failed and 1 inactive deployments
docs-preview — 6a5e43cb Deployed Nov 19, 2026 by ssncferreira via cleanup #259
main — 6a5e43cb Deployed Nov 19, 2026 by ssncferreira via cla #260245
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