Skip to content

Commit bb25424

Browse files
iamwrmclaude
andcommitted
feat: add --fetch CLI argument for custom model pricing data sources
Add support for the --fetch flag that allows users to specify custom URLs or local file paths for model pricing data. This addresses issue #12 by enabling users behind corporate proxies to: - Use custom HTTPS URLs for pricing data - Load pricing data from local filesystem paths - Bypass network restrictions when accessing pricing information Changes: - Add --fetch argument to shared CLI arguments - Update PricingFetcher to accept custom data sources - Support both HTTPS URLs and local file paths - Update all commands (daily, monthly, session) to pass fetch parameter - Update debug functionality to use custom pricing sources - Maintain backward compatibility with default LiteLLM pricing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 4264d35 commit bb25424

9 files changed

Lines changed: 547 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ All commands support the following options:
221221
- `-m, --mode <mode>`: Cost calculation mode: `auto` (default), `calculate`, or `display`
222222
- `-o, --order <order>`: Sort order: `desc` (newest first, default) or `asc` (oldest first).
223223
- `-b, --breakdown`: Show per-model cost breakdown (splits usage by Opus, Sonnet, etc.)
224+
- `-f, --fetch <source>`: Custom URL or local file path for model pricing data (supports HTTPS URLs and local filesystem paths)
224225
- `-d, --debug`: Show pricing mismatch information for debugging
225226
- `--debug-samples <number>`: Number of sample discrepancies to show in debug output (default: 5)
226227
- `-h, --help`: Display help message
@@ -232,6 +233,41 @@ All commands support the following options:
232233
- **`calculate`**: Always calculates costs from token counts using model pricing, ignores any pre-calculated `costUSD` values
233234
- **`display`**: Always uses pre-calculated `costUSD` values only, shows $0.00 for entries without pre-calculated costs
234235

236+
#### Custom Pricing Data Source
237+
238+
The `--fetch` option allows you to specify a custom source for model pricing data instead of using the default LiteLLM pricing database:
239+
240+
- **HTTPS URLs**: Use `--fetch https://example.com/pricing.json` to fetch pricing data from a remote endpoint
241+
- **Local files**: Use `--fetch /path/to/pricing.json` to load pricing data from a local JSON file
242+
243+
The pricing data should be in the same format as LiteLLM's pricing database:
244+
245+
```json
246+
{
247+
"model-name": {
248+
"input_cost_per_token": 0.00001,
249+
"output_cost_per_token": 0.00003,
250+
"cache_creation_input_token_cost": 0.000015,
251+
"cache_read_input_token_cost": 0.0000015
252+
}
253+
}
254+
```
255+
256+
**Examples:**
257+
258+
```bash
259+
# Use a custom pricing file
260+
ccusage daily --fetch ./custom-pricing.json --mode calculate
261+
262+
# Fetch pricing from a custom URL
263+
ccusage session --fetch https://my-company.com/api/pricing --mode calculate
264+
265+
# Works with all commands
266+
ccusage monthly --since 20240101 --fetch ./pricing.json
267+
```
268+
269+
**Note:** The `--fetch` option only affects cost calculations when using `calculate` or `auto` modes. In `display` mode, only pre-calculated `costUSD` values are used.
270+
235271
### MCP (Model Context Protocol) Support
236272

237273
Exposes usage data through Model Context Protocol for integration with other tools:

‎src/commands/daily.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { loadDailyUsageData } from '../data-loader.ts';
1111
import { detectMismatches, printMismatchReport } from '../debug.ts';
1212
import { log, logger } from '../logger.ts';
13+
import { PricingFetcher } from '../pricing-fetcher.ts';
1314
import { sharedCommandConfig } from '../shared-args.internal.ts';
1415
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
1516

@@ -22,12 +23,17 @@ export const dailyCommand = define({
2223
logger.level = 0;
2324
}
2425

26+
// Create a single PricingFetcher instance to reuse across functions
27+
using pricingFetcher = ctx.values.mode === 'display' ? undefined : new PricingFetcher(ctx.values.fetch);
28+
2529
const dailyData = await loadDailyUsageData({
2630
since: ctx.values.since,
2731
until: ctx.values.until,
2832
claudePath: ctx.values.path,
2933
mode: ctx.values.mode,
3034
order: ctx.values.order,
35+
fetch: ctx.values.fetch,
36+
pricingFetcher,
3137
});
3238

3339
if (dailyData.length === 0) {
@@ -45,7 +51,7 @@ export const dailyCommand = define({
4551

4652
// Show debug information if requested
4753
if (ctx.values.debug && !ctx.values.json) {
48-
const mismatchStats = await detectMismatches(ctx.values.path);
54+
const mismatchStats = await detectMismatches(ctx.values.path, ctx.values.fetch, pricingFetcher);
4955
printMismatchReport(mismatchStats, ctx.values.debugSamples);
5056
}
5157

‎src/commands/monthly.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { loadMonthlyUsageData } from '../data-loader.ts';
1111
import { detectMismatches, printMismatchReport } from '../debug.ts';
1212
import { log, logger } from '../logger.ts';
13+
import { PricingFetcher } from '../pricing-fetcher.ts';
1314
import { sharedCommandConfig } from '../shared-args.internal.ts';
1415
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
1516

@@ -22,12 +23,17 @@ export const monthlyCommand = define({
2223
logger.level = 0;
2324
}
2425

26+
// Create a single PricingFetcher instance to reuse across functions
27+
using pricingFetcher = ctx.values.mode === 'display' ? undefined : new PricingFetcher(ctx.values.fetch);
28+
2529
const monthlyData = await loadMonthlyUsageData({
2630
since: ctx.values.since,
2731
until: ctx.values.until,
2832
claudePath: ctx.values.path,
2933
mode: ctx.values.mode,
3034
order: ctx.values.order,
35+
fetch: ctx.values.fetch,
36+
pricingFetcher,
3137
});
3238

3339
if (monthlyData.length === 0) {
@@ -56,7 +62,7 @@ export const monthlyCommand = define({
5662

5763
// Show debug information if requested
5864
if (ctx.values.debug && !ctx.values.json) {
59-
const mismatchStats = await detectMismatches(ctx.values.path);
65+
const mismatchStats = await detectMismatches(ctx.values.path, ctx.values.fetch, pricingFetcher);
6066
printMismatchReport(mismatchStats, ctx.values.debugSamples);
6167
}
6268

‎src/commands/session.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { loadSessionData } from '../data-loader.ts';
1111
import { detectMismatches, printMismatchReport } from '../debug.ts';
1212
import { log, logger } from '../logger.ts';
13+
import { PricingFetcher } from '../pricing-fetcher.ts';
1314
import { sharedCommandConfig } from '../shared-args.internal.ts';
1415
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
1516

@@ -22,12 +23,17 @@ export const sessionCommand = define({
2223
logger.level = 0;
2324
}
2425

26+
// Create a single PricingFetcher instance to reuse across functions
27+
using pricingFetcher = ctx.values.mode === 'display' ? undefined : new PricingFetcher(ctx.values.fetch);
28+
2529
const sessionData = await loadSessionData({
2630
since: ctx.values.since,
2731
until: ctx.values.until,
2832
claudePath: ctx.values.path,
2933
mode: ctx.values.mode,
3034
order: ctx.values.order,
35+
fetch: ctx.values.fetch,
36+
pricingFetcher,
3137
});
3238

3339
if (sessionData.length === 0) {
@@ -45,7 +51,7 @@ export const sessionCommand = define({
4551

4652
// Show debug information if requested
4753
if (ctx.values.debug && !ctx.values.json) {
48-
const mismatchStats = await detectMismatches(ctx.values.path);
54+
const mismatchStats = await detectMismatches(ctx.values.path, ctx.values.fetch, pricingFetcher);
4955
printMismatchReport(mismatchStats, ctx.values.debugSamples);
5056
}
5157

‎src/data-loader.ts‎

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as v from 'valibot';
99
import { logger } from './logger.ts';
1010
import {
1111
PricingFetcher,
12+
PricingSourceError,
1213
} from './pricing-fetcher.ts';
1314
import { groupBy } from './utils.internal.ts';
1415

@@ -256,6 +257,8 @@ export type LoadOptions = {
256257
claudePath?: string; // Custom path to Claude data directory
257258
mode?: CostMode; // Cost calculation mode
258259
order?: SortOrder; // Sort order for dates
260+
fetch?: string; // Custom URL or local file path for model pricing data
261+
pricingFetcher?: PricingFetcher; // Reuse existing PricingFetcher instance
259262
} & DateFilter;
260263

261264
export async function loadDailyUsageData(
@@ -278,8 +281,21 @@ export async function loadDailyUsageData(
278281
// Fetch pricing data for cost calculation only when needed
279282
const mode = options?.mode ?? 'auto';
280283

281-
// Use PricingFetcher with using statement for automatic cleanup
282-
using fetcher = mode === 'display' ? null : new PricingFetcher();
284+
// Use provided PricingFetcher or create new one with using statement for automatic cleanup
285+
using fetcher = mode === 'display' ? undefined : (options?.pricingFetcher ?? new PricingFetcher(options?.fetch));
286+
287+
// Fail fast for custom pricing sources if needed
288+
if (fetcher != null && options?.fetch != null) {
289+
try {
290+
await fetcher.fetchModelPricing();
291+
}
292+
catch (error) {
293+
if (error instanceof PricingSourceError) {
294+
throw error; // Re-throw to fail fast
295+
}
296+
// For other errors, continue with zero costs
297+
}
298+
}
283299

284300
// Track processed message+request combinations for deduplication
285301
const processedHashes = new Set<string>();
@@ -318,9 +334,23 @@ export async function loadDailyUsageData(
318334
const date = formatDate(data.timestamp);
319335
// If fetcher is available, calculate cost based on mode and tokens
320336
// If fetcher is null, use pre-calculated costUSD or default to 0
321-
const cost = fetcher != null
322-
? await calculateCostForEntry(data, mode, fetcher)
323-
: data.costUSD ?? 0;
337+
let cost = 0;
338+
if (fetcher != null) {
339+
try {
340+
cost = await calculateCostForEntry(data, mode, fetcher);
341+
}
342+
catch (error) {
343+
// Re-throw PricingSourceError to fail gracefully on custom source errors
344+
if (error instanceof PricingSourceError) {
345+
throw error;
346+
}
347+
// If cost calculation fails for other reasons, use 0 cost but continue processing
348+
cost = 0;
349+
}
350+
}
351+
else {
352+
cost = data.costUSD ?? 0;
353+
}
324354

325355
allEntries.push({ data, date, cost, model: data.message.model });
326356
}
@@ -452,8 +482,21 @@ export async function loadSessionData(
452482
// Fetch pricing data for cost calculation only when needed
453483
const mode = options?.mode ?? 'auto';
454484

455-
// Use PricingFetcher with using statement for automatic cleanup
456-
using fetcher = mode === 'display' ? null : new PricingFetcher();
485+
// Use provided PricingFetcher or create new one with using statement for automatic cleanup
486+
using fetcher = mode === 'display' ? undefined : (options?.pricingFetcher ?? new PricingFetcher(options?.fetch));
487+
488+
// Fail fast for custom pricing sources if needed
489+
if (fetcher != null && options?.fetch != null) {
490+
try {
491+
await fetcher.fetchModelPricing();
492+
}
493+
catch (error) {
494+
if (error instanceof PricingSourceError) {
495+
throw error; // Re-throw to fail fast
496+
}
497+
// For other errors, continue with zero costs
498+
}
499+
}
457500

458501
// Track processed message+request combinations for deduplication
459502
const processedHashes = new Set<string>();
@@ -508,9 +551,23 @@ export async function loadSessionData(
508551
}
509552

510553
const sessionKey = `${projectPath}/${sessionId}`;
511-
const cost = fetcher != null
512-
? await calculateCostForEntry(data, mode, fetcher)
513-
: data.costUSD ?? 0;
554+
let cost = 0;
555+
if (fetcher != null) {
556+
try {
557+
cost = await calculateCostForEntry(data, mode, fetcher);
558+
}
559+
catch (error) {
560+
// Re-throw PricingSourceError to fail gracefully on custom source errors
561+
if (error instanceof PricingSourceError) {
562+
throw error;
563+
}
564+
// If cost calculation fails for other reasons, use 0 cost but continue processing
565+
cost = 0;
566+
}
567+
}
568+
else {
569+
cost = data.costUSD ?? 0;
570+
}
514571

515572
allEntries.push({
516573
data,

‎src/debug.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ type MismatchStats = {
5252

5353
export async function detectMismatches(
5454
claudePath?: string,
55+
fetch?: string,
56+
pricingFetcher?: PricingFetcher,
5557
): Promise<MismatchStats> {
5658
const claudeDir = claudePath ?? path.join(homedir(), '.claude', 'projects');
5759
const files = await glob(['**/*.jsonl'], {
5860
cwd: claudeDir,
5961
absolute: true,
6062
});
6163

62-
// Use PricingFetcher with using statement for automatic cleanup
63-
using fetcher = new PricingFetcher();
64+
// Use provided PricingFetcher or create new one with using statement for automatic cleanup
65+
using fetcher = pricingFetcher ?? new PricingFetcher(fetch);
6466

6567
const stats: MismatchStats = {
6668
totalEntries: 0,

0 commit comments

Comments
 (0)