ccusage 11.0.2 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,342 +0,0 @@
1
- import { number, object, optional, pipe, regex, safeParse, string } from "./dist-DCvt9hEv.js";
2
- import { logger } from "./logger-DPEwxrOW.js";
3
- import { createRequire } from "node:module";
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
13
- key = keys[i];
14
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
- get: ((k) => from[k]).bind(null, key),
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
- });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
22
- value: mod,
23
- enumerable: true
24
- }) : target, mod));
25
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
26
- /**
27
- * URL for LiteLLM's model pricing and context window data
28
- */
29
- const LITELLM_PRICING_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
30
- /**
31
- * Valibot schema for validating date strings in YYYYMMDD format
32
- */
33
- const dateSchema = pipe(string(), regex(/^\d{8}$/, "Date must be in YYYYMMDD format"));
34
- /**
35
- * Available cost calculation modes
36
- * - auto: Use pre-calculated costs when available, otherwise calculate from tokens
37
- * - calculate: Always calculate costs from token counts using model pricing
38
- * - display: Always use pre-calculated costs, show 0 for missing costs
39
- */
40
- const CostModes = [
41
- "auto",
42
- "calculate",
43
- "display"
44
- ];
45
- /**
46
- * Available sort orders for data presentation
47
- */
48
- const SortOrders = ["desc", "asc"];
49
- /**
50
- * Valibot schema for model pricing information from LiteLLM
51
- */
52
- const modelPricingSchema = object({
53
- input_cost_per_token: optional(number()),
54
- output_cost_per_token: optional(number()),
55
- cache_creation_input_token_cost: optional(number()),
56
- cache_read_input_token_cost: optional(number())
57
- });
58
- var require_usingCtx = __commonJSMin((exports, module) => {
59
- function _usingCtx() {
60
- var r = "function" == typeof SuppressedError ? SuppressedError : function(r$1, e$1) {
61
- var n$1 = Error();
62
- return n$1.name = "SuppressedError", n$1.error = r$1, n$1.suppressed = e$1, n$1;
63
- }, e = {}, n = [];
64
- function using(r$1, e$1) {
65
- if (null != e$1) {
66
- if (Object(e$1) !== e$1) throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
67
- if (r$1) var o = e$1[Symbol.asyncDispose || Symbol["for"]("Symbol.asyncDispose")];
68
- if (void 0 === o && (o = e$1[Symbol.dispose || Symbol["for"]("Symbol.dispose")], r$1)) var t = o;
69
- if ("function" != typeof o) throw new TypeError("Object is not disposable.");
70
- t && (o = function o$1() {
71
- try {
72
- t.call(e$1);
73
- } catch (r$2) {
74
- return Promise.reject(r$2);
75
- }
76
- }), n.push({
77
- v: e$1,
78
- d: o,
79
- a: r$1
80
- });
81
- } else r$1 && n.push({
82
- d: e$1,
83
- a: r$1
84
- });
85
- return e$1;
86
- }
87
- return {
88
- e,
89
- u: using.bind(null, !1),
90
- a: using.bind(null, !0),
91
- d: function d() {
92
- var o, t = this.e, s = 0;
93
- function next() {
94
- for (; o = n.pop();) try {
95
- if (!o.a && 1 === s) return s = 0, n.push(o), Promise.resolve().then(next);
96
- if (o.d) {
97
- var r$1 = o.d.call(o.v);
98
- if (o.a) return s |= 2, Promise.resolve(r$1).then(next, err);
99
- } else s |= 1;
100
- } catch (r$2) {
101
- return err(r$2);
102
- }
103
- if (1 === s) return t !== e ? Promise.reject(t) : Promise.resolve();
104
- if (t !== e) throw t;
105
- }
106
- function err(n$1) {
107
- return t = t !== e ? new r(n$1, t) : n$1, next();
108
- }
109
- return next();
110
- }
111
- };
112
- }
113
- module.exports = _usingCtx, module.exports.__esModule = true, module.exports["default"] = module.exports;
114
- });
115
- var import_usingCtx = __toESM(require_usingCtx(), 1);
116
- /**
117
- * Fetches and caches model pricing information from LiteLLM
118
- * Implements Disposable pattern for automatic resource cleanup
119
- */
120
- var PricingFetcher = class {
121
- cachedPricing = null;
122
- offline;
123
- /**
124
- * Creates a new PricingFetcher instance
125
- * @param offline - Whether to use pre-fetched pricing data instead of fetching from API
126
- */
127
- constructor(offline = false) {
128
- this.offline = offline;
129
- }
130
- /**
131
- * Implements Disposable interface for automatic cleanup
132
- */
133
- [Symbol.dispose]() {
134
- this.clearCache();
135
- }
136
- /**
137
- * Clears the cached pricing data
138
- */
139
- clearCache() {
140
- this.cachedPricing = null;
141
- }
142
- /**
143
- * Ensures pricing data is loaded, either from cache or by fetching
144
- * @returns Map of model names to pricing information
145
- */
146
- async ensurePricingLoaded() {
147
- if (this.cachedPricing != null) return this.cachedPricing;
148
- if (this.offline) {
149
- const pricing = new Map(Object.entries({
150
- "claude-instant-1": {
151
- "input_cost_per_token": 163e-8,
152
- "output_cost_per_token": 551e-8
153
- },
154
- "claude-instant-1.2": {
155
- "input_cost_per_token": 163e-9,
156
- "output_cost_per_token": 551e-9
157
- },
158
- "claude-2": {
159
- "input_cost_per_token": 8e-6,
160
- "output_cost_per_token": 24e-6
161
- },
162
- "claude-2.1": {
163
- "input_cost_per_token": 8e-6,
164
- "output_cost_per_token": 24e-6
165
- },
166
- "claude-3-haiku-20240307": {
167
- "input_cost_per_token": 25e-8,
168
- "output_cost_per_token": 125e-8,
169
- "cache_creation_input_token_cost": 3e-7,
170
- "cache_read_input_token_cost": 3e-8
171
- },
172
- "claude-3-5-haiku-20241022": {
173
- "input_cost_per_token": 8e-7,
174
- "output_cost_per_token": 4e-6,
175
- "cache_creation_input_token_cost": 1e-6,
176
- "cache_read_input_token_cost": 8e-8
177
- },
178
- "claude-3-5-haiku-latest": {
179
- "input_cost_per_token": 1e-6,
180
- "output_cost_per_token": 5e-6,
181
- "cache_creation_input_token_cost": 125e-8,
182
- "cache_read_input_token_cost": 1e-7
183
- },
184
- "claude-3-opus-latest": {
185
- "input_cost_per_token": 15e-6,
186
- "output_cost_per_token": 75e-6,
187
- "cache_creation_input_token_cost": 1875e-8,
188
- "cache_read_input_token_cost": 15e-7
189
- },
190
- "claude-3-opus-20240229": {
191
- "input_cost_per_token": 15e-6,
192
- "output_cost_per_token": 75e-6,
193
- "cache_creation_input_token_cost": 1875e-8,
194
- "cache_read_input_token_cost": 15e-7
195
- },
196
- "claude-3-sonnet-20240229": {
197
- "input_cost_per_token": 3e-6,
198
- "output_cost_per_token": 15e-6
199
- },
200
- "claude-3-5-sonnet-latest": {
201
- "input_cost_per_token": 3e-6,
202
- "output_cost_per_token": 15e-6,
203
- "cache_creation_input_token_cost": 375e-8,
204
- "cache_read_input_token_cost": 3e-7
205
- },
206
- "claude-3-5-sonnet-20240620": {
207
- "input_cost_per_token": 3e-6,
208
- "output_cost_per_token": 15e-6,
209
- "cache_creation_input_token_cost": 375e-8,
210
- "cache_read_input_token_cost": 3e-7
211
- },
212
- "claude-opus-4-20250514": {
213
- "input_cost_per_token": 15e-6,
214
- "output_cost_per_token": 75e-6,
215
- "cache_creation_input_token_cost": 1875e-8,
216
- "cache_read_input_token_cost": 15e-7
217
- },
218
- "claude-sonnet-4-20250514": {
219
- "input_cost_per_token": 3e-6,
220
- "output_cost_per_token": 15e-6,
221
- "cache_creation_input_token_cost": 375e-8,
222
- "cache_read_input_token_cost": 3e-7
223
- },
224
- "claude-4-opus-20250514": {
225
- "input_cost_per_token": 15e-6,
226
- "output_cost_per_token": 75e-6,
227
- "cache_creation_input_token_cost": 1875e-8,
228
- "cache_read_input_token_cost": 15e-7
229
- },
230
- "claude-4-sonnet-20250514": {
231
- "input_cost_per_token": 3e-6,
232
- "output_cost_per_token": 15e-6,
233
- "cache_creation_input_token_cost": 375e-8,
234
- "cache_read_input_token_cost": 3e-7
235
- },
236
- "claude-3-7-sonnet-latest": {
237
- "input_cost_per_token": 3e-6,
238
- "output_cost_per_token": 15e-6,
239
- "cache_creation_input_token_cost": 375e-8,
240
- "cache_read_input_token_cost": 3e-7
241
- },
242
- "claude-3-7-sonnet-20250219": {
243
- "input_cost_per_token": 3e-6,
244
- "output_cost_per_token": 15e-6,
245
- "cache_creation_input_token_cost": 375e-8,
246
- "cache_read_input_token_cost": 3e-7
247
- },
248
- "claude-3-5-sonnet-20241022": {
249
- "input_cost_per_token": 3e-6,
250
- "output_cost_per_token": 15e-6,
251
- "cache_creation_input_token_cost": 375e-8,
252
- "cache_read_input_token_cost": 3e-7
253
- }
254
- }));
255
- this.cachedPricing = pricing;
256
- return pricing;
257
- }
258
- try {
259
- logger.warn("Fetching latest model pricing from LiteLLM...");
260
- const response = await fetch(LITELLM_PRICING_URL);
261
- if (!response.ok) throw new Error(`Failed to fetch pricing data: ${response.statusText}`);
262
- const data = await response.json();
263
- const pricing = /* @__PURE__ */ new Map();
264
- for (const [modelName, modelData] of Object.entries(data)) if (typeof modelData === "object" && modelData !== null) {
265
- const parsed = safeParse(modelPricingSchema, modelData);
266
- if (parsed.success) pricing.set(modelName, parsed.output);
267
- }
268
- this.cachedPricing = pricing;
269
- logger.info(`Loaded pricing for ${pricing.size} models`);
270
- return pricing;
271
- } catch (error) {
272
- logger.error("Failed to fetch model pricing:", error);
273
- throw new Error("Could not fetch model pricing data");
274
- }
275
- }
276
- /**
277
- * Fetches all available model pricing data
278
- * @returns Map of model names to pricing information
279
- */
280
- async fetchModelPricing() {
281
- return this.ensurePricingLoaded();
282
- }
283
- /**
284
- * Gets pricing information for a specific model with fallback matching
285
- * Tries exact match first, then provider prefixes, then partial matches
286
- * @param modelName - Name of the model to get pricing for
287
- * @returns Model pricing information or null if not found
288
- */
289
- async getModelPricing(modelName) {
290
- const pricing = await this.ensurePricingLoaded();
291
- const directMatch = pricing.get(modelName);
292
- if (directMatch != null) return directMatch;
293
- const variations = [
294
- modelName,
295
- `anthropic/${modelName}`,
296
- `claude-3-5-${modelName}`,
297
- `claude-3-${modelName}`,
298
- `claude-${modelName}`
299
- ];
300
- for (const variant of variations) {
301
- const match = pricing.get(variant);
302
- if (match != null) return match;
303
- }
304
- const lowerModel = modelName.toLowerCase();
305
- for (const [key, value] of pricing) if (key.toLowerCase().includes(lowerModel) || lowerModel.includes(key.toLowerCase())) return value;
306
- return null;
307
- }
308
- /**
309
- * Calculates the cost for given token usage and model
310
- * @param tokens - Token usage breakdown
311
- * @param tokens.input_tokens - Number of input tokens
312
- * @param tokens.output_tokens - Number of output tokens
313
- * @param tokens.cache_creation_input_tokens - Number of cache creation tokens
314
- * @param tokens.cache_read_input_tokens - Number of cache read tokens
315
- * @param modelName - Name of the model used
316
- * @returns Total cost in USD
317
- */
318
- async calculateCostFromTokens(tokens, modelName) {
319
- const pricing = await this.getModelPricing(modelName);
320
- if (pricing == null) return 0;
321
- return this.calculateCostFromPricing(tokens, pricing);
322
- }
323
- /**
324
- * Calculates cost from token usage and pricing information
325
- * @param tokens - Token usage breakdown
326
- * @param tokens.input_tokens - Number of input tokens
327
- * @param tokens.output_tokens - Number of output tokens
328
- * @param tokens.cache_creation_input_tokens - Number of cache creation tokens
329
- * @param tokens.cache_read_input_tokens - Number of cache read tokens
330
- * @param pricing - Model pricing rates
331
- * @returns Total cost in USD
332
- */
333
- calculateCostFromPricing(tokens, pricing) {
334
- let cost = 0;
335
- if (pricing.input_cost_per_token != null) cost += tokens.input_tokens * pricing.input_cost_per_token;
336
- if (pricing.output_cost_per_token != null) cost += tokens.output_tokens * pricing.output_cost_per_token;
337
- if (tokens.cache_creation_input_tokens != null && pricing.cache_creation_input_token_cost != null) cost += tokens.cache_creation_input_tokens * pricing.cache_creation_input_token_cost;
338
- if (tokens.cache_read_input_tokens != null && pricing.cache_read_input_token_cost != null) cost += tokens.cache_read_input_tokens * pricing.cache_read_input_token_cost;
339
- return cost;
340
- }
341
- };
342
- export { CostModes, PricingFetcher, SortOrders, __commonJSMin, __require, __toESM, dateSchema, require_usingCtx };