Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/llm-prices.yml
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions __tests__/server-estimate-cost.test.ts
Original file line number Diff line number Diff line change
@@ -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,/);
});
});
21 changes: 7 additions & 14 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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<string, { input: number; output: number }> = {
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);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ModelPrice> = {
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
*/
Expand Down