Skip to content

Commit 1c7969d

Browse files
ryoppippicobra91
andcommitted
fix(internal): broaden pricing lookup to cover provider variants
Co-authored-by: cobra91 <[email protected]>
1 parent bdd760b commit 1c7969d

2 files changed

Lines changed: 138 additions & 4 deletions

File tree

‎docs/guide/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ccusage analyzes the local JSONL files that Claude Code automatically generates
1919

2020
- **Detailed Usage Reports** - Daily, monthly, and session-based breakdowns
2121
- **Cost Analysis** - Estimated costs based on token usage and model pricing
22+
- **Multi-provider model coverage** - Supports Anthropic defaults plus additional LiteLLM providers such as DeepSeek, Dashscope, and GLM 4.5 variants
2223
- **Live Monitoring** - Real-time tracking of active sessions
2324
- **Multiple Formats** - Beautiful tables or JSON for further analysis
2425

‎packages/internal/src/pricing.ts‎

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ const DEFAULT_PROVIDER_PREFIXES = [
7171
'openai/',
7272
'azure/',
7373
'openrouter/openai/',
74+
'zai/',
75+
'deepseek/',
76+
'dashscope/',
77+
'deepinfra/',
78+
'deepinfra/zai-org/',
7479
];
7580

7681
function createLogger(logger?: PricingLogger): PricingLogger {
@@ -86,6 +91,74 @@ function createLogger(logger?: PricingLogger): PricingLogger {
8691
};
8792
}
8893

94+
const normalizeModelName = (modelName: string): string => modelName.toLowerCase();
95+
96+
function getBaseModelName(modelName: string): string {
97+
const normalized = normalizeModelName(modelName);
98+
const segments = normalized.split('/');
99+
return segments[segments.length - 1] ?? normalized;
100+
}
101+
102+
/**
103+
* Heuristic scoring so we can prioritize the “right” pricing row when multiple
104+
* candidate keys partially match (e.g. glm-4.5 vs glm-4.5-air vs zai/glm-4.5).
105+
* LiteLLM exposes dozens of provider/model variations, and a plain substring
106+
* check tends to grab the wrong variant (for example the slower “air” edition).
107+
* This mirrors the better-ccusage matching order while keeping the logic lightweight.
108+
*/
109+
function calculateModelMatchScore(targetModel: string, candidateModel: string): number {
110+
const normalizedTarget = normalizeModelName(targetModel);
111+
const normalizedCandidate = normalizeModelName(candidateModel);
112+
113+
if (normalizedCandidate === normalizedTarget) {
114+
return 200;
115+
}
116+
117+
if (normalizedCandidate.endsWith(`/${normalizedTarget}`)) {
118+
return 190;
119+
}
120+
121+
const targetBase = getBaseModelName(targetModel);
122+
const candidateBase = getBaseModelName(candidateModel);
123+
124+
if (candidateBase === targetBase) {
125+
let score = 150;
126+
127+
if (!normalizedCandidate.includes('air')) {
128+
score += 10;
129+
}
130+
131+
if (normalizedCandidate.startsWith('zai/')) {
132+
score += 5;
133+
}
134+
135+
return score;
136+
}
137+
138+
if (normalizedCandidate.includes(targetBase)) {
139+
let score = 80;
140+
141+
if (!normalizedCandidate.includes('air')) {
142+
score += 5;
143+
}
144+
145+
return score;
146+
}
147+
148+
if (targetBase.includes(candidateBase)) {
149+
return 40;
150+
}
151+
152+
if (
153+
normalizedCandidate.includes(normalizedTarget)
154+
|| normalizedTarget.includes(normalizedCandidate)
155+
) {
156+
return 20;
157+
}
158+
159+
return 0;
160+
}
161+
89162
export class LiteLLMPricingFetcher implements Disposable {
90163
private cachedPricing: Map<string, LiteLLMModelPricing> | null = null;
91164
private readonly logger: PricingLogger;
@@ -214,15 +287,29 @@ export class LiteLLMPricingFetcher implements Disposable {
214287
}
215288
}
216289

217-
const lower = modelName.toLowerCase();
290+
const normalizedTarget = normalizeModelName(modelName);
291+
292+
let bestMatch: LiteLLMModelPricing | null = null;
293+
let bestMatchScore = 0;
294+
218295
for (const [key, value] of pricing) {
219-
const comparison = key.toLowerCase();
220-
if (comparison.includes(lower) || lower.includes(comparison)) {
296+
const normalizedCandidate = normalizeModelName(key);
297+
298+
if (
299+
normalizedCandidate === normalizedTarget
300+
|| normalizedCandidate.endsWith(`/${normalizedTarget}`)
301+
) {
221302
return value;
222303
}
304+
305+
const score = calculateModelMatchScore(modelName, key);
306+
if (score > bestMatchScore) {
307+
bestMatch = value;
308+
bestMatchScore = score;
309+
}
223310
}
224311

225-
return null;
312+
return bestMatchScore > 0 ? bestMatch : null;
226313
}),
227314
);
228315
}
@@ -508,5 +595,51 @@ if (import.meta.vitest != null) {
508595
}, 'theoretical-model'));
509596
expect(costBelow).toBe(0);
510597
});
598+
599+
it('matches GLM models with provider prefixes and selects primary variants over air versions', async () => {
600+
using fetcher = new LiteLLMPricingFetcher({
601+
offline: true,
602+
offlineLoader: async () => ({
603+
'deepinfra/zai-org/GLM-4.5': {
604+
input_cost_per_token: 1e-6,
605+
output_cost_per_token: 2e-6,
606+
},
607+
'zai/glm-4.5-air': {
608+
input_cost_per_token: 5e-7,
609+
output_cost_per_token: 1e-6,
610+
},
611+
}),
612+
});
613+
614+
const basePricing = await Result.unwrap(fetcher.getModelPricing('glm-4.5'));
615+
expect(basePricing).not.toBeNull();
616+
expect(basePricing?.input_cost_per_token).toBeCloseTo(1e-6);
617+
618+
const providerPricing = await Result.unwrap(fetcher.getModelPricing('zai/glm-4.5'));
619+
expect(providerPricing).not.toBeNull();
620+
expect(providerPricing?.output_cost_per_token).toBeCloseTo(2e-6);
621+
622+
const airPricing = await Result.unwrap(fetcher.getModelPricing('glm-4.5-air'));
623+
expect(airPricing).not.toBeNull();
624+
expect(airPricing?.output_cost_per_token).toBeCloseTo(1e-6);
625+
});
626+
627+
describe('model match scoring', () => {
628+
it('prefers exact matches over provider-suffixed variants', () => {
629+
expect(calculateModelMatchScore('glm-4.5', 'glm-4.5')).toBeGreaterThan(
630+
calculateModelMatchScore('glm-4.5', 'provider/glm-4.5'),
631+
);
632+
});
633+
634+
it('prefers main variants over air editions', () => {
635+
expect(calculateModelMatchScore('glm-4.5', 'provider/glm-4.5')).toBeGreaterThan(
636+
calculateModelMatchScore('glm-4.5', 'provider/glm-4.5-air'),
637+
);
638+
});
639+
640+
it('assigns zero to unrelated models', () => {
641+
expect(calculateModelMatchScore('glm-4.5', 'gpt-4.1-mini')).toBe(0);
642+
});
643+
});
511644
});
512645
}

0 commit comments

Comments
 (0)