Repository navigation
Conversation
Add GET /api/v2/organizations/{organization}/members/{user}/ai/spend,
which returns a user's effective AI budget and current-period spend in a
single organization. The budget is resolved scoped to that organization,
matching how AI Gateway resolves a request made there.
Authorization matches the user spend endpoint: dbauthz requires user
read, so users can read their own spend and admins with user read can
read anyone's. Non-members get a 404.
Refs AIGOV-766
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Organization admins can read every user, so only the organization and membership middleware keep them out of organizations they don't administer. Cover both sides of that boundary, and narrow the docs sentence on who can read a member's spend to match. Refs AIGOV-766 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Docs previewLinks open this pull request's preview at https://pr-32203.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. |
| ], | ||
| "summary": "Get organization member AI spend", | ||
| "operationId": "get-organization-member-ai-spend", | ||
| "parameters": [ |
There was a problem hiding this comment.
Should we be able to specify the period start/end in this endpoint? We don't need to for the current ticket, but they're useful for the API to have.
There was a problem hiding this comment.
Agree it's not needed for 766. Adding optional period_start/period_end query params later wouldn't break existing callers, so it seems fine to defer.
| // @Param organization path string true "Organization ID or name" | ||
| // @Param user path string true "User ID, username, or me" | ||
| // @Success 200 {object} codersdk.OrganizationMemberAISpendStatus | ||
| // @Router /api/v2/organizations/{organization}/members/{user}/ai/spend [get] |
There was a problem hiding this comment.
Should this start out as experimental?
There was a problem hiding this comment.
The user spend endpoint it mirrors is in /api/v2, so keeping them together seems consistent. Is anything about this shape likely to change?
| // @Param user path string true "User ID, username, or me" | ||
| // @Success 200 {object} codersdk.OrganizationMemberAISpendStatus | ||
| // @Router /api/v2/organizations/{organization}/members/{user}/ai/spend [get] | ||
| func (api *API) organizationMemberAISpendStatus(rw http.ResponseWriter, r *http.Request) { |
There was a problem hiding this comment.
Run this locally, view an example trace, and upload that to the PR description.
| r.Get("/organizations/{organization}/members/{user}/workspace-quota", api.workspaceQuota) | ||
| }) | ||
|
|
||
| r.Group(func(r chi.Router) { |
There was a problem hiding this comment.
Do we really need a new group for this?!
There was a problem hiding this comment.
The neighbouring workspace-quota group deliberately uses ExtractUserParam (so removed members still resolve) and has no feature check. This route needs ExtractOrganizationMemberParam and RequireFeatureMW(FeatureAIBridge), so a separate group looks right to me.
mollymaefraser
left a comment
There was a problem hiding this comment.
Only nits/praise so approving 👍
| CurrentSpendMicros: spend.SpendMicros, | ||
| } | ||
| if effectiveGroup.Limit != nil { | ||
| resp.EffectiveBudget = &codersdk.AIBudgetLimit{ |
There was a problem hiding this comment.
nit: this could use aiBudgetLimitToSDK from #32202 instead of building AIBudgetLimit by hand.
| // OrganizationMemberAISpendStatus is a user's AI spend snapshot inside a | ||
| // single organization, resolved the same way AI Gateway resolves a request | ||
| // made in that organization. | ||
| type OrganizationMemberAISpendStatus struct { |
There was a problem hiding this comment.
UserOrganizationAISpend and OrganizationMemberAISpendStatus carry nearly the same data. Could they share a type, or is the difference intentional?
| // but not the user gets a 404, as on the user spend endpoint. | ||
| budgetPolicy := codersdk.NewAIBudgetPolicyFromString(api.DeploymentValues.AI.BridgeConfig.BudgetPolicy) | ||
| effectiveGroup, ok, err := budget.ResolveUserEffectiveGroup(ctx, api.Database, member.UserID, uuid.NullUUID{UUID: org.ID, Valid: true}, budgetPolicy) | ||
| if httpapi.Is404Error(err) { |
There was a problem hiding this comment.
Returning 404 both when the user isn't a member and when the caller can't read them looks right, and the org admin 200 and 404 tests cover it well.
Adds
GET /api/v2/organizations/{organization}/members/{user}/ai/spend, which returns a user's effective AI budget and current-period spend inside one organization. Admins, and users themselves, can use it to look up spend in a specific organization.The endpoint resolves the budget inside that organization only, the same way AI Gateway resolves a request made there. The response has the same shape as the user spend endpoint, plus
organization_id.Users can read their own spend, and admins can read the spend of a user whose membership in that organization they can read. The endpoint returns 404 when the user isn't a member of the organization, or when the caller can't read the user. It requires the AI Governance license. The tests cover an organization admin reading a member of their own organization (200) and of another organization (404), since organization admins can read every user and only the organization and membership checks keep them out.
Also adds
Client.OrganizationMemberAISpendStatustocodersdk, and links the endpoint fromcost-controls.md.Stacked on #32202, which adds the per-organization breakdown to
GET /users/{user}/ai/spend. Review only the last two commits here.Refs AIGOV-766
🤖 Generated with Claude Code