GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(coderd): price token usage with organization prices by SasSwart · Pull Request #30719 · coder/coder

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 13, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@linear-code

linear-code Bot commented Oct 13, 2026

Copy link
Copy Markdown

AIGOV-612

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot assigned SasSwart Oct 13, 2026
This was referenced Oct 13, 2026
refactor(enterprise/coderd): extract AI model price body decoding and validation #30716
Merged
feat(coderd/database): add organization AI model config table #30717
Merged
feat(coderd): add queries for organization AI model prices #30718
Merged
feat: add organization AI model price API #30720
Closed
feat: add organization-scoped AI model prices #30690
Closed
@SasSwart
SasSwart added this pull request to stack #30737 October 14, 2026 06:51
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from 135b4d4 to 0958a4f Compare October 14, 2026 12:42
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from 0958a4f to cc7f0f8 Compare October 15, 2026 07:53
SasSwart
SasSwart commented Oct 15, 2026
View reviewed changes

@SasSwart SasSwart left a comment

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self Review

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread coderd/database/queries/aicostcontrol.sql Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread coderd/database/querier.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@SasSwart
SasSwart requested a review from ssncferreira October 15, 2026 09:47
@SasSwart
SasSwart marked this pull request as ready for review October 15, 2026 09:47
@SasSwart
SasSwart requested review from ibetitsmike and johnstcn as code owners October 15, 2026 09:47
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from cc7f0f8 to 198ffd3 Compare October 15, 2026 11:12
ssncferreira
ssncferreira reviewed Oct 15, 2026
View reviewed changes
Comment thread coderd/aibridgedserver/cost.go Outdated
// to the model reported by the provider, and compute cost. Organization
// prices need the organization the request was made in, which
// interceptions do not carry yet, so only site-wide prices match.
// TODO: pass the organization once interceptions carry it.

@ssncferreira ssncferreira Oct 15, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: link to the relevant issue, I believe AIGOV-716

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread coderd/database/queries/aicostcontrol.sql Resolved

Uh oh!

There was an error while loading. Please reload this page.

@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from 198ffd3 to 3df39da Compare October 16, 2026 10:22
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch 3 times, most recently from c71041f to d370e05 Compare October 19, 2026 12:22
SasSwart added a commit that referenced this pull request Oct 21, 2026
@SasSwart
feat(coderd/database): add organization AI model config table (#30717)
d14631d
Adds `ai_model_configs`: an organization's AI Governance configuration
for one model on one configured provider. Token prices are its first
settings. Migration `000630` is additive and needs no backfill.

- Rows are soft deleted with a `deleted` flag. A surrogate `id` is the
primary key, and a partial unique index allows one live row per
`(organization_id, provider_id, model)`. Cascades on delete of the
organization or provider. The organization is a column, as in other
organization-scoped tables.
- A check constraint rejects an empty model.
- Pricing per provider ID is what lets `openai-compat` gateways be
priced, and lets providers of the same type have different prices.
- Site-wide prices per provider type stay in the existing
`ai_model_prices` table, unchanged.

Recording which price was used on each token usage record is deferred
until its design settles.

**Used later:** #30718 adds the queries that read and write the table,
and #30719 looks prices up from it. The rest of the diff is generated.

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
@SasSwart SasSwart mentioned this pull request Oct 21, 2026
chore: add ssncferreira and SasSwart as code owners of AI query files #31116
Merged
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from d370e05 to 4fc368b Compare October 21, 2026 06:38
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from 4fc368b to ae98f91 Compare October 21, 2026 07:20
SasSwart added a commit that referenced this pull request Oct 21, 2026
@SasSwart
chore: add ssncferreira and SasSwart as code owners of AI query files (…
409cdf6
…#31116)

Adds @ssncferreira and @SasSwart as code owners of `ai_providers.sql`,
`ai_provider_keys.sql` and `aicostcontrol.sql`, alongside @ibetitsmike
and @johnstcn. The `codersdk/aiproviders*.go` rules are unchanged.

Both current owners are on PTO, which leaves the organization AI model
price stack (#30718, #30719) without anyone who can give the required
code owner review on `aicostcontrol.sql`.

> Generated by Coder Agents on behalf of @SasSwart.
@SasSwart
SasSwart requested a review from ssncferreira October 21, 2026 08:36
This was referenced Oct 21, 2026
feat: use the resolved organization for AI Gateway budgets and cost (1/2) #31120
Merged
feat: price AI Gateway token usage with the request organization (2/2) #31121
Merged
ssncferreira
ssncferreira approved these changes Oct 21, 2026
View reviewed changes

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I think the query is not falling back to the site prices if all prices are set to NULL

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread coderd/database/queries/aicostcontrol.sql
WHERE ai_model_configs.organization_id = sqlc.narg('organization_id')::uuid
AND ai_model_configs.provider_id = @provider_id::uuid
AND ai_model_configs.model IN (@model::text, @provider_model::text)
AND ai_model_configs.deleted = FALSE

@ssncferreira ssncferreira Oct 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this query isn't quite right yet 🤔 There will be cases where an ai_model_configs row exists without any prices, for example, once we backfill it from the Agents models. In that case, it should fall back to the site-wide pricing, but as it stands this branch would pick the row, and its NULL prices would be counted as zero, so the model would be recorded as free.

We should also add a test for this case.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread coderd/database/queries/aicostcontrol.sql Outdated
Comment on lines +41 to +42
-- most specific first: the organization's price for the provider, a site-wide
-- custom price, then the price book. The requested model is preferred over the

@ssncferreira ssncferreira Oct 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be easier to read if this is written as a numbered list (same as before)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread coderd/database/querier_test.go
func aiModelPriceSeed(t *testing.T, keys map[string]string, inputPrice int64) json.RawMessage {
t.Helper()
elem := map[string]any{
"input_price": inputPrice,

@ssncferreira ssncferreira Oct 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we treat a price layer as a whole and never fall back to individual prices, it would be good to add that in a test. The helpers only set input_price, so org and site-wide rows always have NULL for the other prices, and a per-price fallback to the catalog would still pass. Could we add a case where the site-wide row sets all four prices and check that the org row's NULL prices stay NULL?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Base automatically changed from sdlcgraph/aigov-612-3-store to main October 22, 2026 13:58
SasSwart added a commit that referenced this pull request Oct 22, 2026
@SasSwart
feat(coderd): add queries for organization AI model prices (#30718)
da78c88
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.*
SasSwart added 2 commits October 22, 2026 14:01
@SasSwart
feat(coderd): price token usage with organization prices
05ded5b
@SasSwart
fix(coderd/database): fall back to site prices when an organization c…
a192961
…onfig sets none
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-4-lookup branch from ae98f91 to a192961 Compare October 22, 2026 14:07
@SasSwart

SasSwart commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #30718 merged, and addressed @ssncferreira's review in a192961:

  • NULL prices fall back to site prices: the organization branch of GetAIModelPriceForProvider now skips configs that set no price. TestGetAIModelPriceForProviderOrganizationConfig/NoPricesFallsBackToSite covers it and failed before the fix.
  • Prices used as a whole: TestGetAIModelPriceForProviderOrganizationConfig/PriceIsUsedWhole checks that an organization price keeps its NULL prices when the site-wide price sets all four.
  • Numbered list: the query comment lists the lookup order as a numbered list again.
  • AIGOV-716 link: already present as TODO(AIGOV-716) in resolveTokenUsageCost.

The rebase squashed the earlier branch commits into 05ded5b, since they targeted tables that no longer exist.

Comment generated by Coder Agents on behalf of @SasSwart.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@SasSwart
SasSwart merged commit 837c2e1 into main Oct 22, 2026
68 of 73 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@SasSwart
SasSwart deleted the sdlcgraph/aigov-612-4-lookup branch October 22, 2026 15:13
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@ssncferreira ssncferreira ssncferreira approved these changes
@ibetitsmike ibetitsmike Awaiting requested review from ibetitsmike ibetitsmike is a code owner
@johnstcn johnstcn Awaiting requested review from johnstcn johnstcn is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@SasSwart SasSwart

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@SasSwart @ssncferreira
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.