Skip to content

ccusage / pricing-fetcher / PricingFetcher

Class: PricingFetcher

Fetches and caches model pricing information from LiteLLM Implements Disposable pattern for automatic resource cleanup

Implements

  • Disposable

Constructors

Constructor

ts
new PricingFetcher(offline): PricingFetcher;

Creates a new PricingFetcher instance

Parameters

ParameterTypeDefault valueDescription
offlinebooleanfalseWhether to use pre-fetched pricing data instead of fetching from API

Returns

PricingFetcher

Methods

[dispose]()

ts
dispose: void;

Implements Disposable interface for automatic cleanup

Returns

void

Implementation of

ts
Disposable.[dispose]

clearCache()

ts
clearCache(): void;

Clears the cached pricing data

Returns

void


fetchModelPricing()

ts
fetchModelPricing(): Promise<Map<string, {
  input_cost_per_token?: number;
  output_cost_per_token?: number;
  cache_creation_input_token_cost?: number;
  cache_read_input_token_cost?: number;
}>>;

Fetches all available model pricing data

Returns

Promise<Map<string, { input_cost_per_token?: number; output_cost_per_token?: number; cache_creation_input_token_cost?: number; cache_read_input_token_cost?: number; }>>

Map of model names to pricing information


getModelPricing()

ts
getModelPricing(modelName): Promise<
  | null
  | {
  input_cost_per_token?: number;
  output_cost_per_token?: number;
  cache_creation_input_token_cost?: number;
  cache_read_input_token_cost?: number;
}>;

Gets pricing information for a specific model with fallback matching Tries exact match first, then provider prefixes, then partial matches

Parameters

ParameterTypeDescription
modelNamestringName of the model to get pricing for

Returns

Promise< | null | { input_cost_per_token?: number; output_cost_per_token?: number; cache_creation_input_token_cost?: number; cache_read_input_token_cost?: number; }>

Model pricing information or null if not found


calculateCostFromTokens()

ts
calculateCostFromTokens(tokens, modelName): Promise<number>;

Calculates the cost for given token usage and model

Parameters

ParameterTypeDescription
tokens{ input_tokens: number; output_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number; }Token usage breakdown
tokens.input_tokensnumberNumber of input tokens
tokens.output_tokensnumberNumber of output tokens
tokens.cache_creation_input_tokens?numberNumber of cache creation tokens
tokens.cache_read_input_tokens?numberNumber of cache read tokens
modelNamestringName of the model used

Returns

Promise<number>

Total cost in USD


calculateCostFromPricing()

ts
calculateCostFromPricing(tokens, pricing): number;

Calculates cost from token usage and pricing information

Parameters

ParameterTypeDescription
tokens{ input_tokens: number; output_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number; }Token usage breakdown
tokens.input_tokensnumberNumber of input tokens
tokens.output_tokensnumberNumber of output tokens
tokens.cache_creation_input_tokens?numberNumber of cache creation tokens
tokens.cache_read_input_tokens?numberNumber of cache read tokens
pricing{ input_cost_per_token?: number; output_cost_per_token?: number; cache_creation_input_token_cost?: number; cache_read_input_token_cost?: number; }Model pricing rates
pricing.input_cost_per_token?number-
pricing.output_cost_per_token?number-
pricing.cache_creation_input_token_cost?number-
pricing.cache_read_input_token_cost?number-

Returns

number

Total cost in USD

Released under the MIT License.