Skip to content

feat: add organization unpriced AI models endpoint - #31753

Merged
ssncferreira merged 2 commits into
mainfrom
ssncf/unpriced-models-endpoint
Nov 17, 2026
Merged

ssncferreira merged 2 commits into
mainfrom
ssncf/unpriced-models-endpoint

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adds the experimental GET /api/experimental/organizations/{organization}/ai/model-prices/unpriced endpoint behind the "N models not set up" warning on the organization AI spend page (Figma). It lists the models used in the period that still hold no price in the organization, using the query from this stack.

Changes

  • Add the organizationUnpricedAIModels handler and ExperimentalClient.OrganizationUnpricedAIModels:
    • Reuses aiSpendRetainedPeriod, since token usage is subject to data retention.
    • Returns the applied period, count, and models (provider name, display name, icon and type, model, token count), most unpriced tokens first.
    • Requires read on the organization's model prices (owners and organization admins), so the UI only requests it for users who can set prices.
  • Add handler tests for the response, period validation, organization scoping and role access.

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

Closes AIGOV-730

Note

Generated by Coder Agents on behalf of @ssncferreira.

@linear-code

linear-code Bot commented Nov 4, 2026

Copy link
Copy Markdown

AIGOV-730

@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from a88e1cb to 50901a6 Compare November 6, 2026 10:54
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 50901a6 to 25fcdf7 Compare November 6, 2026 11:12
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 25fcdf7 to f4fca88 Compare November 6, 2026 11:15
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from f4fca88 to 4484f55 Compare November 6, 2026 11:28
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 4484f55 to 83ad095 Compare November 6, 2026 11:33
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 83ad095 to 2638c0a Compare November 6, 2026 11:38
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 2638c0a to 0a301d4 Compare November 6, 2026 11:48
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 0a301d4 to 577e27c Compare November 6, 2026 11:50
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 577e27c to 9721677 Compare November 6, 2026 12:00
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 9721677 to 909764a Compare November 6, 2026 12:06
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 909764a to fe61228 Compare November 6, 2026 12:07
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from fe61228 to 87c0ba1 Compare November 6, 2026 12:14
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from c7ed743 to f4e2209 Compare November 6, 2026 16:16
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from f4e2209 to 20109cd Compare November 6, 2026 16:18
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 20109cd to edb1b37 Compare November 6, 2026 16:18
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch 3 times, most recently from ca467db to 243f1ce Compare November 11, 2026 13:29
Comment thread enterprise/coderd/aimodelprices.go Resolved
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 243f1ce to 21028c4 Compare November 12, 2026 10:20
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 21028c4 to a4e5815 Compare November 13, 2026 10:58
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from a4e5815 to 91e4a81 Compare November 13, 2026 12:17
@ssncferreira
ssncferreira temporarily deployed to docs-preview November 13, 2026 12:21 — with GitHub Actions Inactive
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from 91e4a81 to cb2baf0 Compare November 13, 2026 13:08
@ssncferreira
ssncferreira temporarily deployed to docs-preview November 13, 2026 13:11 — with GitHub Actions Inactive
Base automatically changed from ssncf/unpriced-models-org-scope to main November 13, 2026 13:21
@ssncferreira
ssncferreira force-pushed the ssncf/unpriced-models-endpoint branch from cb2baf0 to 03de7ab Compare November 13, 2026 13:21
@ssncferreira
ssncferreira temporarily deployed to main November 13, 2026 13:21 — with GitHub Actions Inactive
@ssncferreira
ssncferreira temporarily deployed to docs-preview November 13, 2026 13:24 — with GitHub Actions Inactive
Comment thread docs/reference/api/schemas.md Resolved
//
// @Summary List organization unpriced AI models
// @Description Returns, for each provider, the requested models with token usage recorded without a price in the organization over the period, most unpriced tokens first. Only models without a price now are returned. A model has a price when it has a site-wide price for the provider's type or an organization price for the provider. Usage is attributed to the organization through its effective group.
// @Description The optional period_start and period_end query parameters bound the period and are interpreted as UTC. They must be provided together and span at most 31 days. When both are omitted, the current budget period is used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: wrong default presented as fixed (Fact Checker).

The handler calls aiSpendRetainedPeriod (enterprise/coderd/aimodelprices.go:378). When the AI Gateway retention (default 60d, codersdk/deployment.go:2012-2019) is shorter than the budget period, the default start is clamped to the retention window. An explicit period_start older than that window returns 400 (enterprise/coderd/aibridge.go:1267-1291). The sibling export endpoint documents both behaviors at enterprise/coderd/aibridge.go:1296, and this description does not.

Suggested change
// @Description The optional period_start and period_end query parameters bound the period and are interpreted as UTC. They must be provided together and span at most 31 days. When both are omitted, the current budget period is used.
// @Description The optional period_start and period_end query parameters bound the period and are interpreted as UTC. They must be provided together and span at most 31 days. When both are omitted, the current budget period is used, narrowed to the AI Gateway data retention window when retention is shorter than the period. An explicit period_start older than that window is rejected with 400.

Run make gen afterwards so coderd/apidoc and the Swagger output pick up the new text.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This will be addressed in a follow-up

Comment thread docs/reference/api/schemas.md Resolved
@coderagents

coderagents Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Check

Blocking

  • docs/reference/api/schemas.md (open) - The period_end row of codersdk.UnpricedAIModels says "current budget period", but explicit period_start and period_end are echoed. Fix the shared comment in codersdk/aibridge.go:64-68 to "applied period", then run make gen.
  • enterprise/coderd/aimodelprices.go:354 (feeds the served Swagger spec, not the rendered reference) - The endpoint description says the default period is the budget period. It is clamped to the AI Gateway retention window, and an explicit period_start older than that returns 400. Add both, then run make gen.
Findings for agents
- id: accuracy-1
  persona: accuracy
  class: wrong
  blocking: true
  file: docs/reference/api/schemas.md
  line: 16975
  quote: "Period end is the exclusive upper bound of the current budget period."
  evidence:
    - enterprise/coderd/aimodelprices.go:398-399
    - enterprise/coderd/aimodelprices_test.go (ExplicitPeriodWithoutUsage)
    - codersdk/aibridge.go:67-68
  rule: null
  problem: The response echoes an explicit caller window, so "current budget period" is false for that request.
  fix: "Period end is the exclusive upper bound of the applied period." (change at codersdk/aibridge.go, then make gen)
  verified: true
- id: accuracy-2
  persona: accuracy
  class: wrong
  blocking: true
  file: enterprise/coderd/aimodelprices.go
  line: 354
  quote: "When both are omitted, the current budget period is used."
  evidence:
    - enterprise/coderd/aimodelprices.go:378
    - enterprise/coderd/aibridge.go:1267-1291
    - enterprise/coderd/aibridge.go:1296
    - codersdk/deployment.go:2012-2019
  rule: null
  problem: The default start is clamped to the retention window when retention is shorter than the period, and an explicit old period_start returns 400. Neither is stated.
  fix: See the suggestion block on the inline comment.
  verified: true
- id: audience-1
  persona: audience
  class: suggestion
  blocking: false
  file: docs/reference/api/schemas.md
  line: 16942
  quote: "| `model`                 | string"
  evidence:
    - codersdk/aimodelprices.go:144-160
    - coderd/database/queries/aicostcontrol.sql:779-822
    - docs/ai-governance/ai-gateway/cost-controls.md:222
  rule: null
  problem: The model, provider_name, and provider_type rows have no description, so a reader cannot tell the model is the requested one.
  fix: Add field comments in codersdk/aimodelprices.go, then make gen.
  verified: true
- id: learner-1
  persona: learner
  class: wrong
  blocking: true
  merged_into: accuracy-1
- id: consistency-1
  persona: consistency
  class: suggestion
  blocking: false
  merged_into: accuracy-1

Automated review via Coder Agents. Reply to a finding's thread with the reason if the finding is wrong.

@SasSwart SasSwart left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!


// Only roles that can read the organization's prices can list its
// unpriced models.
t.Run("RoleAccess", func(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I like this personally. Just asking based on codebase patterns. Should this be tested in dbauthz instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

dbauthz already checks the query asks for ResourceAiModelPrice.InOrg(org) and roles_test covers which roles have it. This test covers the endpoint wiring: the handler's 403 and the 404 for another org's admin. It follows the other AI spend endpoints.

This branch had an error being deployed

1 failed and 1 inactive deployments
docs-preview — c047b5ae Deployed Nov 17, 2026 by ssncferreira via cleanup #168
main — c047b5ae Deployed Nov 17, 2026 by ssncferreira via cla #258745
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