@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.cjs CHANGED
@@ -105,29 +105,40 @@ var SqliteStorage = class {
105
105
  migrate() {
106
106
  this.db.exec(`
107
107
  CREATE TABLE IF NOT EXISTS usage (
108
- id INTEGER PRIMARY KEY AUTOINCREMENT,
109
- model TEXT NOT NULL,
110
- input_tokens INTEGER NOT NULL,
111
- output_tokens INTEGER NOT NULL,
112
- cost_usd REAL NOT NULL,
113
- session_id TEXT,
114
- user_id TEXT,
115
- timestamp TEXT NOT NULL
108
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
109
+ model TEXT NOT NULL,
110
+ input_tokens INTEGER NOT NULL,
111
+ output_tokens INTEGER NOT NULL,
112
+ reasoning_tokens INTEGER NOT NULL DEFAULT 0,
113
+ cost_usd REAL NOT NULL,
114
+ session_id TEXT,
115
+ user_id TEXT,
116
+ feature TEXT,
117
+ timestamp TEXT NOT NULL
116
118
  )
117
119
  `);
120
+ const cols = this.db.prepare(`PRAGMA table_info(usage)`).all().map((c) => c.name);
121
+ if (!cols.includes("reasoning_tokens")) {
122
+ this.db.exec(`ALTER TABLE usage ADD COLUMN reasoning_tokens INTEGER NOT NULL DEFAULT 0`);
123
+ }
124
+ if (!cols.includes("feature")) {
125
+ this.db.exec(`ALTER TABLE usage ADD COLUMN feature TEXT`);
126
+ }
118
127
  }
119
128
  record(entry) {
120
129
  this.db.prepare(
121
130
  `INSERT INTO usage
122
- (model, input_tokens, output_tokens, cost_usd, session_id, user_id, timestamp)
123
- VALUES (?, ?, ?, ?, ?, ?, ?)`
131
+ (model, input_tokens, output_tokens, reasoning_tokens, cost_usd, session_id, user_id, feature, timestamp)
132
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
124
133
  ).run(
125
134
  entry.model,
126
135
  entry.inputTokens,
127
136
  entry.outputTokens,
137
+ entry.reasoningTokens ?? 0,
128
138
  entry.costUSD,
129
139
  entry.sessionId ?? null,
130
140
  entry.userId ?? null,
141
+ entry.feature ?? null,
131
142
  entry.timestamp
132
143
  );
133
144
  }
@@ -137,9 +148,11 @@ var SqliteStorage = class {
137
148
  model: r.model,
138
149
  inputTokens: r.input_tokens,
139
150
  outputTokens: r.output_tokens,
151
+ ...r.reasoning_tokens > 0 && { reasoningTokens: r.reasoning_tokens },
140
152
  costUSD: r.cost_usd,
141
153
  ...r.session_id != null && { sessionId: r.session_id },
142
154
  ...r.user_id != null && { userId: r.user_id },
155
+ ...r.feature != null && { feature: r.feature },
143
156
  timestamp: r.timestamp
144
157
  }));
145
158
  }
@@ -1410,11 +1423,7 @@ ${issues}`);
1410
1423
  }
1411
1424
  function track(entry) {
1412
1425
  const price = resolveModelPrice(entry.model);
1413
- const costUSD = calculateCost(
1414
- entry.inputTokens,
1415
- entry.outputTokens + (entry.reasoningTokens ?? 0),
1416
- price
1417
- );
1426
+ const costUSD = calculateCost(entry.inputTokens, entry.outputTokens, price);
1418
1427
  const full = {
1419
1428
  ...entry,
1420
1429
  costUSD,
@@ -1561,7 +1570,7 @@ function trackWithMeta(tracker, model, inputTokens, outputTokens, reasoningToken
1561
1570
  tracker.track({
1562
1571
  model,
1563
1572
  inputTokens,
1564
- outputTokens,
1573
+ outputTokens: outputTokens + reasoningTokens,
1565
1574
  ...reasoningTokens > 0 && { reasoningTokens },
1566
1575
  ...sessionId !== void 0 && { sessionId },
1567
1576
  ...userId !== void 0 && { userId },
@@ -1674,14 +1683,10 @@ function trackWithMeta2(tracker, model, inputTokens, outputTokens, reasoningToke
1674
1683
  model,
1675
1684
  inputTokens,
1676
1685
  outputTokens,
1677
- // For Anthropic, reasoningTokens is informational (thinking already in outputTokens).
1678
- // Pass 0 so tracker does not add it to cost (tracker only adds when > 0 AND separate).
1679
- // We store it as a field but the tracker cost formula adds reasoningTokens to outputTokens,
1680
- // so we must NOT pass it here to avoid double-counting.
1686
+ ...reasoningTokens > 0 && { reasoningTokens },
1681
1687
  ...sessionId !== void 0 && { sessionId },
1682
1688
  ...userId !== void 0 && { userId },
1683
- ...feature !== void 0 && { feature },
1684
- ...reasoningTokens > 0 && { reasoningTokens }
1689
+ ...feature !== void 0 && { feature }
1685
1690
  });
1686
1691
  }
1687
1692
  async function* wrapStream2(stream, model, sessionId, userId, feature, tracker) {