Repository navigation
feat: add provider, model, and client spend to the AI spend users endpoint - #32330
Conversation
ea504bb to
faf8005
Compare
faf8005 to
e7c1913
Compare
e7c1913 to
c002260
Compare
c002260 to
d13ff55
Compare
d13ff55 to
a998290
Compare
a998290 to
35a5197
Compare
35a5197 to
ac3f43d
Compare
ac3f43d to
4c87384
Compare
4c87384 to
713235c
Compare
| query := r.URL.Query() | ||
| parser := httpapi.NewQueryParamParser() | ||
| page := parseOrganizationAISpendPage(parser, query) | ||
| userIDs := parser.UUIDs(query, nil, "user_ids") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ah, you're right, I missed that parseQueryParamSet treats an empty value as missing.
| ELSE true | ||
| END | ||
| GROUP BY ai.initiator_id | ||
| GROUP BY GROUPING SETS ( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
nit: no need to join organization_id, right?
There was a problem hiding this comment.
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.
| 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"` |
There was a problem hiding this comment.
Hmm.. can't we deduce it from UnpricedModelCount and shrink the response?
There was a problem hiding this comment.
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)} |
There was a problem hiding this comment.
if API returns display_name, icon and type, do we still need providersByName?
There was a problem hiding this comment.
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") |
| // 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"` |
There was a problem hiding this comment.
oh, does omitempty have any effect on uuid.UUID?
There was a problem hiding this comment.
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.
53202d3 to
6a5e43c
Compare
mollymaefraser
left a comment
There was a problem hiding this comment.
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.
Description
Extends the experimental
GET /api/experimental/organizations/{organization}/ai/spend/usersendpoint 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
ListOrganizationAISpendUserswithGROUPING SETS, returning one row per user and provider, model, or client:user_idsandgroup_idfilters.ai_providers, including deleted providers. The live provider wins when a name was reused, likeGetAIProviderFilterOptions.provider_spend,model_spend, andclient_spend, most expensive first, each withhas_unpriced_usage.totalsreturnshas_unpriced_usage.providers,clients, andmodelsname arrays andunpriced_usage_count.user_ids(comma-separated, maximum 100) andgroup_id.user_idson the group members AI spend endpoint as a validation error, like/ai/spend/users.Dogfood performance
The table shows PostgreSQL execution time in seconds, average (min-max).
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.