fix: warn on unknown model pricing lookups
Add console.warn when calculateMessageCost encounters an unknown model, so pricing.json gaps are visible rather than silently returning $0. Update tests to expect the warning. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
db9a676680
commit
c507a4f9d5
3 changed files with 15 additions and 3 deletions
|
|
@ -73,7 +73,12 @@ export function calculateMessageCost(
|
|||
cacheCreationTokens: number
|
||||
): number {
|
||||
const pricing = getPricing(modelName);
|
||||
if (!pricing) return 0;
|
||||
if (!pricing) {
|
||||
if (inputTokens > 0 || outputTokens > 0) {
|
||||
console.warn(`[pricing] No pricing data for model "${modelName}", cost will be $0`);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const inputCost = calculateTieredCost(
|
||||
inputTokens,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* Tests for cost calculation in jsonl.ts
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { calculateMetrics } from '@main/utils/jsonl';
|
||||
import type { ParsedMessage } from '@main/types';
|
||||
|
||||
|
|
@ -84,6 +84,7 @@ describe('Cost Calculation', () => {
|
|||
});
|
||||
|
||||
it('should return 0 cost when model pricing not found', () => {
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const messages: ParsedMessage[] = [
|
||||
{
|
||||
type: 'assistant',
|
||||
|
|
@ -103,6 +104,7 @@ describe('Cost Calculation', () => {
|
|||
|
||||
const metrics = calculateMetrics(messages);
|
||||
expect(metrics.costUsd).toBe(0);
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import {
|
||||
getPricing,
|
||||
calculateTieredCost,
|
||||
|
|
@ -55,8 +55,13 @@ describe('Shared Pricing Module', () => {
|
|||
});
|
||||
|
||||
it('should return 0 for unknown models', () => {
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const cost = calculateMessageCost('unknown-model', 1000, 500, 0, 0);
|
||||
expect(cost).toBe(0);
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
'[pricing] No pricing data for model "unknown-model", cost will be $0'
|
||||
);
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('should include cache token costs', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue