context-doctor 0.44.0 → 0.46.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.
- package/README.md +5 -2
- package/dist/budget.d.ts +14 -1
- package/dist/budget.js +50 -28
- package/dist/cache.js +5 -4
- package/dist/cachemisses.js +6 -4
- package/dist/cacheplan.js +2 -2
- package/dist/cachettl.js +8 -5
- package/dist/ci.js +18 -2
- package/dist/cli.js +27 -6
- package/dist/codexcompact.js +14 -7
- package/dist/coldresume.d.ts +6 -0
- package/dist/coldresume.js +20 -11
- package/dist/compactwindow.js +14 -8
- package/dist/doctor.js +26 -2
- package/dist/extract.d.ts +2 -2
- package/dist/extract.js +14 -3
- package/dist/fanout.js +1 -1
- package/dist/fix.js +14 -10
- package/dist/hook.js +6 -2
- package/dist/httpguard.d.ts +9 -1
- package/dist/httpguard.js +5 -4
- package/dist/lens.js +7 -7
- package/dist/limits.d.ts +14 -5
- package/dist/limits.js +26 -17
- package/dist/mcp.js +62 -62
- package/dist/mcpschema.js +5 -3
- package/dist/mod/api.d.ts +129 -0
- package/dist/mod/api.js +12 -0
- package/dist/mod/logic.d.ts +146 -0
- package/dist/mod/logic.js +222 -0
- package/dist/mod/register.d.ts +58 -0
- package/dist/mod/register.js +237 -0
- package/dist/modelids.d.ts +23 -0
- package/dist/modelids.js +49 -0
- package/dist/modmeasure.d.ts +121 -0
- package/dist/modmeasure.js +298 -0
- package/dist/orgrates.d.ts +75 -0
- package/dist/orgrates.js +165 -0
- package/dist/overhead.js +11 -8
- package/dist/pack.js +95 -22
- package/dist/pricing.d.ts +123 -6
- package/dist/pricing.js +236 -62
- package/dist/proxy.d.ts +12 -1
- package/dist/proxy.js +13 -1
- package/dist/prune.js +55 -3
- package/dist/savings.js +19 -10
- package/dist/scan.d.ts +7 -1
- package/dist/scan.js +19 -1
- package/dist/sessionprice.d.ts +25 -2
- package/dist/sessionprice.js +60 -20
- package/dist/spend.d.ts +62 -10
- package/dist/spend.js +255 -29
- package/dist/statusline.d.ts +10 -2
- package/dist/statusline.js +18 -9
- package/dist/subagentmodel.d.ts +2 -2
- package/dist/subagentmodel.js +31 -24
- package/dist/subagentpay.d.ts +7 -1
- package/dist/subagentpay.js +51 -25
- package/dist/subagents.js +15 -10
- package/dist/tasks.js +9 -6
- package/dist/tooloutput.js +14 -5
- package/package.json +1 -1
package/dist/subagentmodel.js
CHANGED
|
@@ -34,7 +34,7 @@ import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "n
|
|
|
34
34
|
import { homedir } from "node:os";
|
|
35
35
|
import { dirname, join, resolve } from "node:path";
|
|
36
36
|
import { pick, rowsOf, slimUsage } from "./scan.js";
|
|
37
|
-
import { pricingFor, usageCost } from "./pricing.js";
|
|
37
|
+
import { pricingFor, rateCard, ratesAt, requestCost, usageCost, WEB_SEARCH_USD } from "./pricing.js";
|
|
38
38
|
import { realModel } from "./tokens.js";
|
|
39
39
|
import { claudeDir } from "./claudedir.js";
|
|
40
40
|
import { subagentTranscripts } from "./cachettl.js";
|
|
@@ -72,7 +72,7 @@ const READ_ONLY = new Set(["Explore", "Plan"]);
|
|
|
72
72
|
* last row); the working folder once, to find the project's agent files.
|
|
73
73
|
*/
|
|
74
74
|
export const SUBAGENT_ROWS = {
|
|
75
|
-
id: "subagent-requests.
|
|
75
|
+
id: "subagent-requests.2",
|
|
76
76
|
init: () => ({ out: {}, cwd: false }),
|
|
77
77
|
line(raw, row, out, st) {
|
|
78
78
|
const usage = raw.includes('"usage"');
|
|
@@ -238,15 +238,25 @@ export function agentTypeOf(transcript) {
|
|
|
238
238
|
}
|
|
239
239
|
const tokens = (v) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
|
|
240
240
|
const promptTokens = (u) => tokens(u?.input_tokens) + tokens(u?.cache_read_input_tokens) + tokens(u?.cache_creation_input_tokens);
|
|
241
|
-
/**
|
|
242
|
-
|
|
243
|
-
|
|
241
|
+
/**
|
|
242
|
+
* The rates of a mix of models (shares adding to 1), each as it would bill
|
|
243
|
+
* `u` (its tier, US-only: rateCard) when given; undefined when none has a price.
|
|
244
|
+
*/
|
|
245
|
+
function blend(mix, u, at) {
|
|
246
|
+
const rates = (m) => { const c = rateCard(m, u, at); return c && ratesAt(c, promptTokens(u ?? {})); };
|
|
247
|
+
const parts = Object.entries(mix).map(([m, w]) => [rates(m), w]).filter((x) => !!x[0] && x[1] > 0);
|
|
244
248
|
const total = parts.reduce((s, [, w]) => s + w, 0);
|
|
245
249
|
if (!(total > 0))
|
|
246
250
|
return undefined;
|
|
247
251
|
const rate = (k) => parts.reduce((s, [p, w]) => s + (p[k] * w) / total, 0);
|
|
248
252
|
return { inputPerM: rate("inputPerM"), outputPerM: rate("outputPerM"), cacheReadPerM: rate("cacheReadPerM") };
|
|
249
253
|
}
|
|
254
|
+
/** A request at the baseline mix (prices are linear in each rate: the blend of its costs), web searches as any Claude model bills them. */
|
|
255
|
+
function atBaseline(base, u, at, mix) {
|
|
256
|
+
const rates = (mix && blend(mix, u, at)) || base;
|
|
257
|
+
const searches = Number(u?.server_tool_use?.web_search_requests) > 0 ? Number(u.server_tool_use.web_search_requests) : 0;
|
|
258
|
+
return { totalUsd: usageCost(u, rates).totalUsd + searches * WEB_SEARCH_USD, rates };
|
|
259
|
+
}
|
|
250
260
|
const cheaper = (a, b) => a.inputPerM < b.inputPerM || (a.inputPerM === b.inputPerM && a.outputPerM < b.outputPerM);
|
|
251
261
|
/**
|
|
252
262
|
* Replay every subagent request in [since, until): as billed, and with the
|
|
@@ -258,7 +268,6 @@ const cheaper = (a, b) => a.inputPerM < b.inputPerM || (a.inputPerM === b.inputP
|
|
|
258
268
|
export function replaySubagentModels(opts) {
|
|
259
269
|
const candidate = opts.candidate ?? SUBAGENT_CANDIDATE;
|
|
260
270
|
const cand = pricingFor(candidate);
|
|
261
|
-
const haiku = pricingFor(HAIKU);
|
|
262
271
|
const base = opts.baseline ? blend(opts.baseline) : undefined;
|
|
263
272
|
const until = opts.until ?? Infinity;
|
|
264
273
|
const home = opts.home ?? homedir();
|
|
@@ -304,9 +313,9 @@ export function replaySubagentModels(opts) {
|
|
|
304
313
|
continue;
|
|
305
314
|
t.billedUsd += (r.more * q.billed.outputPerM) / 1e6;
|
|
306
315
|
t.atCandidateUsd += (r.more * q.other.outputPerM) / 1e6;
|
|
307
|
-
t.atHaikuUsd += (r.more * haiku.outputPerM) / 1e6;
|
|
316
|
+
t.atHaikuUsd += (r.more * q.haiku.outputPerM) / 1e6;
|
|
308
317
|
if (how.moves)
|
|
309
|
-
add(beforeBy, q.model, (r.more * cand.outputPerM) / 1e6);
|
|
318
|
+
add(beforeBy, q.model, (r.more * q.cand.outputPerM) / 1e6);
|
|
310
319
|
continue;
|
|
311
320
|
}
|
|
312
321
|
if (r?.t !== "r")
|
|
@@ -324,28 +333,31 @@ export function replaySubagentModels(opts) {
|
|
|
324
333
|
seen.add(r.id);
|
|
325
334
|
}
|
|
326
335
|
const price = pricingFor(model);
|
|
327
|
-
|
|
336
|
+
const u = r.usage;
|
|
337
|
+
// Each arm as that model would bill these tokens (requestCost): its tier (Haiku 5.5 over 100k), US-only, web searches.
|
|
338
|
+
const asBilled = requestCost(model, u, at);
|
|
339
|
+
if (!price || !asBilled) {
|
|
328
340
|
unpriced++;
|
|
329
341
|
continue;
|
|
330
342
|
}
|
|
331
|
-
const
|
|
332
|
-
const
|
|
333
|
-
const
|
|
343
|
+
const atCand = requestCost(candidate, u, at);
|
|
344
|
+
const atHaiku = requestCost(HAIKU, u, at);
|
|
345
|
+
const billed = asBilled.totalUsd;
|
|
334
346
|
const move = how.moves && (base ? true : cheaper(cand, price));
|
|
335
|
-
const other = move ?
|
|
347
|
+
const other = move ? base ? atBaseline(base, u, at, opts.baseline) : atCand : asBilled;
|
|
336
348
|
t.requests++;
|
|
337
349
|
counted = true;
|
|
338
350
|
t.billedUsd += billed;
|
|
339
|
-
t.atCandidateUsd += move ?
|
|
340
|
-
t.atHaikuUsd +=
|
|
351
|
+
t.atCandidateUsd += move ? other.totalUsd : billed;
|
|
352
|
+
t.atHaikuUsd += atHaiku.totalUsd;
|
|
341
353
|
if (promptTokens(u) > HAIKU_TIER)
|
|
342
354
|
t.over100k++;
|
|
343
355
|
if (move)
|
|
344
356
|
add(movedBy, model, billed);
|
|
345
357
|
if (how.moves)
|
|
346
|
-
add(beforeBy, model,
|
|
358
|
+
add(beforeBy, model, atCand.totalUsd);
|
|
347
359
|
if (r.id)
|
|
348
|
-
priced.set(r.id, { billed:
|
|
360
|
+
priced.set(r.id, { billed: asBilled.rates, other: other.rates, haiku: atHaiku.rates, cand: atCand.rates, model });
|
|
349
361
|
}
|
|
350
362
|
if (counted)
|
|
351
363
|
t.agents++;
|
|
@@ -394,13 +406,8 @@ export function renderSubagentTypes(r) {
|
|
|
394
406
|
const over = ro.reduce((s, t) => s + t.over100k, 0);
|
|
395
407
|
const reqs = ro.reduce((s, t) => s + t.requests, 0);
|
|
396
408
|
const what = `Read-only work (${ro.map((t) => t.type).join(", ")}, ${usd(billed)}) at Haiku 5.5`;
|
|
397
|
-
// Haiku 5.5's rates hold to 100k-token prompts;
|
|
398
|
-
|
|
399
|
-
out.push(`${what}: ~${usd(haiku)}.`);
|
|
400
|
-
else if (over <= 0.1 * reqs)
|
|
401
|
-
out.push(`${what}: over ~${usd(haiku)} (${over} of ${reqs} requests had prompts over 100k tokens, which Haiku bills higher).`);
|
|
402
|
-
else
|
|
403
|
-
out.push(`${what}: not priced, ${over} of ${reqs} requests had prompts over 100k tokens, where Haiku bills a higher rate.`);
|
|
409
|
+
// Haiku 5.5's rates hold to 100k-token prompts; a longer one bills all of it at 5x, priced in.
|
|
410
|
+
out.push(`${what}: ~${usd(haiku)}${over ? ` (${over} of ${reqs} requests had prompts over 100k tokens, at Haiku's higher rates for those)` : ""}.`);
|
|
404
411
|
out.push("Only an agent of your own runs there (`model: haiku` in its file); the built-ins keep the main conversation's model.");
|
|
405
412
|
}
|
|
406
413
|
return out;
|
package/dist/subagentpay.d.ts
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
* window for a hard task) and what the parent would have spent on its own tool
|
|
25
25
|
* calls, so "did not pay" means "did not pay in context", not "was a mistake".
|
|
26
26
|
*/
|
|
27
|
+
import { type ModelPricing } from "./pricing.js";
|
|
27
28
|
/**
|
|
28
29
|
* The model `fix` offers the subagents (subagentmodel.ts): the id the price
|
|
29
30
|
* table knows, which Claude Code's model catalog maps to every provider's
|
|
@@ -72,7 +73,11 @@ export interface PaybackReport {
|
|
|
72
73
|
}>;
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* A subagent's bill (each request as billed, requestCost), steps, startup
|
|
78
|
+
* context, start time, the tool output it read, and the rates its first
|
|
79
|
+
* request billed at.
|
|
80
|
+
*/
|
|
76
81
|
export declare function scanSubagent(path: string): {
|
|
77
82
|
costUsd: number;
|
|
78
83
|
atSonnetUsd: number;
|
|
@@ -83,6 +88,7 @@ export declare function scanSubagent(path: string): {
|
|
|
83
88
|
start: number;
|
|
84
89
|
model?: string;
|
|
85
90
|
writeMult: number;
|
|
91
|
+
rates?: ModelPricing;
|
|
86
92
|
};
|
|
87
93
|
export declare function subagentPayback(days?: number, paths?: string[], onProgress?: (done: number, total: number) => void): PaybackReport;
|
|
88
94
|
export declare function renderPayback(r: PaybackReport, top?: number): string;
|
package/dist/subagentpay.js
CHANGED
|
@@ -29,7 +29,8 @@ import { basename, join } from "node:path";
|
|
|
29
29
|
import { forEachLine, listSessions, outputSettler, replyCounter, isClaudeSession } from "./session.js";
|
|
30
30
|
import { subagentFiles, workflowRunInfo } from "./subagents.js";
|
|
31
31
|
import { estimateTokens, formatTokens, realModel } from "./tokens.js";
|
|
32
|
-
import { cacheWriteSplit, formatUsd,
|
|
32
|
+
import { cacheWriteSplit, formatUsd, rateCard, ratesAt, requestCost, writeMultiplier, writePerM } from "./pricing.js";
|
|
33
|
+
import { priceBasis } from "./orgrates.js";
|
|
33
34
|
/**
|
|
34
35
|
* The model `fix` offers the subagents (subagentmodel.ts): the id the price
|
|
35
36
|
* table knows, which Claude Code's model catalog maps to every provider's
|
|
@@ -56,13 +57,30 @@ function blockTokens(content) {
|
|
|
56
57
|
}
|
|
57
58
|
return n;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const cheaper = (a, b) => a.inputPerM < b.inputPerM || (a.inputPerM === b.inputPerM && a.outputPerM < b.outputPerM);
|
|
61
|
+
/**
|
|
62
|
+
* A request as billed (requestCost: its tier, fast mode, US-only, its date,
|
|
63
|
+
* web searches) and re-priced for the Sonnet line: at Sonnet 5.5 when that
|
|
64
|
+
* bills it for less, else as billed.
|
|
65
|
+
*/
|
|
66
|
+
function billed(model, u, at) {
|
|
67
|
+
const own = requestCost(model, u, at);
|
|
68
|
+
if (!own)
|
|
69
|
+
return undefined;
|
|
70
|
+
const sonnet = requestCost(SUBAGENT_CANDIDATE, u, at);
|
|
71
|
+
const moved = cheaper(sonnet.rates, own.rates);
|
|
72
|
+
return { usd: own.totalUsd, sonnetUsd: moved ? sonnet.totalUsd : own.totalUsd, rates: own.rates, sonnetRates: moved ? sonnet.rates : own.rates };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A subagent's bill (each request as billed, requestCost), steps, startup
|
|
76
|
+
* context, start time, the tool output it read, and the rates its first
|
|
77
|
+
* request billed at.
|
|
78
|
+
*/
|
|
63
79
|
export function scanSubagent(path) {
|
|
64
|
-
let costUsd = 0, atSonnetUsd = 0, readTokens = 0, task = "", model;
|
|
80
|
+
let costUsd = 0, atSonnetUsd = 0, readTokens = 0, task = "", model, firstRates;
|
|
65
81
|
let steps = 0, startup = 0, start = Infinity, writes5m = 0, writes1h = 0;
|
|
82
|
+
// The rates of the last reply, for the output its later rows settle.
|
|
83
|
+
let last;
|
|
66
84
|
const sighting = replyCounter();
|
|
67
85
|
const settle = outputSettler();
|
|
68
86
|
forEachLine(path, (line) => {
|
|
@@ -91,10 +109,9 @@ export function scanSubagent(path) {
|
|
|
91
109
|
if (seen === "repeat") {
|
|
92
110
|
// A later row of the same reply: only its final output count is new.
|
|
93
111
|
const more = settle(e, false);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
atSonnetUsd += (more * atSonnet(price).outputPerM) / 1e6;
|
|
112
|
+
if (more && last) {
|
|
113
|
+
costUsd += (more * last.rates.outputPerM) / 1e6;
|
|
114
|
+
atSonnetUsd += (more * last.sonnetRates.outputPerM) / 1e6;
|
|
98
115
|
}
|
|
99
116
|
return;
|
|
100
117
|
}
|
|
@@ -113,16 +130,18 @@ export function scanSubagent(path) {
|
|
|
113
130
|
const w = cacheWriteSplit(u);
|
|
114
131
|
writes5m += w.fiveMinute;
|
|
115
132
|
writes1h += w.oneHour;
|
|
116
|
-
const
|
|
117
|
-
|
|
133
|
+
const b = billed(model, u, Number.isFinite(at) ? at : undefined);
|
|
134
|
+
last = b;
|
|
135
|
+
if (!b)
|
|
118
136
|
return;
|
|
119
|
-
|
|
120
|
-
|
|
137
|
+
firstRates ??= b.rates;
|
|
138
|
+
costUsd += b.usd;
|
|
139
|
+
atSonnetUsd += b.sonnetUsd;
|
|
121
140
|
});
|
|
122
|
-
return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h) };
|
|
141
|
+
return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h), ...(firstRates ? { rates: firstRates } : {}) };
|
|
123
142
|
}
|
|
124
143
|
function scanParent(path, ids) {
|
|
125
|
-
const view = { requests: [], prompts: [], compactions: [], writes5m: 0, writes1h: 0, returns: new Map() };
|
|
144
|
+
const view = { requests: [], prompts: [], models: [], billing: [], compactions: [], writes5m: 0, writes1h: 0, returns: new Map() };
|
|
126
145
|
let lastId;
|
|
127
146
|
forEachLine(path, (line) => {
|
|
128
147
|
const hits = [...ids].filter((id) => line.includes(id));
|
|
@@ -143,15 +162,17 @@ function scanParent(path, ids) {
|
|
|
143
162
|
// Placeholder messages Claude Code writes itself ("<synthetic>") are not API requests.
|
|
144
163
|
if (e?.type === "assistant" && !e.isSidechain && e.message?.usage && e.message.id !== lastId && realModel(e.message.model)) {
|
|
145
164
|
lastId = e.message.id;
|
|
165
|
+
view.model = realModel(e.message.model) ?? view.model;
|
|
146
166
|
if (Number.isFinite(at)) {
|
|
147
167
|
const u = e.message.usage;
|
|
148
168
|
view.requests.push(at);
|
|
149
169
|
view.prompts.push((u.input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0));
|
|
170
|
+
view.billing.push({ speed: u.speed, inference_geo: u.inference_geo });
|
|
171
|
+
view.models.push(view.model);
|
|
150
172
|
}
|
|
151
173
|
const w = cacheWriteSplit(e.message.usage);
|
|
152
174
|
view.writes5m += w.fiveMinute;
|
|
153
175
|
view.writes1h += w.oneHour;
|
|
154
|
-
view.model = realModel(e.message.model) ?? view.model;
|
|
155
176
|
return;
|
|
156
177
|
}
|
|
157
178
|
if (!hits.length || !Number.isFinite(at) || e?.type === "assistant")
|
|
@@ -213,7 +234,6 @@ export function subagentPayback(days = 30, paths, onProgress) {
|
|
|
213
234
|
});
|
|
214
235
|
const ids = new Set(metas.flatMap((m) => [m.id, m.meta.toolUseId].filter((x) => Boolean(x))));
|
|
215
236
|
const view = scanParent(parent, ids);
|
|
216
|
-
const price = pricingFor(view.model);
|
|
217
237
|
// What the parent pays to write a token to its cache, from its own usage.
|
|
218
238
|
const parentWrite = writeMultiplier(view.writes5m, view.writes1h);
|
|
219
239
|
for (const m of metas) {
|
|
@@ -230,18 +250,24 @@ export function subagentPayback(days = 30, paths, onProgress) {
|
|
|
230
250
|
const laterRequests = view.requests.filter((t) => t > from && t < until).length;
|
|
231
251
|
const carried = Math.max(0, s.readTokens - returnedTokens);
|
|
232
252
|
// The parent's context when the subagent started: its last request before then.
|
|
233
|
-
let parentContextTokens = 0;
|
|
234
|
-
for (let i = 0; i < view.requests.length && view.requests[i] <= s.start; i++)
|
|
253
|
+
let parentContextTokens = 0, last = -1;
|
|
254
|
+
for (let i = 0; i < view.requests.length && view.requests[i] <= s.start; i++) {
|
|
235
255
|
parentContextTokens = view.prompts[i];
|
|
236
|
-
|
|
256
|
+
last = i;
|
|
257
|
+
}
|
|
258
|
+
// The parent's rates as its next request would have billed them (rateCard: its tier at that context, fast mode and US-only as its last request, the day).
|
|
259
|
+
// The parent as it ran then: a session that moved to another model later (Fable 5.1 to 5.2) is priced on the one it was on.
|
|
260
|
+
const card = rateCard(last >= 0 ? view.models[last] : view.model, last >= 0 ? view.billing[last] : undefined, Number.isFinite(s.start) ? s.start : undefined);
|
|
261
|
+
const price = card && ratesAt(card, parentContextTokens);
|
|
262
|
+
const sub = s.rates ?? price;
|
|
237
263
|
let inParentUsd = s.costUsd, avoidedUsd = 0;
|
|
238
264
|
if (price && sub) {
|
|
239
265
|
// Each side's cache writes at its own TTL mix (subagents write at 5
|
|
240
266
|
// minutes, the main thread mostly at 1 hour) and reads at its model's rate.
|
|
241
|
-
const startupUsd = (s.startup * (s.writeMult
|
|
267
|
+
const startupUsd = (s.startup * (writePerM(sub, s.writeMult) + Math.max(0, s.steps - 1) * sub.cacheReadPerM)) / 1e6;
|
|
242
268
|
const work = Math.max(0, s.costUsd - startupUsd) * (price.inputPerM / sub.inputPerM);
|
|
243
269
|
const rideAlong = (parentContextTokens * s.steps * price.cacheReadPerM) / 1e6;
|
|
244
|
-
avoidedUsd = (carried * (parentWrite
|
|
270
|
+
avoidedUsd = (carried * (writePerM(price, parentWrite) + laterRequests * price.cacheReadPerM)) / 1e6;
|
|
245
271
|
inParentUsd = work + rideAlong + avoidedUsd;
|
|
246
272
|
}
|
|
247
273
|
out.push({
|
|
@@ -271,7 +297,7 @@ export function subagentPayback(days = 30, paths, onProgress) {
|
|
|
271
297
|
}
|
|
272
298
|
/** The workflow agents' lines of `subagents`: what they cost, by run. */
|
|
273
299
|
function renderWorkflows(w, top) {
|
|
274
|
-
const out = ["", `Workflow agents: ${w.agents} in ${w.runs} workflow run${w.runs === 1 ? "" : "s"}, ${formatUsd(w.costUsd)}
|
|
300
|
+
const out = ["", `Workflow agents: ${w.agents} in ${w.runs} workflow run${w.runs === 1 ? "" : "s"}, ${formatUsd(w.costUsd)} ${priceBasis(false)} (each run's work handed off whole, so not weighed against the parent).`];
|
|
275
301
|
if (w.costUsd - w.atSonnetUsd >= 1)
|
|
276
302
|
out.push(`At ${SUBAGENT_CANDIDATE_NAME} (where they ran on a pricier model): ~${formatUsd(w.atSonnetUsd)}, token counts as recorded: an estimate.`);
|
|
277
303
|
for (const r of w.byRun.slice(0, top))
|
|
@@ -288,7 +314,7 @@ export function renderPayback(r, top = 8) {
|
|
|
288
314
|
const cost = all.reduce((s, a) => s + a.costUsd, 0);
|
|
289
315
|
const inParent = all.reduce((s, a) => s + a.inParentUsd, 0);
|
|
290
316
|
const paid = all.filter((a) => a.netUsd > 0);
|
|
291
|
-
out.push(`Last ${r.days} days: ${all.length} subagents in ${r.sessions} sessions cost ${formatUsd(cost)}
|
|
317
|
+
out.push(`Last ${r.days} days: ${all.length} subagents in ${r.sessions} sessions cost ${formatUsd(cost)} ${priceBasis(false)}.`, `The same work done in the parent would have cost ~${formatUsd(inParent)}: ${paid.length} of ${all.length} were cheaper as subagents, net ${inParent - cost >= 0 ? "+" : "−"}${formatUsd(Math.abs(inParent - cost))}.`);
|
|
292
318
|
const sonnet = all.reduce((s, a) => s + a.atSonnetUsd, 0);
|
|
293
319
|
if (cost - sonnet >= 1)
|
|
294
320
|
out.push(`At ${SUBAGENT_CANDIDATE_NAME} (where they ran on a pricier model) the same requests come to ~${formatUsd(sonnet)}, token counts as recorded: an estimate. context-doctor fix says which of them a setting moves.`);
|
package/dist/subagents.js
CHANGED
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
17
17
|
import { basename, join } from "node:path";
|
|
18
18
|
import { formatTokens } from "./tokens.js";
|
|
19
|
-
import { formatUsd,
|
|
19
|
+
import { formatUsd, requestCost } from "./pricing.js";
|
|
20
|
+
import { orgRatesLine } from "./orgrates.js";
|
|
20
21
|
import { forEachLine, outputSettler, replyCounter } from "./session.js";
|
|
21
22
|
import { realModel } from "./tokens.js";
|
|
22
23
|
/** Where Claude Code keeps a session's subagents: beside the transcript, under its id. */
|
|
@@ -107,6 +108,8 @@ function summarize(path) {
|
|
|
107
108
|
let last;
|
|
108
109
|
let cost = 0;
|
|
109
110
|
let priced = true;
|
|
111
|
+
// The last reply's rates: its later rows settle its output at them.
|
|
112
|
+
let rates;
|
|
110
113
|
// One reply is written as several rows with the same usage; counting rows
|
|
111
114
|
// doubled the calls and the bill (a real session: 6156 calls / $769 per
|
|
112
115
|
// row against 3228 / $387 per reply).
|
|
@@ -148,7 +151,7 @@ function summarize(path) {
|
|
|
148
151
|
if (seen === "repeat") {
|
|
149
152
|
// A later row of the same reply: only its final output count is new.
|
|
150
153
|
const more = settle(e, false);
|
|
151
|
-
const p = more ?
|
|
154
|
+
const p = more ? rates : undefined;
|
|
152
155
|
s.outputTokens += more;
|
|
153
156
|
if (p)
|
|
154
157
|
cost += (more * p.outputPerM) / 1_000_000;
|
|
@@ -163,15 +166,14 @@ function summarize(path) {
|
|
|
163
166
|
s.inputBilledTokens += input + read + write;
|
|
164
167
|
s.cacheReadTokens += read;
|
|
165
168
|
s.outputTokens += out;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
else
|
|
169
|
+
// Subagents write at the 5-minute TTL (1.25x), the main thread mostly
|
|
170
|
+
// at 1 hour (2x); the usage records which. As billed: requestCost.
|
|
171
|
+
const c = requestCost(s.model, u, Number.isFinite(at) ? at : undefined);
|
|
172
|
+
rates = c?.rates;
|
|
173
|
+
if (c)
|
|
174
|
+
cost += c.totalUsd;
|
|
175
|
+
else
|
|
173
176
|
priced = false;
|
|
174
|
-
}
|
|
175
177
|
}
|
|
176
178
|
});
|
|
177
179
|
if (first !== undefined && last !== undefined)
|
|
@@ -232,6 +234,9 @@ export function renderSubagents(report, parentInputUsd, top = 5, options = {}) {
|
|
|
232
234
|
return null;
|
|
233
235
|
const lines = [];
|
|
234
236
|
lines.push("Subagents (their own windows, your bill)");
|
|
237
|
+
const org = orgRatesLine();
|
|
238
|
+
if (org)
|
|
239
|
+
lines.push(org);
|
|
235
240
|
lines.push("─".repeat(56));
|
|
236
241
|
const cost = report.unpriced === 0 ? formatUsd(report.totalCostUsd) : `${formatUsd(report.totalCostUsd)}+ (${report.unpriced} unpriced)`;
|
|
237
242
|
lines.push(`${report.agents.length} subagent(s) made ${report.agents.reduce((t, a) => t + a.calls, 0)} API calls: ` +
|
package/dist/tasks.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
import { realModel } from "./tokens.js";
|
|
22
22
|
import { basename } from "node:path";
|
|
23
23
|
import { forEachLine, listSessions, outputSettler, replyCounter, isClaudeSession } from "./session.js";
|
|
24
|
-
import { formatUsd,
|
|
24
|
+
import { formatUsd, requestCost } from "./pricing.js";
|
|
25
25
|
/**
|
|
26
26
|
* One session's bill, priced as the prompt cache bills it, from the usage the
|
|
27
27
|
* transcript records. `since` counts only requests and commits from then on,
|
|
@@ -32,6 +32,8 @@ import { formatUsd, pricingFor, usageCost } from "./pricing.js";
|
|
|
32
32
|
export function sessionCost(path, since = 0, counted = { rows: new Set(), commits: new Set() }) {
|
|
33
33
|
let cwd, model;
|
|
34
34
|
let start = Infinity, end = 0, usd = 0, requests = 0, commits = 0;
|
|
35
|
+
// The last counted reply's rates (requestCost's): its later rows settle its output at them.
|
|
36
|
+
let rates;
|
|
35
37
|
const commitCalls = new Map();
|
|
36
38
|
const sighting = replyCounter(counted.rows);
|
|
37
39
|
const settle = outputSettler();
|
|
@@ -81,7 +83,7 @@ export function sessionCost(path, since = 0, counted = { rows: new Set(), commit
|
|
|
81
83
|
if (seen === "repeat") {
|
|
82
84
|
// A later row of a counted reply: only its final output count is new.
|
|
83
85
|
const more = settle(e, false);
|
|
84
|
-
const price = more ?
|
|
86
|
+
const price = more ? rates : undefined;
|
|
85
87
|
if (price)
|
|
86
88
|
usd += (more * price.outputPerM) / 1e6;
|
|
87
89
|
return;
|
|
@@ -94,11 +96,12 @@ export function sessionCost(path, since = 0, counted = { rows: new Set(), commit
|
|
|
94
96
|
end = Math.max(end, at);
|
|
95
97
|
}
|
|
96
98
|
model = realModel(e.message.model) ?? model;
|
|
97
|
-
// Each request at its own model's price: one session can switch models.
|
|
98
|
-
const
|
|
99
|
-
if (!
|
|
99
|
+
// Each request at its own model's price, as billed: one session can switch models.
|
|
100
|
+
const c = requestCost(model, e.message.usage, Number.isFinite(at) ? at : undefined);
|
|
101
|
+
if (!c)
|
|
100
102
|
return;
|
|
101
|
-
|
|
103
|
+
rates = c.rates;
|
|
104
|
+
usd += c.totalUsd;
|
|
102
105
|
settle(e, true);
|
|
103
106
|
requests++;
|
|
104
107
|
});
|
package/dist/tooloutput.js
CHANGED
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
*/
|
|
39
39
|
import { listSessions, isClaudeSession, replyCounter } from "./session.js";
|
|
40
40
|
import { pick, rowsOf, slimUsage } from "./scan.js";
|
|
41
|
-
import { cacheWriteSplit,
|
|
41
|
+
import { cacheWriteSplit, rateCard, ratesAt, requestCost, writeMultiplier } from "./pricing.js";
|
|
42
42
|
import { isCodeLike, realModel, tokensForLength } from "./tokens.js";
|
|
43
43
|
import { subagentTranscripts } from "./cachettl.js";
|
|
44
44
|
/** The settings key the lever writes. */
|
|
@@ -90,7 +90,7 @@ function resultText(content) {
|
|
|
90
90
|
return s;
|
|
91
91
|
}
|
|
92
92
|
export const TOOL_OUTPUT_ROWS = {
|
|
93
|
-
id: "tool-output.
|
|
93
|
+
id: "tool-output.2",
|
|
94
94
|
init: () => ({ ids: {}, calls: {} }),
|
|
95
95
|
line(raw, row, out, st) {
|
|
96
96
|
if (!raw.includes('"usage"') && !raw.includes('"tool_use"') && !raw.includes('"tool_result"') && !raw.includes("compact_boundary"))
|
|
@@ -209,14 +209,17 @@ function timeline(path, since, until, runRows) {
|
|
|
209
209
|
for (const x of unsized[th].splice(0))
|
|
210
210
|
x.model = m;
|
|
211
211
|
}
|
|
212
|
-
const
|
|
212
|
+
const card = rateCard(m, u, at);
|
|
213
|
+
const price = card && ratesAt(card, prompt);
|
|
214
|
+
const below = card?.over !== undefined && prompt > card.over ? card.rates : undefined;
|
|
213
215
|
out.push({
|
|
214
216
|
k: "r", prompt,
|
|
215
217
|
readPerM: price?.cacheReadPerM ?? 0,
|
|
216
218
|
writePerM: (price?.inputPerM ?? 0) * writeMult,
|
|
217
219
|
cold: num(u.cache_creation_input_tokens) > COLD_SHARE * prompt,
|
|
218
220
|
counted: !!price && seen === "new" && at >= since && at < until,
|
|
219
|
-
billedUsd:
|
|
221
|
+
billedUsd: requestCost(m, u, at)?.promptUsd ?? 0,
|
|
222
|
+
...(below && price ? { over: card.over, lower: below.inputPerM / price.inputPerM } : {}),
|
|
220
223
|
});
|
|
221
224
|
}
|
|
222
225
|
return threads;
|
|
@@ -266,6 +269,7 @@ function walk(lines, move, kind) {
|
|
|
266
269
|
// Never more than the request held.
|
|
267
270
|
const held = live.reduce((n, x) => n + x.e, 0);
|
|
268
271
|
const scale = held > s.prompt ? s.prompt / held : 1;
|
|
272
|
+
let here = 0;
|
|
269
273
|
for (const x of live) {
|
|
270
274
|
if (x.fresh && s.counted) {
|
|
271
275
|
moved++;
|
|
@@ -274,10 +278,15 @@ function walk(lines, move, kind) {
|
|
|
274
278
|
if (s.counted) {
|
|
275
279
|
const t = x.e * scale;
|
|
276
280
|
tokens += t;
|
|
277
|
-
|
|
281
|
+
const usd = (t * (x.fresh || s.cold ? s.writePerM : s.readPerM)) / 1e6;
|
|
282
|
+
savedUsd += usd;
|
|
283
|
+
here += usd;
|
|
278
284
|
}
|
|
279
285
|
x.fresh = false;
|
|
280
286
|
}
|
|
287
|
+
// Under its tier's threshold now: the rest of its prompt bills at the lower rates too.
|
|
288
|
+
if (s.counted && s.over !== undefined && s.lower !== undefined && s.prompt - held * scale <= s.over)
|
|
289
|
+
savedUsd += Math.max(0, s.billedUsd - here) * (1 - s.lower);
|
|
281
290
|
}
|
|
282
291
|
}
|
|
283
292
|
return { moved, results, tokens, savedUsd, openUsd };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "context-doctor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "Context and token optimizer for LLM agents: see where Claude Code, Cursor, Codex and Gemini CLI tokens go, then cut them: an every-prompt hook, smarter compaction, an autopilot proxy that edits history only when the prompt cache is cold, RAG chunk packing and an MCP server. Local, no API key. By gAI Ventures.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|