mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add experimental CLI to price unpriced AI models (#27926)
## Description AI Gateway computes the cost of an interception from `ai_model_prices`, which is seeded on every server start from a price book embedded in the binary. A model the price book does not cover records a NULL cost, so its spend is invisible to cost reporting and is not enforced against budgets. The only fix was to wait for a Coder release that added the model. This adds an experimental CLI, backed by an experimental HTTP endpoint, for pricing those models. Models the price book already covers are rejected, because the seeder re-applies the book on every start and would overwrite an operator price. Support for custom pricing will be handled in https://linear.app/codercom/issue/AIGOV-589/extend-experimental-cli-command-to-set-custom-ai-model-prices. ## Commands ``` coder exp ai-model-prices list [--provider] [--model] coder exp ai-model-prices update [file|-] [--provider] [--model] [--input-price] [--output-price] [--cache-read-price] [--cache-write-price] [--yes] ``` ## Changes - Add `GET` and `POST /api/experimental/ai/model-prices`, gated behind the AI Bridge entitlement and the existing `ai_model_price` RBAC resource. - Add a `GetAIModelPrices` query with optional `provider` and `model` filters applied in SQL. - Validate the whole request before writing anything, so one bad entry cannot leave the table half updated, and report every problem at once. - Reject prices for models the embedded price book already covers, through a new `prices.IsDefaultPriced`. - Add the `coder exp ai-model-prices` command with `list` and `update`. `update` accepts a JSON document or the single-model flags and prints a plan, asking to confirm unless the document is piped in or `--yes` is passed. - Consolidate the supported provider list into `coderd/aibridge/prices/providers` so the price generator and the server share one definition. - Add `codersdk` types and client methods for both endpoints, and bound the request body at 1 MiB. - Document the command in the AI Gateway cost controls page. Closes https://linear.app/codercom/issue/AIGOV-567/experimental-cli-command-to-set-prices-for-unpriced-ai-models > [!NOTE] > Initially generated by Claude Opus 5, modified and reviewed by @ssncferreira
This commit is contained in:
@@ -23,20 +23,9 @@ import (
|
||||
"sort"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// supportedProviders lists the providers we ship prices for. Adding a
|
||||
// provider here is enough to include it on the next regeneration.
|
||||
var supportedProviders = []string{
|
||||
"anthropic",
|
||||
"azure",
|
||||
"bedrock",
|
||||
"copilot",
|
||||
"google",
|
||||
"openai",
|
||||
"openrouter",
|
||||
"vercel",
|
||||
}
|
||||
"github.com/coder/coder/v2/coderd/aibridge/prices/providers"
|
||||
)
|
||||
|
||||
// upstreamProvider is the subset of a models.dev per-provider entry we read.
|
||||
type upstreamProvider struct {
|
||||
@@ -135,7 +124,7 @@ func readUpstream(path string) (map[string]upstreamProvider, error) {
|
||||
}
|
||||
|
||||
func runPrices(upstream map[string]upstreamProvider) error {
|
||||
rows, err := convert(upstream, supportedProviders)
|
||||
rows, err := convert(upstream, providers.SupportedStrings())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -145,7 +134,7 @@ func runPrices(upstream map[string]upstreamProvider) error {
|
||||
if err := write(os.Stdout, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = fmt.Fprintf(os.Stderr, "aibridgepricesgen: wrote %d prices for %d provider(s)\n", len(rows), len(supportedProviders))
|
||||
_, _ = fmt.Fprintf(os.Stderr, "aibridgepricesgen: wrote %d prices for %d provider(s)\n", len(rows), len(providers.Supported))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -169,12 +158,12 @@ func runCatalog(upstream map[string]upstreamProvider) error {
|
||||
// providers. If any configured provider is absent from the upstream payload,
|
||||
// every missing provider is reported and the function returns an error so the
|
||||
// caller doesn't ship an incomplete seed.
|
||||
func convert(upstream map[string]upstreamProvider, providers []string) ([]priceRow, error) {
|
||||
func convert(upstream map[string]upstreamProvider, providerIDs []string) ([]priceRow, error) {
|
||||
var (
|
||||
rows []priceRow
|
||||
missing []string
|
||||
)
|
||||
for _, providerID := range providers {
|
||||
for _, providerID := range providerIDs {
|
||||
provider, ok := upstream[providerID]
|
||||
if !ok || len(provider.Models) == 0 {
|
||||
missing = append(missing, providerID)
|
||||
|
||||
Reference in New Issue
Block a user