Skip to content

feat: price AI Gateway token usage with the request organization (2/2) - #31121

Merged
SasSwart merged 1 commit into
sas/aigov-716-use-the-resolved-organization-throughout-ai-gateway-requestsfrom
sas/aigov-716-org-prices
Oct 30, 2026
Merged

SasSwart merged 1 commit into
sas/aigov-716-use-the-resolved-organization-throughout-ai-gateway-requestsfrom
sas/aigov-716-org-prices

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Cost looks up model prices site-wide only, so an organization's own prices never apply, even though #31118 now stores the request organization on each interception.

Pass the interception's stored organization to GetAIModelPriceForProvider. The lookup already falls back from the organization's price to the site-wide custom price, then to the price book. An interception without an organization (old rows, old gateways) uses only site-wide prices, as before.

Stored org Org price Site-wide custom Price used
none any any site-wide custom, then price book
B live any B's price
B soft-deleted or absent present site-wide custom
B absent absent price book, or NULL cost

Remove TODO(AIGOV-716). Make TestRecordTokenUsageAuthorized store an organization and assert that its price wins under the strict authorizer.

Part of AIGOV-716. Stacked on #31120.


This PR was generated by Coder Agents on behalf of @SasSwart.

@linear-code

linear-code Bot commented Oct 21, 2026

Copy link
Copy Markdown

AIGOV-716

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@github-actions

Copy link
Copy Markdown
Contributor

Docs preview

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.

@SasSwart
SasSwart added this pull request to stack #31264 October 26, 2026 12:00
@SasSwart
SasSwart force-pushed the sas/aigov-716-org-prices branch from bd1d0bb to 6bc1f5a Compare October 28, 2026 08:35
@SasSwart
SasSwart force-pushed the sas/aigov-716-org-prices branch from 6bc1f5a to 26f644d Compare October 28, 2026 08:52
@SasSwart
SasSwart requested a review from ssncferreira October 28, 2026 08:58
@SasSwart
SasSwart marked this pull request as ready for review October 28, 2026 08:58
@SasSwart
SasSwart force-pushed the sas/aigov-716-org-prices branch 2 times, most recently from 4a2e92c to 58b33a3 Compare October 28, 2026 13:25
@SasSwart
SasSwart force-pushed the sas/aigov-716-org-prices branch from 58b33a3 to ca4dd58 Compare October 29, 2026 10:14
Cost now passes the organization stored on the interception to the price
lookup, so an organization's price applies to requests made in it. The
lookup falls back to the site-wide custom price and then the price book,
and an interception without an organization uses only site-wide prices.
@SasSwart
SasSwart force-pushed the sas/aigov-716-org-prices branch from ca4dd58 to 235e6f1 Compare October 30, 2026 11:20
@SasSwart
SasSwart merged commit 5baa8ef into main Oct 30, 2026
27 checks passed
@SasSwart
SasSwart deleted the sas/aigov-716-org-prices branch October 30, 2026 11:35
SasSwart added a commit that referenced this pull request Oct 30, 2026
…1/2) (#31120)

`IsAuthorized` resolves the workspace organization, but uses it only to
reject requests; nothing downstream reads it. The budget check and cost
each resolve the group site-wide, so a user working in an org B
workspace can be billed in org A, and no interception records its
organization.

Resolve the request organization once, in `IsAuthorized`. A workspace
session token resolves to the workspace's organization (source
`workspace`). Any other request, including chatd requests until
AIGOV-713, resolves to the organization of the user's site-wide
effective group (source `effective_group`). Return the source in
`IsAuthorizedResponse`.

Have aibridged pass the organization to `IsBudgetExceeded`, which
resolves the budget inside it, and stamp the organization and source on
`RecordInterception`. Resolve cost's effective group inside the
organization stored on the interception. When there is no organization,
behave site-wide as before.

Move `resolveOrganization` and the workspace checks into
`orgresolver.go`, and leave a slot for the chat organization
(AIGOV-713). Bump the aibridged API to v1.6, adding
`IsAuthorizedResponse.organization_source` and
`IsBudgetExceededRequest.organization_id`.

aibridged checks only the shape of the organization (both fields or
neither, and a valid non-nil UUID), not the source value. That lets a
newer coderd add a source without breaking older gateways.

Behaviour changes to review:

- Spend follows the workspace's organization. A user whose budgets are
all in org A has no limit in an org B workspace if B has none.
- A database error while resolving the organization now returns 403 from
`IsAuthorized`, instead of 500 from `IsBudgetExceeded`.
- Requests without a workspace make up to three extra budget queries in
`IsAuthorized`.
- The user spend status endpoint (`GET /api/v2/users/{user}/ai/spend`)
stays site-wide, so it can disagree with where a request was charged.

The org-aware price lookup follows in #31121.

Part of AIGOV-716. Stacked on #31119.

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 30, 2026
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.

2 participants