@diogonzafe/tokenwatch 0.2.0 → 0.2.1

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.
package/dist/index.js CHANGED
@@ -74,29 +74,40 @@ var SqliteStorage = class {
74
74
  migrate() {
75
75
  this.db.exec(`
76
76
  CREATE TABLE IF NOT EXISTS usage (
77
- id INTEGER PRIMARY KEY AUTOINCREMENT,
78
- model TEXT NOT NULL,
79
- input_tokens INTEGER NOT NULL,
80
- output_tokens INTEGER NOT NULL,
81
- cost_usd REAL NOT NULL,
82
- session_id TEXT,
83
- user_id TEXT,
84
- timestamp TEXT NOT NULL
77
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
78
+ model TEXT NOT NULL,
79
+ input_tokens INTEGER NOT NULL,
80
+ output_tokens INTEGER NOT NULL,
81
+ reasoning_tokens INTEGER NOT NULL DEFAULT 0,
82
+ cost_usd REAL NOT NULL,
83
+ session_id TEXT,
84
+ user_id TEXT,
85
+ feature TEXT,
86
+ timestamp TEXT NOT NULL
85
87
  )
86
88
  `);
89
+ const cols = this.db.prepare(`PRAGMA table_info(usage)`).all().map((c) => c.name);
90
+ if (!cols.includes("reasoning_tokens")) {
91
+ this.db.exec(`ALTER TABLE usage ADD COLUMN reasoning_tokens INTEGER NOT NULL DEFAULT 0`);
92
+ }
93
+ if (!cols.includes("feature")) {
94
+ this.db.exec(`ALTER TABLE usage ADD COLUMN feature TEXT`);
95
+ }
87
96
  }
88
97
  record(entry) {
89
98
  this.db.prepare(
90
99
  `INSERT INTO usage
91
- (model, input_tokens, output_tokens, cost_usd, session_id, user_id, timestamp)
92
- VALUES (?, ?, ?, ?, ?, ?, ?)`
100
+ (model, input_tokens, output_tokens, reasoning_tokens, cost_usd, session_id, user_id, feature, timestamp)
101
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
93
102
  ).run(
94
103
  entry.model,
95
104
  entry.inputTokens,
96
105
  entry.outputTokens,
106
+ entry.reasoningTokens ?? 0,
97
107
  entry.costUSD,
98
108
  entry.sessionId ?? null,
99
109
  entry.userId ?? null,
110
+ entry.feature ?? null,
100
111
  entry.timestamp
101
112
  );
102
113
  }
@@ -106,9 +117,11 @@ var SqliteStorage = class {
106
117
  model: r.model,
107
118
  inputTokens: r.input_tokens,
108
119
  outputTokens: r.output_tokens,
120
+ ...r.reasoning_tokens > 0 && { reasoningTokens: r.reasoning_tokens },
109
121
  costUSD: r.cost_usd,
110
122
  ...r.session_id != null && { sessionId: r.session_id },
111
123
  ...r.user_id != null && { userId: r.user_id },
124
+ ...r.feature != null && { feature: r.feature },
112
125
  timestamp: r.timestamp
113
126
  }));
114
127
  }
@@ -1379,11 +1392,7 @@ ${issues}`);
1379
1392
  }
1380
1393
  function track(entry) {
1381
1394
  const price = resolveModelPrice(entry.model);
1382
- const costUSD = calculateCost(
1383
- entry.inputTokens,
1384
- entry.outputTokens + (entry.reasoningTokens ?? 0),
1385
- price
1386
- );
1395
+ const costUSD = calculateCost(entry.inputTokens, entry.outputTokens, price);
1387
1396
  const full = {
1388
1397
  ...entry,
1389
1398
  costUSD,
@@ -1530,7 +1539,7 @@ function trackWithMeta(tracker, model, inputTokens, outputTokens, reasoningToken
1530
1539
  tracker.track({
1531
1540
  model,
1532
1541
  inputTokens,
1533
- outputTokens,
1542
+ outputTokens: outputTokens + reasoningTokens,
1534
1543
  ...reasoningTokens > 0 && { reasoningTokens },
1535
1544
  ...sessionId !== void 0 && { sessionId },
1536
1545
  ...userId !== void 0 && { userId },
@@ -1643,14 +1652,10 @@ function trackWithMeta2(tracker, model, inputTokens, outputTokens, reasoningToke
1643
1652
  model,
1644
1653
  inputTokens,
1645
1654
  outputTokens,
1646
- // For Anthropic, reasoningTokens is informational (thinking already in outputTokens).
1647
- // Pass 0 so tracker does not add it to cost (tracker only adds when > 0 AND separate).
1648
- // We store it as a field but the tracker cost formula adds reasoningTokens to outputTokens,
1649
- // so we must NOT pass it here to avoid double-counting.
1655
+ ...reasoningTokens > 0 && { reasoningTokens },
1650
1656
  ...sessionId !== void 0 && { sessionId },
1651
1657
  ...userId !== void 0 && { userId },
1652
- ...feature !== void 0 && { feature },
1653
- ...reasoningTokens > 0 && { reasoningTokens }
1658
+ ...feature !== void 0 && { feature }
1654
1659
  });
1655
1660
  }
1656
1661
  async function* wrapStream2(stream, model, sessionId, userId, feature, tracker) {