Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8269: fix(runtimes): price Bedrock-served Anthropic ids and DeepSeek-V4.1-Flash (#9561)
Andy Lo-A-Foec11b3375f506
diff --git a/packages/views/runtimes/utils.test.ts b/packages/views/runtimes/utils.test.tsindex c00c9b419b..bdcd317089 100644--- a/packages/views/runtimes/utils.test.ts+++ b/packages/views/runtimes/utils.test.ts@@ -807,8 +807,7 @@ describe("estimateCost", () => {// header comment. Pinning them in tests is what catches a future edit// that copies a price from a near-named neighbour by accident — the// mistake the previous attempt (PR #3170, closed) made.- it("prices deepseek-v4-flash at the official $0.14/$0.28 with ~50× cache-hit discount", () => {- // 1M input × $0.14 + 1M output × $0.28 + 1M cache read × $0.0028 = $0.4228.+ it("prices the retired deepseek-v4-flash alias at the current Flash peak rate", () => {const cost = estimateCost({...zeroUsage,model: "deepseek-v4-flash",@@ -816,12 +815,11 @@ describe("estimateCost", () => {output_tokens: 1_000_000,cache_read_tokens: 1_000_000,});- expect(cost).toBeCloseTo(0.14 + 0.28 + 0.0028, 5);+ expect(cost).toBeCloseTo(0.30 + 1.20 + 0.006, 5);});it("prices the deepseek-chat / deepseek-reasoner aliases at the same rate as deepseek-v4-flash", () => {- // The DeepSeek docs explicitly route both legacy names to v4-flash —- // they must hit the same numbers, not the older $0.27/$1.10 tier.+ // Preserve the existing Flash routing for these historical aliases.const flash = estimateCost({...zeroUsage,model: "deepseek-v4-flash",@@ -843,6 +841,106 @@ describe("estimateCost", () => {).toBeCloseTo(flash, 5);});+ it("prices documented Flash aliases and the 1017 snapshot at the official peak rate", () => {+ const want = { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 };+ for (const model of [+ "deepseek-flash",+ "deepseek/deepseek-flash",+ "custom:deepseek/deepseek-flash[1m]",+ "deepseek-v4-flash",+ "deepseek-v4-flash-vision-exp",+ "deepseek/deepseek-v4-flash-vision-exp",+ "deepseek-chat",+ "deepseek-reasoner",+ "deepseek-v4.1-flash",+ "deepseek/deepseek-v4.1-flash",+ "deepseek/deepseek-v4.1-flash-1017",+ "deepseek-v4.1-flash-1017",+ ]) {+ const usage = {+ ...zeroUsage,+ model,+ input_tokens: 1_000_000,+ output_tokens: 1_000_000,+ cache_read_tokens: 1_000_000,+ cache_write_tokens: 1_000_000,+ };+ expect(estimateCostBreakdown(usage), model).toEqual(want);+ expect(estimateCost(usage), model).toBeCloseTo(1.806, 5);+ expect(collectUnmappedModels([usage]), model).toEqual([]);+ }+ });++ it("leaves unknown Flash variants unpriced", () => {+ for (const model of [+ "deepseek-v4.1-pro", "deepseek-v4.1-flash-preview",+ "deepseek-v4-1-flash", "deepseek-v4.1-flash-1020",+ "deepseek-v4.1-flash-1017-preview", "deepseek-flash-preview",+ "deepseek-v4-flash-preview", "deepseek-v4-flash-vision-exp-extra",+ ]) {+ expect(isModelPriced(model), model).toBe(false);+ expect(estimateCost({ ...zeroUsage, model, input_tokens: 1_000_000 }), model).toBe(0);+ }+ });++ it("prices Bedrock-served Anthropic ids like their bare catalog SKUs", () => {+ // Bedrock reports `us.anthropic.<id>` (region family + vendor prefix) and+ // appends the inference-profile version to snapshots. Before the prefix+ // strip none of these matched a table key, so a Bedrock runtime showed+ // $0.00 for every turn while the tokens were still counted.+ const tokens = {+ input_tokens: 1_000_000,+ output_tokens: 1_000_000,+ cache_read_tokens: 1_000_000,+ cache_write_tokens: 1_000_000,+ };+ const bareSonnet5 = estimateCost({+ ...zeroUsage,+ model: "claude-sonnet-5",+ ...tokens,+ });+ for (const model of [+ "us.anthropic.claude-sonnet-5",+ "eu.anthropic.claude-sonnet-5",+ "global.anthropic.claude-sonnet-5",+ "anthropic.claude-sonnet-5",+ "custom:bedrock/us.anthropic.claude-sonnet-5-v1:0[1m]",+ ]) {+ expect(estimateCost({ ...zeroUsage, provider: "claude", model, ...tokens })).toBeCloseTo(+ bareSonnet5,+ 5,+ );+ }+ // Snapshot + profile version suffix resolves to the same family tier.+ expect(+ estimateCost({+ ...zeroUsage,+ provider: "claude",+ model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0",+ input_tokens: 1_000_000,+ }),+ ).toBeCloseTo(3, 5);+ // A non-Anthropic Bedrock model must stay unmapped instead of borrowing a+ // Claude rate.+ expect(isModelPriced("us.amazon.nova-pro-v1:0", "amazon")).toBe(false);+ });++ it("only strips Bedrock versions from recognized Anthropic model ids", () => {+ for (const model of [+ "us.anthropic.claude-sonnet-6",+ "us.anthropic.claude-sonnet-5-preview",+ "global.anthropic.claude-opus-5-mini",+ "us.anthropic.claude-sonnet-5-preview-v1:0",+ "us.anthropic.claude-sonnet-5-v1:0junk",+ "us.anthropic.claude-sonnet-5-v1:0[1m][2m]",+ "gpt-6.1-sol-v1:0",+ "claude-sonnet-5-v1:0",+ "us.anthropic.gpt-6.1-sol-v1:0",+ ]) {+ expect(isModelPriced(model), model).toBe(false);+ }+ });+it("prices kimi-k2.6 at the official $0.95 / $4.00 tier (not the K2 tier)", () => {// Moonshot's K2.6 page is the only authoritative source today; K2.6 is// explicitly NOT priced like K2. 1M input × $0.95 + 1M output × $4.00 = $4.95.diff --git a/packages/views/runtimes/utils.ts b/packages/views/runtimes/utils.tsindex 90af355419..d11529fcd8 100644--- a/packages/views/runtimes/utils.ts+++ b/packages/views/runtimes/utils.ts@@ -324,18 +324,21 @@ const MODEL_PRICING: Record<"gpt-4o-mini": { input: 0.15, output: 0.60, cacheRead: 0.075, cacheWrite: 0.15 },"gpt-4o": { input: 2.50, output: 10, cacheRead: 1.25, cacheWrite: 2.50 },- // -- DeepSeek (api-docs.deepseek.com/quick_start/pricing).- // The official catalog lists exactly two current SKUs; `deepseek-chat`- // and `deepseek-reasoner` are aliases that route to `deepseek-v4-flash`- // (non-thinking and thinking mode respectively) per the same page.- // `deepseek-v4-pro` is currently under a 75%-off promo that ends- // 2026-05-31 15:59 UTC; we price at the post-promo standard rate- // ($1.74/$3.48) so the dashboard does not jump 4× on June 1 — accept- // a brief over-estimate during the promo over a sudden cliff after it. --- "deepseek-v4-flash": { input: 0.14, output: 0.28, cacheRead: 0.0028, cacheWrite: 0.14 },+ // -- DeepSeek Flash: api-docs.deepseek.com/quick_start/pricing/ (2026-10-27).+ // The English sheet supplies USD; the Chinese sheet lists CNY. The official+ // Flash name and retired v4 aliases serve V4.1-Flash-1017. Preserve the+ // existing Flash routing for chat/reasoner. Only known spellings and the+ // 1017 snapshot resolve. Use peak USD rates because rollups lack each+ // request's pricing window; off-peak costs half as much and reseller prices+ // can differ. Cache writes bill as ordinary input. Mirror pricing.go.+ "deepseek-flash": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-v4-flash": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-v4-flash-vision-exp": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-v4.1-flash": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-v4.1-flash-1017": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-chat": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },+ "deepseek-reasoner": { input: 0.30, output: 1.20, cacheRead: 0.006, cacheWrite: 0.30 },"deepseek-v4-pro": { input: 1.74, output: 3.48, cacheRead: 0.0145, cacheWrite: 1.74 },- "deepseek-chat": { input: 0.14, output: 0.28, cacheRead: 0.0028, cacheWrite: 0.14 },- "deepseek-reasoner": { input: 0.14, output: 0.28, cacheRead: 0.0028, cacheWrite: 0.14 },// -- Moonshot Kimi (kimi.com/resources/kimi-k2-6-pricing).// Only K2.6 is on the official price sheet today; earlier K2 variants@@ -696,6 +699,21 @@ function canonicalCandidates(model: string): string[] {// semantic, so we leave `gpt-5.4` etc. alone.const canonAnthropic = (s: string) =>s.startsWith("claude-") ? s.replace(/\./g, "-") : s;+ // Amazon Bedrock prefixes Anthropic ids with a region family (`us.`, `eu.`,+ // `apac.`, `global.`) and the vendor (`anthropic.`), and appends the+ // inference-profile version to snapshot ids (`...-20250929-v1:0`). A Bedrock+ // turn reports `us.anthropic.claude-sonnet-5`, which matched no table key at+ // all, so every such turn rendered as $0 plus an "unmapped" warning. Only the+ // vendor-prefixed forms are rewritten: `us.amazon.nova-*` and friends stay+ // unmapped rather than borrowing a Claude rate. Mirrors `normalizeBedrockID`+ // in server/internal/metrics/pricing.go.+ const stripBedrock = (s: string) => {+ const prefix = /^(?:(?:us|eu|apac|global)\.)?anthropic\.(claude-)/;+ if (!prefix.test(s)) return s;+ return s+ .replace(prefix, "$1")+ .replace(/-v\d+:\d+(\[[^\]]+\])?$/, "$1");+ };// Trailing context-window tag (`claude-opus-4-7[1m]`). Same family,// same price tier — see resolver comment above for the 1M-context// pricing trade-off.@@ -714,15 +732,21 @@ function canonicalCandidates(model: string): string[] {const noProvider = stripProvider(raw);const dashed = canonAnthropic(noProvider);const noTag = stripContextTag(dashed);+ // Bedrock ids reach us with dots (`us.anthropic.claude-…`) and a+ // `-v1:0` profile version, so they need the prefix strip BEFORE the+ // dot↔dash normalisation that `canonAnthropic` only applies to `claude-*`.+ const bedrock = canonAnthropic(stripContextTag(stripBedrock(noProvider)));push(raw);push(noProvider);push(dashed);push(noTag);+ push(bedrock);push(stripDate(raw));push(stripDate(noProvider));push(stripDate(dashed));push(stripDate(noTag));+ push(stripDate(bedrock));push(canonGemini(noTag));push(canonGemini(stripDate(noTag)));canonicalCandidatesCache.set(model, out);diff --git a/server/internal/metrics/pricing.go b/server/internal/metrics/pricing.goindex 141efc3c08..ea93ae4edf 100644--- a/server/internal/metrics/pricing.go+++ b/server/internal/metrics/pricing.go@@ -64,9 +64,14 @@ var modelPrices = map[string]ModelPrice{"anthropic:claude-sonnet-4.5": {Provider: "anthropic", Model: "claude-sonnet-4.5", InputPerM: 3.00, CacheReadPerM: 0.30, CacheWritePerM: 3.75, OutputPerM: 15.00},"anthropic:claude-haiku-4.5": {Provider: "anthropic", Model: "claude-haiku-4.5", InputPerM: 1.00, CacheReadPerM: 0.10, CacheWritePerM: 1.25, OutputPerM: 5.00},"deepseek:v4-pro": {Provider: "deepseek", Model: "v4-pro", InputPerM: 1.74, CacheReadPerM: 0.0145, CacheWritePerM: 1.74, OutputPerM: 3.48},- "deepseek:v4-flash": {Provider: "deepseek", Model: "v4-flash", InputPerM: 0.56, CacheReadPerM: 0.0112, CacheWritePerM: 0.56, OutputPerM: 1.12},- "minimax:m2.7": {Provider: "minimax", Model: "m2.7", InputPerM: 0.30, CacheReadPerM: 0.06, CacheWritePerM: 0.375, OutputPerM: 1.20},- "minimax:m2.7-highspeed": {Provider: "minimax", Model: "m2.7-highspeed", InputPerM: 0.60, CacheReadPerM: 0.06, CacheWritePerM: 0.375, OutputPerM: 2.40},+ // DeepSeek Flash: api-docs.deepseek.com/quick_start/pricing/ (2026-10-27).+ // The English sheet supplies USD; the Chinese sheet lists CNY. Use peak+ // USD rates because rollups lack each request's pricing window. Off-peak+ // costs half as much; reseller prices can also differ. Cache writes bill+ // as ordinary input. Mirror packages/views/runtimes/utils.ts.+ "deepseek:v4.1-flash": {Provider: "deepseek", Model: "v4.1-flash", InputPerM: 0.30, CacheReadPerM: 0.006, CacheWritePerM: 0.30, OutputPerM: 1.20},+ "minimax:m2.7": {Provider: "minimax", Model: "m2.7", InputPerM: 0.30, CacheReadPerM: 0.06, CacheWritePerM: 0.375, OutputPerM: 1.20},+ "minimax:m2.7-highspeed": {Provider: "minimax", Model: "m2.7-highspeed", InputPerM: 0.60, CacheReadPerM: 0.06, CacheWritePerM: 0.375, OutputPerM: 2.40},// Google standard token prices: ai.google.dev/gemini-api/docs/pricing// (2026-10-19). Mirror packages/views/runtimes/utils.ts.// Argon's intro rate expires 2026-11-30; Flash 3.6/3.8, Robotics and@@ -222,8 +227,11 @@ var modelAliasRules = []struct {{regexp.MustCompile(`claude-sonnet-4[-.]6|claude-4[-.]6-sonnet`), "anthropic:claude-sonnet-4.6"},{regexp.MustCompile(`claude-sonnet-4[-.]5|claude-4[-.]5-sonnet`), "anthropic:claude-sonnet-4.5"},{regexp.MustCompile(`claude-haiku-4[-.]5`), "anthropic:claude-haiku-4.5"},+ // The official Flash name and retired v4 aliases serve V4.1-Flash-1017.+ // Preserve chat/reasoner's existing Flash routing. Only known spellings+ // and the 1017 snapshot resolve, matching the frontend's exact keys.+ {regexp.MustCompile(`(^|/|:)deepseek-(?:flash|v4-flash(?:-vision-exp)?|v4\.1-flash(?:-1017)?|chat|reasoner)$`), "deepseek:v4.1-flash"},{regexp.MustCompile(`deepseek-v4-pro`), "deepseek:v4-pro"},- {regexp.MustCompile(`deepseek-v4-flash|^deepseek-chat$|^deepseek-reasoner$`), "deepseek:v4-flash"},{regexp.MustCompile(`minimax-m2[.]7.*highspeed|highspeed.*minimax-m2[.]7`), "minimax:m2.7-highspeed"},{regexp.MustCompile(`minimax-m2[.]7`), "minimax:m2.7"},// Each Gemini SKU is exact: an image/audio/preview variant must never@@ -311,8 +319,32 @@ func matchModelAlias(model string) (ModelPrice, bool) {return ModelPrice{}, false}+// Bedrock Anthropic ids may follow routing prefixes such as `custom:bedrock/`.+// Accept the same routing segments as the frontend's stripProvider, and only+// normalize Claude ids under the recognized Bedrock vendor/region prefixes.+var bedrockAnthropicIDRe = regexp.MustCompile(`^(?:[a-z][a-z0-9_-]*[/:])*(?:(?:us|eu|apac|global)\.)?anthropic\.(claude-)`)++// Preserve a single context tag for the existing matcher while removing the+// Bedrock version (`...-v1:0[1m]`). Multiple tags remain unrecognized.+var bedrockVersionSuffixRe = regexp.MustCompile(`-v\d+:\d+(\[[^\]]+\])?$`)++// normalizeBedrockID rewrites a Bedrock-served Anthropic id into the bare+// catalog id the price rows are keyed by, so `us.anthropic.claude-sonnet-5`+// prices as `claude-sonnet-5` — the same SKU. It is a no-op for every other id,+// including non-Anthropic Bedrock models (`us.amazon.nova-*`), which stay+// unmapped rather than inheriting a Claude rate. Keep in step with+// `stripBedrock` in packages/views/runtimes/utils.ts: the dashboard and this+// metric must agree on what is priced.+func normalizeBedrockID(model string) string {+ if !bedrockAnthropicIDRe.MatchString(model) {+ return model+ }+ model = bedrockAnthropicIDRe.ReplaceAllString(model, "${1}")+ return bedrockVersionSuffixRe.ReplaceAllString(model, "${1}")+}+func PriceForModelAlias(model string) (ModelPrice, bool) {- model = strings.ToLower(strings.TrimSpace(model))+ model = normalizeBedrockID(strings.ToLower(strings.TrimSpace(model)))if price, ok := matchModelAlias(model); ok {return price, true}diff --git a/server/internal/metrics/pricing_test.go b/server/internal/metrics/pricing_test.goindex 6c33fd23f2..8b3b3e0e5a 100644--- a/server/internal/metrics/pricing_test.go+++ b/server/internal/metrics/pricing_test.go@@ -653,3 +653,113 @@ func TestPriceForModelAliasAnthropicOpus55(t *testing.T) {}}}++// TestPriceForModelAliasBedrockAnthropicIDs pins the Bedrock shapes. Bedrock+// reports Anthropic ids with a region family and vendor prefix plus a version+// suffix. Newer Claude rules reject that suffix until it is normalized.+func TestPriceForModelAliasBedrockAnthropicIDs(t *testing.T) {+ cases := []struct {+ model string+ want ModelPrice+ }{+ {+ model: "us.anthropic.claude-sonnet-5",+ want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},+ },+ {+ // Europe / APAC / global profiles are the same SKU at the same rate.+ model: "eu.anthropic.claude-sonnet-5",+ want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},+ },+ {+ model: "global.anthropic.claude-opus-5",+ want: ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},+ },+ {+ // Vendor prefix without a region family.+ model: "anthropic.claude-haiku-5-5",+ want: ModelPrice{Provider: "anthropic", Model: "claude-haiku-5-5", InputPerM: 0.1, CacheReadPerM: 0.01, CacheWritePerM: 0.125, OutputPerM: 0.5},+ },+ {+ // Snapshot id with the inference-profile version suffix.+ model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0",+ want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-4.5", InputPerM: 3, CacheReadPerM: 0.3, CacheWritePerM: 3.75, OutputPerM: 15},+ },+ {+ model: "custom:bedrock/us.anthropic.claude-sonnet-5-v1:0[1m]",+ want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},+ },+ }++ for _, tc := range cases {+ got, ok := PriceForModelAlias(tc.model)+ if !ok {+ t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)+ }+ if got != tc.want {+ t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)+ }+ }++ // Normalising the prefix must not invent a rate: a later SKU, a+ // non-Anthropic Bedrock model, or a qualifier that is its own SKU all stay+ // unmapped rather than borrowing the nearest Claude tier.+ for _, model := range []string{+ "us.anthropic.claude-sonnet-6",+ "us.anthropic.claude-sonnet-5-preview",+ "us.amazon.nova-pro-v1:0",+ "global.anthropic.claude-opus-5-mini",+ "us.anthropic.claude-sonnet-5-preview-v1:0",+ "us.anthropic.claude-sonnet-5-v1:0junk",+ "us.anthropic.claude-sonnet-5-v1:0[1m][2m]",+ "gpt-6.1-sol-v1:0",+ "claude-sonnet-5-v1:0",+ "us.anthropic.gpt-6.1-sol-v1:0",+ } {+ if got, ok := PriceForModelAlias(model); ok {+ t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)+ }+ }+}++// TestPriceForModelAliasDeepSeekV41Flash pins the V4.1 Flash row, including the+// dated OpenRouter snapshot that DeepSeek documents as the served model for the+// undated id (same SKU, same rate — one row, not two).+func TestPriceForModelAliasDeepSeekV41Flash(t *testing.T) {+ want := ModelPrice{Provider: "deepseek", Model: "v4.1-flash", InputPerM: 0.30, CacheReadPerM: 0.006, CacheWritePerM: 0.30, OutputPerM: 1.20}+ for _, model := range []string{+ "deepseek-flash",+ "deepseek/deepseek-flash",+ "custom:deepseek/deepseek-flash[1m]",+ "deepseek-v4-flash",+ "deepseek-v4-flash-vision-exp",+ "deepseek/deepseek-v4-flash-vision-exp",+ "deepseek-chat",+ "deepseek-reasoner",+ "deepseek-v4.1-flash",+ "deepseek/deepseek-v4.1-flash",+ "deepseek/deepseek-v4.1-flash-1017",+ "deepseek-v4.1-flash-1017",+ } {+ got, ok := PriceForModelAlias(model)+ if !ok {+ t.Errorf("PriceForModelAlias(%q) did not resolve", model)+ continue+ }+ if got != want {+ t.Errorf("PriceForModelAlias(%q) = %+v, want %+v", model, got, want)+ }+ }++ // Only documented Flash aliases and the known 1017 snapshot are priced.+ for _, model := range []string{+ "deepseek-v4.1-pro", "deepseek-v4.1-flash-preview",+ "deepseek-v4-1-flash", "deepseek-v4.1-flash-1020",+ "deepseek-v4.1-flash-1017-preview", "deepseek-flash-preview",+ "deepseek-v4-flash-preview", "deepseek-v4-flash-vision-exp-extra",+ } {+ if got, ok := PriceForModelAlias(model); ok {+ t.Errorf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)+ }+ }+}