context-doctor 0.51.0 → 0.53.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 +30 -32
- package/dist/cli.js +27 -7
- package/dist/doctor.js +26 -3
- package/dist/fix.d.ts +30 -0
- package/dist/fix.js +51 -5
- package/dist/limits.d.ts +80 -0
- package/dist/limits.js +185 -0
- package/dist/mcp.js +55 -55
- package/dist/modelids.d.ts +44 -0
- package/dist/modelids.js +174 -0
- package/dist/pricecheck.d.ts +11 -0
- package/dist/pricecheck.js +32 -0
- package/dist/pricing.js +12 -3
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +1 -1
- package/dist/sample.d.ts +8 -0
- package/dist/sample.js +86 -0
- package/dist/savings.d.ts +8 -1
- package/dist/savings.js +32 -7
- package/dist/starts.d.ts +39 -0
- package/dist/starts.js +157 -1
- package/package.json +1 -1
package/dist/modelids.d.ts
CHANGED
|
@@ -37,6 +37,50 @@ export declare function unpriced(ids: string[]): string[];
|
|
|
37
37
|
* transcription) and the tools table are not models priced by the token.
|
|
38
38
|
*/
|
|
39
39
|
export declare function openaiPageIds(markdown: string): string[];
|
|
40
|
+
/** One model's per-1M rates as OpenAI's price page states them: short context, and the long tier where it has one. */
|
|
41
|
+
export interface PagePrice {
|
|
42
|
+
model: string;
|
|
43
|
+
short: {
|
|
44
|
+
input: number;
|
|
45
|
+
output: number;
|
|
46
|
+
cached?: number;
|
|
47
|
+
};
|
|
48
|
+
long?: {
|
|
49
|
+
input: number;
|
|
50
|
+
output: number;
|
|
51
|
+
cached?: number;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The prices in OpenAI's price page's Standard tables (not Batch, Flex, Fast
|
|
56
|
+
* or Ultrafast): the main table's "Short context …" and "Long context …"
|
|
57
|
+
* columns, and the specialized tables' "(short context)" / "(long context)"
|
|
58
|
+
* row pairs. A "-" cell is no price (no cached rate, or no long tier). So
|
|
59
|
+
* the weekly check can compare what we charge with what the page says, not
|
|
60
|
+
* only that a model has some price.
|
|
61
|
+
*/
|
|
62
|
+
export declare function openaiPagePrices(markdown: string): PagePrice[];
|
|
63
|
+
/**
|
|
64
|
+
* The prices in Anthropic's price page's model table (platform.claude.com
|
|
65
|
+
* pricing.md, "Model | Base input tokens | … | Cache hits and refreshes |
|
|
66
|
+
* Output tokens"): each "Claude Opus 5.5" row as `claude-opus-5-5`, notes in
|
|
67
|
+
* parentheses and footnote marks dropped. A model priced in two rows by
|
|
68
|
+
* prompt size (Haiku 5.5's "for prompts over 100,000 tokens") gets the
|
|
69
|
+
* second as its long tier. Cache writes are not read: they are 1.25x and 2x
|
|
70
|
+
* input by rule, which pricing.ts applies.
|
|
71
|
+
*/
|
|
72
|
+
export declare function anthropicPagePrices(markdown: string): PagePrice[];
|
|
73
|
+
/**
|
|
74
|
+
* The prices in Google's price page (ai.google.dev/gemini-api/docs/pricing,
|
|
75
|
+
* its .md.txt): in each model's section, the Standard tab's paid-tier cells
|
|
76
|
+
* for input, output and context caching. A cell's first price is today's
|
|
77
|
+
* (a later "$4.00 starting December 1" line is the dated row's `then`, which
|
|
78
|
+
* the dated check covers); "prompts <= 200k" / "prompts > 200k" lines split
|
|
79
|
+
* a short and a long tier; per-hour storage lines and audio rates after a
|
|
80
|
+
* text rate are not read. A section whose cells do not read this way is
|
|
81
|
+
* left out rather than guessed.
|
|
82
|
+
*/
|
|
83
|
+
export declare function googlePagePrices(markdown: string): PagePrice[];
|
|
40
84
|
/**
|
|
41
85
|
* The models Google's price page (ai.google.dev/gemini-api/docs/pricing, its
|
|
42
86
|
* .md.txt) prices: each section's model id line (`*[\`gemini-4-argon\`](...)*`),
|
package/dist/modelids.js
CHANGED
|
@@ -117,6 +117,180 @@ export function openaiPageIds(markdown) {
|
|
|
117
117
|
}
|
|
118
118
|
return [...out].sort();
|
|
119
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The prices in OpenAI's price page's Standard tables (not Batch, Flex, Fast
|
|
122
|
+
* or Ultrafast): the main table's "Short context …" and "Long context …"
|
|
123
|
+
* columns, and the specialized tables' "(short context)" / "(long context)"
|
|
124
|
+
* row pairs. A "-" cell is no price (no cached rate, or no long tier). So
|
|
125
|
+
* the weekly check can compare what we charge with what the page says, not
|
|
126
|
+
* only that a model has some price.
|
|
127
|
+
*/
|
|
128
|
+
export function openaiPagePrices(markdown) {
|
|
129
|
+
const out = new Map();
|
|
130
|
+
const usd = (c) => {
|
|
131
|
+
const m = c?.match(/^\$([0-9]+(?:\.[0-9]+)?)$/);
|
|
132
|
+
return m ? Number(m[1]) : undefined;
|
|
133
|
+
};
|
|
134
|
+
let speed = "";
|
|
135
|
+
let header;
|
|
136
|
+
for (const raw of markdown.split("\n")) {
|
|
137
|
+
const line = raw.trim();
|
|
138
|
+
if (/^(Standard|Batch|Flex|Fast|Ultrafast|Priority)$/.test(line))
|
|
139
|
+
speed = line;
|
|
140
|
+
if (!line.startsWith("|")) {
|
|
141
|
+
header = undefined;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const cells = line.split("|").slice(1, -1).map((c) => c.trim());
|
|
145
|
+
if (!header) {
|
|
146
|
+
header = cells.map((c) => c.toLowerCase());
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (speed !== "Standard" || cells.every((c) => /^-+$/.test(c)))
|
|
150
|
+
continue;
|
|
151
|
+
// A Training column is fine-tuning's table: the price of a fine-tuned model, not the model's own.
|
|
152
|
+
if (header.includes("modality") || header.includes("use case") || header.includes("training") || header[0] === "tool")
|
|
153
|
+
continue;
|
|
154
|
+
const at = header.indexOf("model");
|
|
155
|
+
if (at === -1)
|
|
156
|
+
continue;
|
|
157
|
+
const cell = (cells[at] ?? "").replace(/<[^>]*>/g, "").trim();
|
|
158
|
+
const name = cell.replace(/\s*\(.*\)\s*$/, "").trim();
|
|
159
|
+
if (!/^[a-z][a-z0-9.-]*$/.test(name) || NOT_A_TEXT_MODEL.test(name))
|
|
160
|
+
continue;
|
|
161
|
+
const col = (h) => usd(cells[header.indexOf(h)]);
|
|
162
|
+
const tier = (prefix) => {
|
|
163
|
+
const input = col(`${prefix}input`), output = col(`${prefix}output`);
|
|
164
|
+
if (input === undefined || output === undefined)
|
|
165
|
+
return undefined;
|
|
166
|
+
const cached = col(`${prefix}cached input`);
|
|
167
|
+
return { input, output, ...(cached !== undefined ? { cached } : {}) };
|
|
168
|
+
};
|
|
169
|
+
const entry = out.get(name) ?? { model: name, short: undefined };
|
|
170
|
+
if (header.includes("short context input")) {
|
|
171
|
+
const short = tier("short context "), long = tier("long context ");
|
|
172
|
+
if (!short)
|
|
173
|
+
continue;
|
|
174
|
+
entry.short = short;
|
|
175
|
+
if (long)
|
|
176
|
+
entry.long = long;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const t = tier("");
|
|
180
|
+
if (!t)
|
|
181
|
+
continue;
|
|
182
|
+
if (/\(long context\)/i.test(cell))
|
|
183
|
+
entry.long = t;
|
|
184
|
+
else
|
|
185
|
+
entry.short = t;
|
|
186
|
+
}
|
|
187
|
+
out.set(name, entry);
|
|
188
|
+
}
|
|
189
|
+
return [...out.values()].filter((e) => e.short).sort((a, b) => a.model.localeCompare(b.model));
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The prices in Anthropic's price page's model table (platform.claude.com
|
|
193
|
+
* pricing.md, "Model | Base input tokens | … | Cache hits and refreshes |
|
|
194
|
+
* Output tokens"): each "Claude Opus 5.5" row as `claude-opus-5-5`, notes in
|
|
195
|
+
* parentheses and footnote marks dropped. A model priced in two rows by
|
|
196
|
+
* prompt size (Haiku 5.5's "for prompts over 100,000 tokens") gets the
|
|
197
|
+
* second as its long tier. Cache writes are not read: they are 1.25x and 2x
|
|
198
|
+
* input by rule, which pricing.ts applies.
|
|
199
|
+
*/
|
|
200
|
+
export function anthropicPagePrices(markdown) {
|
|
201
|
+
const out = new Map();
|
|
202
|
+
const usd = (c) => {
|
|
203
|
+
const m = c?.replace(/<sup>.*?<\/sup>/g, "").match(/^\$([0-9]+(?:\.[0-9]+)?) \/ MTok$/);
|
|
204
|
+
return m ? Number(m[1]) : undefined;
|
|
205
|
+
};
|
|
206
|
+
let header;
|
|
207
|
+
for (const raw of markdown.split("\n")) {
|
|
208
|
+
const line = raw.trim();
|
|
209
|
+
if (!line.startsWith("|")) {
|
|
210
|
+
header = undefined;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const cells = line.split("|").slice(1, -1).map((c) => c.trim());
|
|
214
|
+
if (!header) {
|
|
215
|
+
header = cells.map((c) => c.toLowerCase());
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (cells.every((c) => /^-+$/.test(c)))
|
|
219
|
+
continue;
|
|
220
|
+
const [m, i, o, h] = ["model", "base input tokens", "output tokens", "cache hits and refreshes"].map((k) => header.indexOf(k));
|
|
221
|
+
if (m === -1 || i === -1 || o === -1 || h === -1)
|
|
222
|
+
continue;
|
|
223
|
+
const cell = cells[m] ?? "";
|
|
224
|
+
const name = cell.replace(/\s*\(.*\)\s*$/, "").trim();
|
|
225
|
+
if (!/^Claude [A-Za-z]+ [0-9.]+$/.test(name))
|
|
226
|
+
continue;
|
|
227
|
+
const id = "claude-" + name.slice(7).toLowerCase().replace(/[ .]/g, "-");
|
|
228
|
+
const input = usd(cells[i]), output = usd(cells[o]), cached = usd(cells[h]);
|
|
229
|
+
if (input === undefined || output === undefined)
|
|
230
|
+
continue;
|
|
231
|
+
const tier = { input, output, ...(cached !== undefined ? { cached } : {}) };
|
|
232
|
+
const prev = out.get(id);
|
|
233
|
+
if (prev && /prompts over/i.test(cell))
|
|
234
|
+
prev.long = tier;
|
|
235
|
+
else if (!prev)
|
|
236
|
+
out.set(id, { model: id, short: tier });
|
|
237
|
+
}
|
|
238
|
+
return [...out.values()].sort((a, b) => a.model.localeCompare(b.model));
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The prices in Google's price page (ai.google.dev/gemini-api/docs/pricing,
|
|
242
|
+
* its .md.txt): in each model's section, the Standard tab's paid-tier cells
|
|
243
|
+
* for input, output and context caching. A cell's first price is today's
|
|
244
|
+
* (a later "$4.00 starting December 1" line is the dated row's `then`, which
|
|
245
|
+
* the dated check covers); "prompts <= 200k" / "prompts > 200k" lines split
|
|
246
|
+
* a short and a long tier; per-hour storage lines and audio rates after a
|
|
247
|
+
* text rate are not read. A section whose cells do not read this way is
|
|
248
|
+
* left out rather than guessed.
|
|
249
|
+
*/
|
|
250
|
+
export function googlePagePrices(markdown) {
|
|
251
|
+
const out = [];
|
|
252
|
+
const sections = markdown.split(/^## /m).slice(1);
|
|
253
|
+
for (const sec of sections) {
|
|
254
|
+
const id = sec.match(/\{% id="([a-z0-9.-]+)" %\}/)?.[1];
|
|
255
|
+
if (!id || !/^gemini-/.test(id) || NOT_A_TEXT_MODEL.test(id))
|
|
256
|
+
continue;
|
|
257
|
+
// The Standard tab when the section has tabs, else its first table.
|
|
258
|
+
const std = sec.indexOf('{% tab title="Standard" %}');
|
|
259
|
+
const body = std === -1 ? sec : sec.slice(std, sec.indexOf("{% /tab %}", std) >>> 0 || undefined);
|
|
260
|
+
const rows = body.split(/^\s*---\s*$/m);
|
|
261
|
+
const cellOf = (label) => {
|
|
262
|
+
const row = rows.find((r) => label.test(r.split("\n").find((l) => /^\s*\*\s/.test(l)) ?? ""));
|
|
263
|
+
if (!row)
|
|
264
|
+
return undefined;
|
|
265
|
+
const bullets = row.split(/^\s*\*\s/m).slice(1);
|
|
266
|
+
return bullets[2];
|
|
267
|
+
};
|
|
268
|
+
const tiers = (cell) => {
|
|
269
|
+
if (!cell)
|
|
270
|
+
return undefined;
|
|
271
|
+
let short, long;
|
|
272
|
+
for (const line of cell.split("\n")) {
|
|
273
|
+
const m = line.trim().match(/^\$([0-9]+(?:\.[0-9]+)?)\b(.*)$/);
|
|
274
|
+
if (!m || /starting|per hour|storage/i.test(m[2]))
|
|
275
|
+
continue;
|
|
276
|
+
const v = Number(m[1]);
|
|
277
|
+
if (/prompts\s*>\s*\d/i.test(m[2]))
|
|
278
|
+
long ??= v;
|
|
279
|
+
else
|
|
280
|
+
short ??= v;
|
|
281
|
+
}
|
|
282
|
+
return short === undefined ? undefined : { short, long };
|
|
283
|
+
};
|
|
284
|
+
const i = tiers(cellOf(/Input price/)), o = tiers(cellOf(/Output price/)), c = tiers(cellOf(/Context caching price/));
|
|
285
|
+
if (!i || !o)
|
|
286
|
+
continue;
|
|
287
|
+
const entry = { model: id, short: { input: i.short, output: o.short, ...(c ? { cached: c.short } : {}) } };
|
|
288
|
+
if (i.long !== undefined && o.long !== undefined)
|
|
289
|
+
entry.long = { input: i.long, output: o.long, ...(c?.long !== undefined ? { cached: c.long } : {}) };
|
|
290
|
+
out.push(entry);
|
|
291
|
+
}
|
|
292
|
+
return out.sort((a, b) => a.model.localeCompare(b.model));
|
|
293
|
+
}
|
|
120
294
|
/**
|
|
121
295
|
* The models Google's price page (ai.google.dev/gemini-api/docs/pricing, its
|
|
122
296
|
* .md.txt) prices: each section's model id line (`*[\`gemini-4-argon\`](...)*`),
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does what we charge match what a provider's price page says? The weekly
|
|
3
|
+
* check (`scripts/check-models.mjs`) asked only whether each model had some
|
|
4
|
+
* price, so a row the page later changed (GPT-6 Rosalind went from $15 to
|
|
5
|
+
* $10 per 1M input) or a long-prompt tier a row left out (GPT-6 Astra for
|
|
6
|
+
* Law) stayed wrong without a red. This compares each of the page's
|
|
7
|
+
* Standard prices with our rate card for the model, as of `at`.
|
|
8
|
+
*/
|
|
9
|
+
import type { PagePrice } from "./modelids.js";
|
|
10
|
+
/** One line per model whose price differs from the page's, in plain words; empty when all agree. */
|
|
11
|
+
export declare function pagePriceMismatches(prices: PagePrice[], at?: number): string[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { rateCard } from "./pricing.js";
|
|
2
|
+
const same = (a, b) => Math.abs(a - b) <= 1e-9 + 1e-6 * Math.max(a, b);
|
|
3
|
+
const money = (n) => "$" + String(Number(n.toFixed(4)));
|
|
4
|
+
/** One line per model whose price differs from the page's, in plain words; empty when all agree. */
|
|
5
|
+
export function pagePriceMismatches(prices, at = Date.now()) {
|
|
6
|
+
const out = [];
|
|
7
|
+
for (const p of prices) {
|
|
8
|
+
const card = rateCard(p.model, undefined, at);
|
|
9
|
+
// No price at all is the id check's job; an organization's contracted row is theirs, not the page's.
|
|
10
|
+
if (!card || card.org)
|
|
11
|
+
continue;
|
|
12
|
+
const diffs = [];
|
|
13
|
+
const tier = (label, page, ours) => {
|
|
14
|
+
if (!same(page.input, ours.inputPerM))
|
|
15
|
+
diffs.push(`${label} input ${money(page.input)} on the page, ${money(ours.inputPerM)} here`);
|
|
16
|
+
if (!same(page.output, ours.outputPerM))
|
|
17
|
+
diffs.push(`${label} output ${money(page.output)} on the page, ${money(ours.outputPerM)} here`);
|
|
18
|
+
if (page.cached !== undefined && !same(page.cached, ours.cacheReadPerM))
|
|
19
|
+
diffs.push(`${label} cached input ${money(page.cached)} on the page, ${money(ours.cacheReadPerM)} here`);
|
|
20
|
+
};
|
|
21
|
+
tier("short-context", p.short, card.rates);
|
|
22
|
+
if (p.long && !card.longRates)
|
|
23
|
+
diffs.push(`the page has a long-context price (${money(p.long.input)} / ${money(p.long.output)}) and src/pricing.ts has no long tier`);
|
|
24
|
+
else if (!p.long && card.longRates)
|
|
25
|
+
diffs.push("src/pricing.ts has a long tier the page does not list");
|
|
26
|
+
else if (p.long && card.longRates)
|
|
27
|
+
tier("long-context", p.long, card.longRates);
|
|
28
|
+
if (diffs.length)
|
|
29
|
+
out.push(`${p.model}: ${diffs.join("; ")}`);
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
package/dist/pricing.js
CHANGED
|
@@ -151,15 +151,24 @@ const PRICING = [
|
|
|
151
151
|
[openai("gpt-6.2-sol"), ...openaiLong(2, 10, 0.1)],
|
|
152
152
|
[openai("gpt-6-astra"), ...openaiLong(10, 50, 1)],
|
|
153
153
|
// GPT-6 Astra for Law's price is promotional "at least through December
|
|
154
|
-
// 31, 2026" (read 2026-11-14): no end date to record yet.
|
|
155
|
-
|
|
154
|
+
// 31, 2026" (read 2026-11-14): no end date to record yet. Its long-context
|
|
155
|
+
// row ($20 / $75 / $2, read 2026-11-26) is 2x / 1.5x; the row had left it out.
|
|
156
|
+
[openai("gpt-6-astra-law"), ...openaiLong(10, 50, 1)],
|
|
157
|
+
// The healthcare configurations (read 2026-11-26) are priced as their base
|
|
158
|
+
// models, promotional "at least through December 31, 2026" like the Law ones.
|
|
159
|
+
[openai("gpt-6.2-astra-healthcare"), ...openaiLong(8, 40, 0.4)],
|
|
160
|
+
[openai("gpt-6.2-sol-healthcare"), ...openaiLong(2, 10, 0.1)],
|
|
161
|
+
[openai("gpt-6-astra-healthcare"), ...openaiLong(10, 50, 1)],
|
|
162
|
+
[openai("gpt-6.1-sol-healthcare"), ...openaiLong(2, 10, 0.1)],
|
|
156
163
|
[openai("gpt-6.1-sol"), ...openaiLong(2, 10, 0.1)],
|
|
157
164
|
[openai("gpt-6.1-terra"), ...openaiLong(1, 5, 0.05)],
|
|
158
165
|
[openai("gpt-6-sol"), ...openaiLong(2, 10, 0.2)],
|
|
159
166
|
[openai("gpt-6-luna"), ...openaiLong(0.1, 0.5, 0.01)],
|
|
160
167
|
// gpt-6-cyber's long-context row ($40 / $150 / $4, read 2026-11-14) is 2x / 1.5x.
|
|
161
168
|
[openai("gpt-6-cyber"), ...openaiLong(20, 100, 2)],
|
|
162
|
-
|
|
169
|
+
// GPT-6 Rosalind: $10 / $50 / $1 (read 2026-11-26; $15 / $75 / $1.50 when
|
|
170
|
+
// first read), promotional "at least through December 31, 2026".
|
|
171
|
+
[openai("gpt-6-rosalind"), ...openaiLong(10, 50, 1)],
|
|
163
172
|
[openai("gpt-rosalind-research"), rate(5, 25, 0.5)],
|
|
164
173
|
[openai("gpt-rosalind-discovery"), rate(5, 25, 0.5)],
|
|
165
174
|
// GPT-5.6 Sol's $4 / $20 is promotional "until November 30, 2026" (read
|
package/dist/proxy.d.ts
CHANGED
|
@@ -83,7 +83,7 @@ export interface ProxyOptions extends OptimizeOptions {
|
|
|
83
83
|
/** Default request body cap: far above any real request (a 1M-token context is ~4 MB of JSON, images aside). */
|
|
84
84
|
export declare const DEFAULT_MAX_BODY_BYTES: number;
|
|
85
85
|
/** Reported by /health so `autopilot status` can tell an outdated service from a current one. */
|
|
86
|
-
export declare const PROXY_VERSION = "0.
|
|
86
|
+
export declare const PROXY_VERSION = "0.53.0";
|
|
87
87
|
/**
|
|
88
88
|
* Remove a leading `/t/<token>` from a request path, or return undefined when
|
|
89
89
|
* the prefix is absent or the token differs. The comparison is constant time
|
package/dist/proxy.js
CHANGED
|
@@ -25,7 +25,7 @@ import { makeGuard, refusal } from "./httpguard.js";
|
|
|
25
25
|
/** Default request body cap: far above any real request (a 1M-token context is ~4 MB of JSON, images aside). */
|
|
26
26
|
export const DEFAULT_MAX_BODY_BYTES = 100 * 1024 * 1024;
|
|
27
27
|
/** Reported by /health so `autopilot status` can tell an outdated service from a current one. */
|
|
28
|
-
export const PROXY_VERSION = "0.
|
|
28
|
+
export const PROXY_VERSION = "0.53.0";
|
|
29
29
|
/** Connection-level headers that must not be forwarded. */
|
|
30
30
|
const SKIP_REQUEST_HEADERS = new Set(["host", "content-length", "connection", "transfer-encoding", "accept-encoding", "expect"]);
|
|
31
31
|
/** A reply carrying any of these has something for the limits ledger: a plan window, or the usage-credits state. */
|
package/dist/sample.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface SampleHistory {
|
|
2
|
+
/** Transcript paths, ready for estimateSavings(days, {}, paths). */
|
|
3
|
+
paths: string[];
|
|
4
|
+
/** Delete the temp folder. Call it in a finally. */
|
|
5
|
+
cleanup: () => void;
|
|
6
|
+
}
|
|
7
|
+
/** Two synthetic sessions: one with a cold return past the prompt cache, one without. */
|
|
8
|
+
export declare function writeSampleHistory(now?: number): SampleHistory;
|
package/dist/sample.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `savings --sample`: a demonstration report. The sessions are synthetic,
|
|
3
|
+
* written to a temp folder here and deleted right after; the report is the
|
|
4
|
+
* real replay pipeline (replaySession, estimateSavings) run over them, so
|
|
5
|
+
* every dollar is the product's own arithmetic on generated data. Nothing is
|
|
6
|
+
* measured on the user's machine and no usage is invented for them: the
|
|
7
|
+
* output says SAMPLE and says the data is generated. It is never substituted
|
|
8
|
+
* for the real report: with no history, `savings` says so and offers the flag.
|
|
9
|
+
*
|
|
10
|
+
* Deterministic apart from the anchor time: contents come from a seeded
|
|
11
|
+
* generator, timestamps are relative to `now` so the sessions always fall
|
|
12
|
+
* inside the report's 30-day window.
|
|
13
|
+
*/
|
|
14
|
+
import { mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { tmpdir } from "node:os";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
const MIN = 60_000;
|
|
18
|
+
const DAY = 86_400_000;
|
|
19
|
+
function rng(seed) {
|
|
20
|
+
return () => {
|
|
21
|
+
seed |= 0;
|
|
22
|
+
seed = (seed + 0x6d2b79f5) | 0;
|
|
23
|
+
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
|
24
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
25
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** A Read result, big enough to matter once it goes stale. */
|
|
29
|
+
const BIG_READ = "const value = compute(input, options); // step\n".repeat(400);
|
|
30
|
+
function sessionRows(s) {
|
|
31
|
+
const r = rng(s.seed);
|
|
32
|
+
const rows = [];
|
|
33
|
+
let t = s.start;
|
|
34
|
+
const iso = (ms) => new Date(ms).toISOString();
|
|
35
|
+
rows.push(JSON.stringify({ type: "user", cwd: "/work/app", sessionId: s.id, version: "2.1.300", entrypoint: "cli", uuid: `${s.id}-q0`, timestamp: iso(t), message: { role: "user", content: "Work through the backlog in src/, and run the tests as you go." } }));
|
|
36
|
+
const PROMPTS = ["Fix the failing test", "Now the next ticket", "That broke the build, revert it", "Add the retry and run the suite again", "Looks right, continue"];
|
|
37
|
+
const breakAt = new Map((s.breaks ?? []).map((b) => [b.at, b.hours]));
|
|
38
|
+
let prompt = s.promptStart;
|
|
39
|
+
for (let i = 0; i < s.requests; i++) {
|
|
40
|
+
const brk = breakAt.get(i);
|
|
41
|
+
t += brk !== undefined ? brk * 60 * MIN : 30_000 + Math.floor(r() * 60_000);
|
|
42
|
+
// Each request starts with what the person typed, as real transcripts do;
|
|
43
|
+
// the return after a long break always does.
|
|
44
|
+
if (i > 0 && (brk !== undefined || r() < 0.5)) {
|
|
45
|
+
rows.push(JSON.stringify({ type: "user", cwd: "/work/app", sessionId: s.id, version: "2.1.300", entrypoint: "cli", uuid: `${s.id}-u${i}`, timestamp: iso(t - 2_000), message: { role: "user", content: PROMPTS[Math.floor(r() * PROMPTS.length)] } }));
|
|
46
|
+
}
|
|
47
|
+
const usage = {
|
|
48
|
+
input_tokens: 5 + Math.floor(r() * 20),
|
|
49
|
+
cache_read_input_tokens: prompt,
|
|
50
|
+
cache_creation_input_tokens: s.promptStep,
|
|
51
|
+
output_tokens: 100 + Math.floor(r() * 400),
|
|
52
|
+
cache_creation: { ephemeral_5m_input_tokens: 0, ephemeral_1h_input_tokens: s.promptStep },
|
|
53
|
+
service_tier: "standard",
|
|
54
|
+
};
|
|
55
|
+
const toolId = `toolu_${s.id}_${i}`;
|
|
56
|
+
rows.push(JSON.stringify({
|
|
57
|
+
type: "assistant", cwd: "/work/app", sessionId: s.id, version: "2.1.300", entrypoint: "cli", uuid: `${s.id}-a${i}`, timestamp: iso(t),
|
|
58
|
+
message: { id: `msg_${s.id}_${i}`, type: "message", role: "assistant", model: s.model, content: [{ type: "tool_use", id: toolId, name: "Read", input: { file_path: `/work/app/src/file${i % 7}.ts` } }], usage },
|
|
59
|
+
}));
|
|
60
|
+
rows.push(JSON.stringify({
|
|
61
|
+
type: "user", cwd: "/work/app", sessionId: s.id, version: "2.1.300", entrypoint: "cli", uuid: `${s.id}-r${i}`, timestamp: iso(t + 1_000),
|
|
62
|
+
message: { role: "user", content: [{ type: "tool_result", tool_use_id: toolId, content: BIG_READ }] },
|
|
63
|
+
}));
|
|
64
|
+
prompt += s.promptStep;
|
|
65
|
+
}
|
|
66
|
+
return { rows, end: t + 2_000 };
|
|
67
|
+
}
|
|
68
|
+
/** Two synthetic sessions: one with a cold return past the prompt cache, one without. */
|
|
69
|
+
export function writeSampleHistory(now = Date.now()) {
|
|
70
|
+
const dir = mkdtempSync(join(tmpdir(), "context-doctor-sample-"));
|
|
71
|
+
const specs = [
|
|
72
|
+
// A big session, come back to twice after the cache expired: both levers show.
|
|
73
|
+
{ id: "sample-a", start: now - 6 * DAY, requests: 40, seed: 42, model: "claude-opus-5-5", promptStart: 200_000, promptStep: 15_000, breaks: [{ at: 12, hours: 3 }, { at: 26, hours: 26 }] },
|
|
74
|
+
// A smaller session without a cold return: not every session has one.
|
|
75
|
+
{ id: "sample-b", start: now - 2 * DAY, requests: 14, seed: 7, model: "claude-opus-5-5", promptStart: 60_000, promptStep: 8_000 },
|
|
76
|
+
];
|
|
77
|
+
const paths = [];
|
|
78
|
+
for (const s of specs) {
|
|
79
|
+
const { rows, end } = sessionRows(s);
|
|
80
|
+
const p = join(dir, `${s.id}.jsonl`);
|
|
81
|
+
writeFileSync(p, rows.join("\n") + "\n");
|
|
82
|
+
utimesSync(p, end / 1000, end / 1000);
|
|
83
|
+
paths.push(p);
|
|
84
|
+
}
|
|
85
|
+
return { paths, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
|
|
86
|
+
}
|
package/dist/savings.d.ts
CHANGED
|
@@ -94,7 +94,14 @@ export declare function renderSavings(r: SavingsReport, autopilotOn?: boolean, c
|
|
|
94
94
|
hours: number;
|
|
95
95
|
netUsd: number;
|
|
96
96
|
pingUsd: number;
|
|
97
|
-
}): string;
|
|
97
|
+
}, sample?: boolean): string;
|
|
98
|
+
/**
|
|
99
|
+
* The demonstration report: the real replay over the synthetic history from
|
|
100
|
+
* sample.ts, said to be a sample in its first and last lines. Shown only by
|
|
101
|
+
* `savings --sample`; without it, no history stays an honest empty report
|
|
102
|
+
* that offers the flag.
|
|
103
|
+
*/
|
|
104
|
+
export declare function renderSampleSavings(r: SavingsReport): string;
|
|
98
105
|
/**
|
|
99
106
|
* `savings --share`: a few lines people can paste or screenshot. Totals only:
|
|
100
107
|
* no project names, paths or session ids, so sharing it leaks nothing.
|
package/dist/savings.js
CHANGED
|
@@ -354,10 +354,11 @@ export function estimateSavings(days = 30, options = {}, paths, onProgress, repl
|
|
|
354
354
|
}
|
|
355
355
|
const tokens = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)}B` : formatTokens(n));
|
|
356
356
|
const usd = (n) => (n >= 100 ? `$${Math.round(n).toLocaleString("en-US")}` : `$${n.toFixed(2)}`);
|
|
357
|
-
export function renderSavings(r, autopilotOn = false, cw, missLines, limitLines, lensLines, keepWarm) {
|
|
357
|
+
export function renderSavings(r, autopilotOn = false, cw, missLines, limitLines, lensLines, keepWarm, sample = false) {
|
|
358
358
|
const lines = [];
|
|
359
359
|
if (r.sessions.length === 0) {
|
|
360
|
-
lines.push(`No Claude Code sessions with 10+ requests in the last ${r.days} days to replay.`);
|
|
360
|
+
lines.push(`No Claude Code sessions with 10+ requests in the last ${r.days} days to replay: nothing of yours was measured.`);
|
|
361
|
+
lines.push("See the same report on generated example sessions: `context-doctor savings --sample`.");
|
|
361
362
|
lines.push("Use Claude Code for a while, then run this again: `context-doctor savings`.");
|
|
362
363
|
return withInvocation(lines.join("\n"));
|
|
363
364
|
}
|
|
@@ -365,9 +366,11 @@ export function renderSavings(r, autopilotOn = false, cw, missLines, limitLines,
|
|
|
365
366
|
const allDesktop = r.unreachableSessions === n;
|
|
366
367
|
const pct = (x) => (r.billedUsd > 0 ? ` (${Math.round((x / r.billedUsd) * 100)}%)` : "");
|
|
367
368
|
// Kept within 80 columns: this is most people's first screen of the tool.
|
|
368
|
-
lines.push("What context-doctor finds in your Claude Code sessions");
|
|
369
|
+
lines.push(sample ? "SAMPLE: what context-doctor finds in Claude Code sessions" : "What context-doctor finds in your Claude Code sessions");
|
|
369
370
|
// The same count, said the same way, as compact-window's.
|
|
370
|
-
lines.push(
|
|
371
|
+
lines.push(sample
|
|
372
|
+
? `Generated example sessions (no real history here), ${priceBasis()}`
|
|
373
|
+
: `Last ${r.days} days, ${sessionsLabel(n, r.transcripts ?? n)}, ${priceBasis()}`);
|
|
371
374
|
lines.push("─".repeat(60));
|
|
372
375
|
lines.push(`Input in your main conversations ${usd(r.billedUsd).padStart(9)} from your transcripts' usage`);
|
|
373
376
|
// spend's total is larger: it adds output and the subagents. Said here, so the two do not look like they disagree.
|
|
@@ -424,12 +427,34 @@ export function renderSavings(r, autopilotOn = false, cw, missLines, limitLines,
|
|
|
424
427
|
lines.push(...missLines);
|
|
425
428
|
}
|
|
426
429
|
lines.push("");
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
+
if (sample) {
|
|
431
|
+
lines.push("Replayed request by request through the shipped code, on sessions");
|
|
432
|
+
lines.push("generated for this demonstration. Nothing was measured on this machine.");
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
lines.push("Replayed request by request through the shipped code and priced as the");
|
|
436
|
+
lines.push("prompt cache bills. On a subscription the same tokens come out of your");
|
|
437
|
+
lines.push("usage limit instead. Share yours: context-doctor savings --share");
|
|
438
|
+
lines.push("Run it again after a week of use: the report re-measures on what");
|
|
439
|
+
lines.push("changed, and `fix` shows what each applied fix has saved since.");
|
|
440
|
+
}
|
|
430
441
|
// Hints as the user can type them: `npx context-doctor …` after an npx run.
|
|
431
442
|
return withInvocation(lines.join("\n"));
|
|
432
443
|
}
|
|
444
|
+
/**
|
|
445
|
+
* The demonstration report: the real replay over the synthetic history from
|
|
446
|
+
* sample.ts, said to be a sample in its first and last lines. Shown only by
|
|
447
|
+
* `savings --sample`; without it, no history stays an honest empty report
|
|
448
|
+
* that offers the flag.
|
|
449
|
+
*/
|
|
450
|
+
export function renderSampleSavings(r) {
|
|
451
|
+
const body = renderSavings(r, false, undefined, undefined, undefined, undefined, undefined, true);
|
|
452
|
+
if (r.sessions.length === 0)
|
|
453
|
+
return body;
|
|
454
|
+
return body + "\n\n" + withInvocation("This is a demonstration on generated data, not your usage. Once this\n" +
|
|
455
|
+
"machine has real Claude Code sessions, `context-doctor savings` replays\n" +
|
|
456
|
+
"them the same way. See this sample again: `context-doctor savings --sample`.");
|
|
457
|
+
}
|
|
433
458
|
/**
|
|
434
459
|
* `savings --share`: a few lines people can paste or screenshot. Totals only:
|
|
435
460
|
* no project names, paths or session ids, so sharing it leaks nothing.
|
package/dist/starts.d.ts
CHANGED
|
@@ -97,17 +97,56 @@ export interface StartsReport {
|
|
|
97
97
|
tokens: number;
|
|
98
98
|
usd: number;
|
|
99
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* The period's compactions, and the servers whose tools went whole into
|
|
102
|
+
* some start that each compaction was followed by a call to (#111915: a
|
|
103
|
+
* tool loaded through tool search is deferred again after a compaction,
|
|
104
|
+
* so with the server behind tool search each would load once more).
|
|
105
|
+
* Each load priced as one more request: the context at that call read
|
|
106
|
+
* from the cache, plus one of the server's tool definitions written.
|
|
107
|
+
*/
|
|
108
|
+
compactions?: {
|
|
109
|
+
total: number;
|
|
110
|
+
after: Array<Array<{
|
|
111
|
+
server: string;
|
|
112
|
+
usd: number;
|
|
113
|
+
unpriced?: true;
|
|
114
|
+
}>>;
|
|
115
|
+
};
|
|
100
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* What the reload count reads from a transcript: each compaction boundary,
|
|
119
|
+
* and each assistant row that calls an MCP tool, with the request's usage
|
|
120
|
+
* (the context at that call). Read once with the other extractors (scan.ts).
|
|
121
|
+
*/
|
|
122
|
+
export declare const RELOAD_ROWS: Extractor<Record<string, never>>;
|
|
123
|
+
/** A Claude Code that ran sessions in the period: the entrypoint and version their transcripts recorded, and how many. */
|
|
124
|
+
export interface SessionHost {
|
|
125
|
+
/** "claude-desktop", "cli", "sdk-cli", "claude-vscode"... ("" when a transcript records none). */
|
|
126
|
+
entrypoint: string;
|
|
127
|
+
version?: string;
|
|
128
|
+
sessions: number;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Which Claude Code ran the sessions in the period: each entrypoint and
|
|
132
|
+
* version a main transcript recorded while it was in the period (limits.ts
|
|
133
|
+
* HOST_ROWS keeps a record where either changes), counted once per session.
|
|
134
|
+
* fix uses it to say when an install too old for a setting ran none of them.
|
|
135
|
+
*/
|
|
136
|
+
export declare function sessionHosts(paths: string[], since: number): SessionHost[];
|
|
101
137
|
/**
|
|
102
138
|
* Every session's and subagent's start in the period, grouped by entrypoint
|
|
103
139
|
* and subagent type, each priced on every request that re-read it.
|
|
104
140
|
* `paths`: the main transcripts (subagents are found beside each).
|
|
141
|
+
* `compactions`: also count the compactions followed by a call to a server
|
|
142
|
+
* sent whole (fix's desktop-tools-defer; `overhead` does not ask).
|
|
105
143
|
*/
|
|
106
144
|
export declare function measureStarts(opts?: {
|
|
107
145
|
days?: number;
|
|
108
146
|
paths?: string[];
|
|
109
147
|
cwd?: string;
|
|
110
148
|
home?: string;
|
|
149
|
+
compactions?: boolean;
|
|
111
150
|
}): StartsReport;
|
|
112
151
|
/** The servers whose tools went whole into some start and that the user's own config holds: `alwaysLoad: false` defers them. */
|
|
113
152
|
export declare function deferrableConfigured(r: StartsReport): Array<{
|