Files
coder/coderd/database/dbauthz
Susana Ferreira d611c60a9a feat: scope unpriced AI models to an organization and its prices (#31752)
## Description

Scopes `GetUnpricedAIModels` to an optional organization and checks
organization prices, so the unpriced models endpoint can list what an
organization still needs to price. Usage is attributed to an
organization through its token usage's effective group, the same as
spend.

## Changes

- Optional `organization_id`: when set, only that organization's usage
counts, and when null (the notification), all usage counts.
- A model still holds a price if it has a site-wide price for the
provider type, or an organization price (`ai_model_configs`, live and
with a price) for the provider in the usage's organization.
- dbauthz reads `ResourceAiModelPrice`, scoped to the organization when
one is given, like `GetAIModelPriceForProvider`.
- Table-driven query tests run every case for all organizations, org A
and org B.

<details>
<summary>Cases</summary>

Model X is on `anthropic-prod`. Y is the model the provider reports
serving for X. "Notif" is the deployment-wide notification, "UI A/B" is
the endpoint for org A/B.

| # | Prices | Usage | Notif before | Notif after | UI A | UI B |
|---|---|---|---|---|---|---|
| 1 | No price for X anywhere | A uses X, unpriced | ✅ | ✅ | ✅ | ❌ (B
didn't use X) |
| 2 | Site-wide price for X | A and B use X, priced | ❌ | ❌ | ❌ | ❌ |
| 3 | A priced X before the usage | Only A uses X, priced | ❌ | ❌ | ❌ |
❌ |
| 4 | A priced X after the usage | Only A uses X, unpriced when recorded
| ✅ (wrong) | ❌ | ❌ | ❌ |
| 5 | A priced X, B has none | A uses X (priced), B uses X (unpriced) |
✅ (because of B) | ✅ (because of B) | ❌ | ✅ |
| 6a | No price for X or Y when recorded, then Y priced site-wide | Only
A uses X, unpriced when recorded | ✅ | ✅ | ✅ | ❌ |
| 6b | Same as 6a | A and B use X, unpriced when recorded | ✅ | ✅ | ✅ |
✅ |
| 7a | No price for X, but a site-wide price for Y | A uses X, priced
through Y | ❌ | ❌ | ❌ | ❌ |
| 7b | No price for X, A priced Y, and B has none | A and B use X: A
priced through Y, B unpriced | ✅ (because of B) | ✅ (because of B) | ❌ |
✅ |

</details>

Note: pagination will be added in a follow-up.

Related to AIGOV-730

> [!NOTE]
> Generated by Coder Agents on behalf of @ssncferreira.
2026-11-13 13:21:32 +00:00
..