From 0252311d322eb08f7a556fa08a22728c7c1f73b0 Mon Sep 17 00:00:00 2001 From: Matthew Charles Busel Date: Sun, 15 Nov 2026 14:13:33 -0500 Subject: [PATCH] fix(server): price ledger runs by model, not a per-1K provider table ProxyServer.estimateCost kept its own per-1K table that priced every OpenAI run at $5/$15 per 1M (GPT-4o is $2.50/$10) and every Claude run at Sonnet rates regardless of model, so a Haiku 4.5 run was billed 3x and an Opus 4.6 run at 60% of list. Price by request.model through telemetry.ts MODEL_PRICING (which takes its Claude and GPT-5 rows from model-pricing.ts), and fall back to a per-provider row only when the model has no price. Adds a regression test and a weekly drift check on src/ against the providers' pricing pages. --- .github/workflows/llm-prices.yml | 29 +++++++++++ __tests__/server-estimate-cost.test.ts | 68 ++++++++++++++++++++++++++ src/server.ts | 21 +++----- src/telemetry.ts | 32 ++++++++++++ 4 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/llm-prices.yml create mode 100644 __tests__/server-estimate-cost.test.ts diff --git a/.github/workflows/llm-prices.yml b/.github/workflows/llm-prices.yml new file mode 100644 index 0000000..6cd331e --- /dev/null +++ b/.github/workflows/llm-prices.yml @@ -0,0 +1,29 @@ +name: llm-prices + +# Fails when a hard-coded model price in src/ drifts from the provider's +# official pricing page (Anthropic, OpenAI, Google, DeepSeek). Prices are +# compared per 1M tokens, so per-1K tables are checked too. +on: + pull_request: + paths: + - 'src/**' + push: + branches: [main] + paths: + - 'src/**' + schedule: + - cron: '0 9 * * 1' + workflow_dispatch: + +permissions: + contents: read + +jobs: + price-drift: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: Mattbusel/llm-prices@v1 + with: + paths: 'src/**' + fail-on: drift diff --git a/__tests__/server-estimate-cost.test.ts b/__tests__/server-estimate-cost.test.ts new file mode 100644 index 0000000..2fdcd43 --- /dev/null +++ b/__tests__/server-estimate-cost.test.ts @@ -0,0 +1,68 @@ +/** + * Guards the ledger cost recorded by ProxyServer (src/server.ts). + * + * server.ts used to keep its own per-1K table that priced every OpenAI + * run at $5/$15 per 1M (the old GPT-4o launch price) and every Claude + * run at Sonnet rates, whatever the model. It now prices by model id + * through telemetry.ts and only falls back to a provider rate for + * models with no row. + */ +import { describe, it, expect } from 'vitest'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { + MODEL_PRICING, + PROVIDER_FALLBACK_PRICING, + estimateCost, + estimateProviderCost, +} from '../src/telemetry.js'; + +const M = 1_000_000; + +describe('estimateProviderCost', () => { + it('prices gpt-4o at its list price, not $5/$15', () => { + expect(estimateProviderCost('openai', 'gpt-4o', M, M)).toBeCloseTo(2.5 + 10, 10); + }); + + it('prices by model, not by provider', () => { + expect(estimateProviderCost('anthropic', 'claude-haiku-4-5', M, M)).toBeCloseTo(1 + 5, 10); + expect(estimateProviderCost('anthropic', 'claude-opus-4-6', M, M)).toBeCloseTo(5 + 25, 10); + expect(estimateProviderCost('openai', 'gpt-4o-mini', M, M)).toBeCloseTo(0.15 + 0.6, 10); + }); + + it('agrees with estimateCost for every known model', () => { + for (const model of Object.keys(MODEL_PRICING)) { + if (model === 'default') continue; + expect(estimateProviderCost('openai', model, 1234, 567), model).toBeCloseTo( + estimateCost(model, 1234, 567), + 12, + ); + } + }); + + it('accepts provider-prefixed ids', () => { + expect(estimateProviderCost('openrouter', 'anthropic/claude-sonnet-4-6', M, M)).toBeCloseTo(3 + 15, 10); + }); + + it('falls back to the provider row for unknown models', () => { + const openai = PROVIDER_FALLBACK_PRICING['openai']!; + expect(estimateProviderCost('openai', 'some-new-model', M, M)).toBeCloseTo(openai.input + openai.output, 10); + expect(estimateProviderCost('openai', undefined, M, 0)).toBeCloseTo(openai.input, 10); + const anthropic = PROVIDER_FALLBACK_PRICING['anthropic']!; + expect(estimateProviderCost('unknown-provider', 'x', M, M)).toBeCloseTo(anthropic.input + anthropic.output, 10); + }); + + it('fallback rows match the model rows they name', () => { + expect(PROVIDER_FALLBACK_PRICING['openai']).toEqual(MODEL_PRICING['gpt-4o']); + expect(PROVIDER_FALLBACK_PRICING['anthropic']).toEqual(MODEL_PRICING['claude-sonnet-4-6']); + }); +}); + +describe('server.ts', () => { + const src = fs.readFileSync(path.join(__dirname, '../src/server.ts'), 'utf-8'); + + it('has no hand-kept price table of its own', () => { + expect(src).not.toMatch(/input:\s*0\.00\d+,\s*output:\s*0\.0\d+/); + expect(src).toMatch(/estimateProviderCost\(provider, model,/); + }); +}); diff --git a/src/server.ts b/src/server.ts index 76b4cbe..918cfc1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -58,6 +58,7 @@ import { import { RelayPlaneMiddleware } from './middleware.js'; import { type RelayPlaneConfig, type MeshConfig, resolveConfig, resolveMeshConfig } from './relay-config.js'; import { initMesh, type MeshHandle } from './mesh.js'; +import { estimateProviderCost } from './telemetry.js'; /** * Proxy server configuration @@ -1110,7 +1111,7 @@ export class ProxyServer { const latencyMs = Date.now() - startTime; if (providerResponse.success) { - const costUsd = this.estimateCost(provider, providerResponse.usage); + const costUsd = this.estimateCost(provider, request.model, providerResponse.usage); // Complete run successfully await this.ledger.completeRun(runId, { @@ -1349,25 +1350,17 @@ export class ProxyServer { } /** - * Estimate cost based on provider and usage + * Estimate cost from the run's model, falling back to the provider rate. + * Prices come from telemetry.ts MODEL_PRICING (model-pricing.ts for + * Claude and GPT-5 rows), so this path agrees with the dashboard. */ private estimateCost( provider: string, + model: string | undefined, usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number } ): number { if (!usage) return 0; - - // Approximate pricing per 1K tokens - const pricing: Record = { - anthropic: { input: 0.003, output: 0.015 }, // Claude 3.5 Sonnet - openai: { input: 0.005, output: 0.015 }, // GPT-4o - openrouter: { input: 0.003, output: 0.015 }, // Varies - }; - - const rates = pricing[provider] ?? { input: 0.003, output: 0.015 }; - return ( - (usage.prompt_tokens / 1000) * rates.input + (usage.completion_tokens / 1000) * rates.output - ); + return estimateProviderCost(provider, model, usage.prompt_tokens, usage.completion_tokens); } /** diff --git a/src/telemetry.ts b/src/telemetry.ts index e03d00b..994816f 100644 --- a/src/telemetry.ts +++ b/src/telemetry.ts @@ -179,6 +179,38 @@ export function estimateCost(model: string, inputTokens: number, outputTokens: n return inputCost + outputCost; // Full precision , rounding happens at display time } +/** + * Per-provider rates (USD per 1M tokens) used only when a run's model has + * no row in MODEL_PRICING. Each row is the list price of the model named + * in its comment, so the fallback is a real price rather than a guess. + */ +export const PROVIDER_FALLBACK_PRICING: Record = { + anthropic: { input: 3.0, output: 15.0 }, // claude-sonnet-4-6 + openai: { input: 2.5, output: 10.0 }, // gpt-4o + openrouter: { input: 3.0, output: 15.0 }, // varies; Sonnet-class default +}; + +/** + * Cost of a completed run when the caller knows the provider and the + * model. Prices by model id first (canonical ids, aliases and + * provider-prefixed ids such as 'anthropic/claude-sonnet-4-6'), then + * falls back to the provider's row in PROVIDER_FALLBACK_PRICING. + */ +export function estimateProviderCost( + provider: string, + model: string | undefined, + inputTokens: number, + outputTokens: number, +): number { + const bare = model && model.includes('/') ? model.slice(model.indexOf('/') + 1) : model; + const pricing = + (model ? MODEL_PRICING[model] : undefined) ?? + (bare ? MODEL_PRICING[bare] : undefined) ?? + PROVIDER_FALLBACK_PRICING[provider] ?? + PROVIDER_FALLBACK_PRICING['anthropic']!; + return (inputTokens / 1_000_000) * pricing.input + (outputTokens / 1_000_000) * pricing.output; +} + /** * Set audit mode - shows telemetry payload before sending */