Skip to content

feat: add organization AI model price API - #30720

Closed
SasSwart wants to merge 16 commits into
mainfrom
sdlcgraph/aigov-612-5-api
Closed

SasSwart wants to merge 16 commits into
mainfrom
sdlcgraph/aigov-612-5-api

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Adds experimental, AI Gateway licensed routes to manage an organization's AI model prices: GET, POST and DELETE on /api/experimental/organizations/{organization}/ai/model-prices. They authorize against ResourceAiModelPrice.InOrg(org), so only owners and org admins can use them. The site-wide routes are unchanged.

  • GET returns one of the organization's prices, keyed by provider_id and model in the query string. It returns 404 when there is no such price.
  • POST sets one price: provider_id (a live provider), model and the four prices. Invalid requests return 400 and write nothing.
  • DELETE takes the same key as GET. The key is in the query string because model names can contain slashes.

Adds the matching codersdk types and ExperimentalClient methods. Swagger, API docs and TypeScript types are regenerated.

This is the top of the stack. Closes AIGOV-612. Replaces #30690 and #30396.


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

@linear-code

linear-code Bot commented Oct 13, 2026

Copy link
Copy Markdown

AIGOV-612

@SasSwart
SasSwart added this pull request to stack #30737 October 14, 2026 06:51
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from 2bb1898 to fca688a Compare October 14, 2026 12:42
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from fca688a to 1c1f8ee Compare October 15, 2026 07:53
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from 1c1f8ee to 3f3c081 Compare October 15, 2026 11:12
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from 3f3c081 to f2a98ad Compare October 16, 2026 10:22
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from f2a98ad to dbad92e Compare October 19, 2026 08:56
SasSwart added a commit that referenced this pull request Oct 19, 2026
… validation (#30716)

Moves request body decoding and per-price validation out of the
site-wide `upsertAIModelPrices` handler, so the organization price
handler later in this stack can reuse them.

- The handler still reads the body and writes the HTTP errors.
`writeAIModelPricesBodyReadError` picks 413 or 400.
- `decodeAIModelPricesBody` only decodes, and returns an error.
- `aiModelPriceKeys` holds the JSON keys present in one price, so
validation can tell an explicit `null` apart from a missing price.
- `validateAIModelPriceValues` checks the four prices of one model
price. It takes them as an `aiModelTokenPrices` struct, so callers set
each price by name and cannot pass them in the wrong order.

No behavior change. The existing `TestUpsertAIModelPrices`,
`TestListAIModelPrices` and `TestValidateAIModelPrices` cover the
site-wide handler.

**Used later:** #30720 reuses the body read error, the price keys and
the value validation for its single-price upsert.

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch 2 times, most recently from 19145a1 to d280997 Compare October 19, 2026 12:22
Organization prices are only per configured provider, so remove the
upsert and delete queries for provider type prices and their listing
branch.
Org admins edit prices one model at a time, so the upsert takes typed
params instead of a JSON batch. The org price listing tells organization
and site-wide rows apart by provider_id instead of a price kind.
Replace GetOrganizationAIModelPrices, which listed every price that
applies to an organization, with GetOrganizationAIProviderModelPrice,
which reads one price by organization, provider and model. Queries
target the renamed ai_model_configs table.

Split the tests per query, and cover that an identical upsert leaves
updated_at unchanged and that an empty model is rejected.

Leave a TODO to authorize these queries against a resource for AI
Governance model config rather than ResourceAiModelPrice.
DeleteOrganizationAIProviderModelPrice now flags the live row as deleted
instead of removing it. Reads and upserts only match live rows, so an
upsert after a delete inserts a new live row and leaves the soft-deleted
one untouched.
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from d280997 to 09b9751 Compare October 21, 2026 06:38
…elper

The get helper captured the parent test's context and testing.T, which
paralleltestctx flags once the subtests call t.Parallel.
…okup

The lookup now ranks an organization's price for the configured provider,
then site-wide custom and default prices.
Drop the price kind from the lookup and token usage records, remove the
unused GetAIModelPriceByProviderModel query, and stop counting
organization prices in the unpriced-models report until they apply at
runtime.
Follow the rename of the organization price table to ai_model_configs.
Link the TODO for passing the organization to the price lookup to
AIGOV-716.
GetAIModelPriceForProvider read ai_model_configs without the deleted
filter, so a deleted organization price kept pricing token usage
instead of falling back to the site-wide price.
Organization upserts and deletes take a required provider_id. The
provider type field and the organization_provider_type scope are gone.
POST on the organization route takes a single price for one configured
provider, matching how org admins edit prices. The listing scope comes
from provider_id now that the price kind is gone.
GetOrganizationAIModelPrices is gone, so the organization GET route now
returns one price, keyed like DELETE by the provider_id and model query
parameters, and 404s when the organization has no such price.
OrganizationAIModelPrice drops the fields that only described a merged
listing: scope, provider type, provider name, and source.

Validate the organization price through aiModelTokenPrices.
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-5-api branch from 09b9751 to 753930e Compare October 21, 2026 07:20
…ID as a UUID

TestSDKFieldsFormatted requires uuid.UUID fields to carry format:"uuid".
SasSwart added a commit that referenced this pull request Oct 22, 2026
Adds the store queries for organization AI model prices in
`ai_model_configs`, with authorization. They cover basic CRUD on one
price, keyed by `(organization_id, provider_id, model)`.

- `GetOrganizationAIProviderModelPrice` reads one price.
- `UpsertOrganizationAIProviderModelPrice` sets one price, with typed
params. It rewrites an existing row only when a price differs, so
`updated_at` records the last change.
- `DeleteOrganizationAIProviderModelPrice` soft deletes one price. Reads
and upserts only match live rows, so setting a price after a delete
inserts a new live row.

All three authorize against `ResourceAiModelPrice.InOrg(org)`. The new
`AiModelPriceInOrg` case in `TestRolePermissions` pins that only owners
and org admins pass. A TODO in dbauthz notes that this should become a
resource for AI Governance model config.

Each query has its own test. The upsert tests cover that identical
prices leave `updated_at` unchanged, that changed prices advance it, and
that an empty model is rejected.

**Used later:** #30719 seeds its lookup tests with the upsert. #30720
serves all three through the org price API.

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
Base automatically changed from sdlcgraph/aigov-612-4-lookup to main October 22, 2026 15:13
SasSwart added a commit that referenced this pull request Oct 22, 2026
Prices token usage by the most specific match: the organization's price
for the configured provider, then a site-wide custom price, then the
price book. The requested model is still preferred over the model the
provider reports.

- `GetAIModelPriceForProvider` replaces
`GetAIModelPriceByProviderModel`, which is removed. It is one `UNION
ALL` of primary-key lookups, ordered by requested model first, then by
specificity. dbauthz checks `ResourceAiModelPrice`, scoped to the org
when one is given.
- `coderd/aibridge/prices` tests read seeded prices through
`GetAIModelPrices`.

Interceptions don't carry an organization yet, so
`resolveTokenUsageCost` passes none and only site-wide prices apply at
runtime for now (AIGOV-716). Org matching is covered by database tests
with explicit org IDs. For the same reason, the unpriced-models report
still counts only site-wide prices.

**Used later:** prices set through the API in #30720 take effect here
once interceptions carry an organization.

---

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

Copy link
Copy Markdown
Collaborator Author

Closing at the author's request.


This comment was generated by Coder Agents.

@SasSwart SasSwart closed this Oct 26, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 26, 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.

1 participant