Skip to content

feat: add organization member AI spend endpoint - #32203

Open
SasSwart wants to merge 2 commits into
aigov-766/user-spend-org-breakdownfrom
aigov-766/org-member-ai-spend
Open

SasSwart wants to merge 2 commits into
aigov-766/user-spend-org-breakdownfrom
aigov-766/org-member-ai-spend

Conversation

@SasSwart

@SasSwart SasSwart commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

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.OrganizationMemberAISpendStatus to codersdk, and links the endpoint from cost-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

SasSwart and others added 2 commits November 14, 2026 12:35
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>
@linear-code

linear-code Bot commented Nov 14, 2026

Copy link
Copy Markdown

AIGOV-766

@SasSwart
SasSwart added this pull request to stack #32205 November 14, 2026 12:50
@SasSwart
SasSwart temporarily deployed to docs-preview November 14, 2026 12:50 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown
Contributor

Docs preview

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

@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

Comment thread coderd/apidoc/docs.go
],
"summary": "Get organization member AI spend",
"operationId": "get-organization-member-ai-spend",
"parameters": [

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.

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.

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.

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]

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.

Should this start out as experimental?

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.

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

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.

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

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.

Do we really need a new group for this?!

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.

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.

@SasSwart
SasSwart marked this pull request as ready for review November 16, 2026 08:18

@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 nits/praise so approving 👍

CurrentSpendMicros: spend.SpendMicros,
}
if effectiveGroup.Limit != nil {
resp.EffectiveBudget = &codersdk.AIBudgetLimit{

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 could use aiBudgetLimitToSDK from #32202 instead of building AIBudgetLimit by hand.

Comment thread codersdk/aibridge.go
// 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 {

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.

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

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.

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.

This branch was previously deployed

1 inactive deployment
docs-preview — 92d08cac Deployed Nov 14, 2026 by SasSwart via deploy #190
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.

2 participants