Files
coder/docs/coder-agents/platform-controls/spend-management.md
T
Sas Swart 4ac8f0a2aa docs: document how AI Gateway assigns a request's organization (#31925)
Documents how AI Gateway assigns every request to one organization, and
how that organization now decides the effective group, budget,
overrides, and spend. Adds a **Request organization** page and corrects
pages that still described budget resolution as deployment-wide.

Refs AIGOV-753. Describes behavior merged in #30902, #31118, #31119,
#31120, #31121, #31583, and #31584.

## Changes

- New page `docs/ai-governance/ai-gateway/request-organization.md`
(sidebar, after Cost control): resolution order, the effective group
fallback, rejected requests, what the organization controls, a multi-org
worked example, reports that still resolve across all orgs, and the
caveat for standalone gateways older than v2.40.
- `cost-controls.md`: removes "the effective group is deployment-wide",
scopes overrides and effective group resolution to the request's
organization, and adds the reporting caveat.
- `standalone.md`: older replicas check budgets across all
organizations.
- `coder-agents/platform-controls/{index,spend-management}.md`: these
said an override beats every group budget, which is no longer true for
chats in another organization.

## For reviewers

- **Mismatch rejection is not documented.** The ticket and RFC describe
rejecting a workspace/chat org mismatch. A chat can only bind a
workspace in its own org (`coderd/exp_chats.go`), chatd requests resolve
to the chat's org even with a workspace, and no client request can carry
both inputs. The page says the two always agree instead.
- **"v2.40" is inferred.** `release/2.39` is on aibridged API v1.5 and
`main` is on v1.6, so v1.6 ships in v2.40. A 2.39.x backport would make
this wrong.
- **Custom prices.** The price lookup reads per-org prices, but no API
or CLI can set them yet, so the page says custom prices apply in every
org. This needs an update when per-org prices ship.
- **Site-wide reports.** The avatar menu, Agents usage indicator, `GET
/api/v2/users/{user}/ai/spend`, Members tab budget, Groups page combined
limit, and `blocked_users` metric still resolve across all orgs
(`TODO(AIGOV-527)`), so they can disagree with what was enforced.

## Release note draft

```markdown
- AI Gateway budgets and spend follow the request's organization (#31119, #31120, #31121, #31583, #31584)

  > Every AI Gateway request now belongs to one organization: the workspace's organization for workspace session tokens, the chat's organization for Coder Agents, and otherwise the organization of the user's effective group. Budgets, user overrides, and spend apply within that organization only, instead of across all of the user's organizations. Users in one organization see no change. Users in several organizations can hit a smaller budget, or have no limit at all, in another organization's workspaces and chats. Requests are rejected when the user is no longer a member of the workspace's or chat's organization, or of any organization.
  >
  > Migration: Set budgets in every organization your users send AI requests from. Upgrade standalone AI Gateway replicas to v2.40. Older replicas keep checking budgets across all organizations until upgraded.
```

## Validation

`make pre-commit-light`, Vale on the changed files, and `make
lint/docs-manifest` pass. A `doc-check` author self-check found 13
blocking findings, all fixed. Not applied: a suggested test pinning the
site-wide spend endpoint (code change, out of scope for this docs PR).

<details>
<summary>Decision log and verified behavior</summary>

Sources: AIGOV-753, epic AIGOV-749 and its delivered tickets (AIGOV-712
to 716), the canceled AIGOV-717, and the RFC "AI Gateway Model RBAC and
Org-Scoping" (Phase 1).

Verified against `main` (`a3aab79775`):

| Claim | Evidence |
|---|---|
| Order: workspace session token, chat org, site-wide effective group |
`coderd/aibridgedserver/orgresolver.go` `resolveOrganization` |
| Workspace session token = `coder_workspace_owner.session_token` |
`provisionerdserver.WorkspaceSessionTokenName`, `workspaceAttribution`
(API tokens excluded) |
| Rejections return `403 unauthorized` | `coderd/aibridged/http.go`
IsAuthorized failure path |
| Rejected: deleted workspace, not a member of the workspace/chat org or
org deleted, no live org | `orgresolver.go` `ErrUnknownWorkspace`,
`ErrUnknownOrganizationMember`, `ErrNoOrganization` |
| Chat request takes chat org even with a workspace |
`enterprise/aibridged_integration_test.go` "delegated organization with
workspace" |
| With an org, only that org's groups, override, and Everyone group
count | `coderd/aibridge/budget/budget.go`, #31119 |
| Fallback with no budgets: default org, then earliest membership |
`GetUserEveryoneFallbackGroup` |
| Budget counts only spend recorded against its group |
`GetUserAISpendSince` keyed on `effective_group_id` |
| Notifications use the per-request effective group |
`coderd/aibridgedserver/notifications.go` |
| Per-org prices not settable |
`UpsertOrganizationAIProviderModelConfig` has no non-test caller |
| v2.39 gateways: site-wide budget check, no org recorded |
`release/2.39` `coderd/aibridged/http.go`,
`aibridge/recorder/drpc_recorder.go` |

Page vs. section: a dedicated explanation page, because
`cost-controls.md` is already long and RFC Phases 3 and 4 (model RBAC,
"require organization scope") will build on the same resolution rules.

</details>

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
2026-11-17 09:51:14 +02:00

5.1 KiB

title
title
Spend management

Coder controls agent spend with AI Gateway budgets, and surfaces the resulting spend to both admins and users.

Budgets

Coder Agents spend is controlled by AI Gateway budgets, which cap all AI Gateway usage (including Coder Agents chats) per user over the budget period.

  • Group budgets: set a budget for a group from the group's settings page. The deployment budget policy resolves which group budget applies when a user belongs to multiple budgeted groups, which defaults to the group with the highest budget.
  • Per-user overrides: set a custom budget for an individual user, attributed to one of their groups. Per-user overrides take priority over group budgets in the organization of the override's assigned group.

For budget settings, limits, and how Coder picks a user's budget group, refer to AI Governance cost control.

A chat uses the budgets of the chat's organization. For users in several organizations, refer to Request organization.

Important

Budget controls in the Coder UI, the group budget endpoints (/api/v2/groups/{group}/ai/budget), and the AI spend status and reporting endpoints all require the AI Gateway entitlement. No experiment is needed.

Coder v2.37 removed native chat usage limits and native cost tracking. Coder does not move the old limits, per-model prices, or cost history to AI Gateway. After the upgrade, the Spend page, the per-model price fields, and the old cost endpoints are not available. To keep the old data, export it before you upgrade.

The API reference documents how to get, upsert, and delete a group budget.

Enforcement

  • The AI Gateway checks the user's current spend before forwarding each request. When spend meets or exceeds the budget, the request is rejected and the chat shows a terminal error explaining that the budget was exceeded.
  • Brief overage is possible when concurrent requests are in flight, because cost is recorded after the large language model (LLM) responds.

User-facing status

The usage indicator on the Agents page and the summary in the user menu both show the signed-in user's current AI spend, their budget, and the period reset date. Both appear only when the deployment has the AI Gateway entitlement. For a user in several organizations, both can show a different budget from the one that applies to a chat, as Where spend reports can differ explains.

Export native cost data before you upgrade

Do these steps on your current server, before you upgrade to Coder v2.37 or later.

  1. Record the per-model prices in Admin settings > AI > Models.

  2. Choose a start date and an end date that cover all the history you need. Use RFC 3339 UTC timestamps.

    If you do not set the dates, the endpoints return only the last 30 days.

  3. Get the first page of users:

    GET /api/experimental/chats/cost/users?start_date=<start>&end_date=<end>&limit=100&offset=0

  4. If offset + users.length is less than count, get the next page. For each page, increase offset by 100.

  5. For each users[].user_id, get the summary with the same dates:

    GET /api/experimental/chats/cost/{user_id}/summary?start_date=<start>&end_date=<end>

    Each summary has the user's totals and the by_model and by_chat breakdowns.

After you upgrade, set up AI Gateway budgets. AI Gateway reports do not include the old cost history.

Spend details

Spend is shown where it is actionable:

  • Agents page and user menu: the signed-in user's spend against their budget, as described previously.
  • Group settings: each member's spend against the group's budget, for admins who can manage the group.
  • Chat summary panel: the cost of one chat tree, on a chat's Summary tab. A subagent reports the total for its whole tree, including the chat that started it.

For organization spend exports, refer to AI Gateway spend export.

The AI Gateway sessions views show per-request token usage, which is the input to those costs rather than the costs themselves.

AI Gateway data is subject to its own retention period, 60 days by default, which is configured independently of chat retention. Spend for requests older than that period is no longer reported, so a chat for which gateway records have been pruned reports no cost.