big-spender 0.6.0 → 0.7.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 +11 -9
- package/dist/collector.mjs +105 -10
- package/dist/schema.d.ts +9 -1
- package/dist/schema.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -327,19 +327,21 @@ about 15 to 20 percent less.
|
|
|
327
327
|
|
|
328
328
|
## How it counts
|
|
329
329
|
|
|
330
|
-
| Source
|
|
331
|
-
|
|
|
332
|
-
| Claude Code
|
|
333
|
-
| Codex
|
|
334
|
-
| Cursor
|
|
335
|
-
| pi
|
|
336
|
-
|
|
337
|
-
|
|
330
|
+
| Source | Files | Notes |
|
|
331
|
+
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
332
|
+
| Claude Code | `~/.claude/projects/**/*.jsonl`, `~/.config/claude/projects`, `$CLAUDE_CONFIG_DIR/projects` | `assistant` lines; de-duplicated by `message.id` + `requestId` (one API response is logged once per content block, and resumed sessions copy history). `<synthetic>` skipped; subagent logs included. Some Claude Code versions (2.1.293 to 2.1.310) log a partial output count on a message's first line and the final count on a later line; a later line that reports more output adds the difference. |
|
|
333
|
+
| Codex | `~/.codex/sessions/**/rollout-*.jsonl`, `$CODEX_HOME/sessions` | `event_msg` / `token_count` with `info.last_token_usage`; skipped when `total_token_usage` did not change. Model from the latest `turn_context`. `cached_input_tokens` is part of `input_tokens`; reasoning is part of `output_tokens`. |
|
|
334
|
+
| Cursor | Cursor's API, `DashboardService/GetFilteredUsageEvents`, with `BIG_SPENDER_CURSOR_API_KEY` | Events with token counts only. Cost is Cursor's `totalCents`. Events have no unique id and pages can overlap, so a time window with more than 1,000 events is split in half instead of paged. `cacheWriteTokens` count as 5-minute cache writes. Sessions are hashed `conversationId`s. Model names are lowercased, and anything outside `a-z 0-9 . _ : @ -` becomes `-`. |
|
|
335
|
+
| pi | `~/.pi/agent/sessions/**/*.jsonl`, `$PI_CODING_AGENT_DIR/sessions`, `$PI_CODING_AGENT_SESSION_DIR`, `$PI_AGENT_DIR` (comma-separated, as in ccusage) | Assistant `message` entries with `usage`; `input` excludes cached tokens, reasoning is part of `output`, and `cacheWrite` counts as 5-minute writes except its `cacheWrite1h` part. Calls with no tokens (aborted, failed before a retry) are skipped, and a call with only `totalTokens` counts it as output. A fork's file starts with a copy of its parent's calls up to the fork; those leading calls are skipped when the parent file has them. ccusage looks only at the parent's current branch, so it counts the copy again once the parent moves to another branch. A model id that is a file path (absolute, a `file://` or `host:/` URL, or ending in `.gguf` and similar) is reduced to its file name. `subagent-artifacts/` transcripts are skipped. Cost is LiteLLM's, not the cost pi stores. |
|
|
336
|
+
| Grok Build (experimental) | `~/.grok/sessions/**/updates.jsonl`, `$GROK_HOME/sessions` | **Experimental:** checked against ccusage on test fixtures built from its Grok parser tests, not yet against real Grok Build logs. `turn_completed` lines, one row per model in `usage.modelUsage` (the top-level usage under `summary.json`'s `current_model_id` when that map is missing). De-duplicated by `eventId` + model, also across sessions, since one event can be exported into several; a row without `eventId` by its full content within its file. `cachedReadTokens` and `cacheCreationTokens` are part of `inputTokens`; reasoning is part of `outputTokens`. Always the base rate: a turn sums several API requests, so its total cannot pick the long-context tier; a request of its own above 200K tokens is under-priced. A model id that is a file path is reduced to its file name. A turn cut off before it completes is never logged, so it is not counted. |
|
|
337
|
+
|
|
338
|
+
**Pricing.** Claude Code, Codex, pi and Grok Build use LiteLLM `model_prices_and_context_window.json`, cached
|
|
338
339
|
for 24 hours in `~/.cache/big-spender/`. Each source lists the LiteLLM keys to try for a model. Claude
|
|
339
340
|
Code and Codex try the exact model, then the `anthropic/` or `openai/` prefix, then without a date
|
|
340
341
|
suffix. pi tries the LiteLLM prefixes of the providers pi supports too (`openrouter/`, `fireworks_ai/`,
|
|
341
342
|
`groq/` and others). For an OpenRouter id such as `anthropic/claude-sonnet-4.5` it then tries
|
|
342
|
-
`claude-sonnet-4.5` and `claude-sonnet-4-5`.
|
|
343
|
+
`claude-sonnet-4.5` and `claude-sonnet-4-5`. Grok Build tries the model and its `xai/` key, then
|
|
344
|
+
both without a trailing `-build`. Unknown models count with cost 0 and log a warning.
|
|
343
345
|
Long-context tiers
|
|
344
346
|
(`*_above_<N>k_tokens`) are applied per request; 1-hour cache writes use
|
|
345
347
|
`cache_creation_input_token_cost_above_1hr`. Cache savings are cache-read tokens times (input price
|
package/dist/collector.mjs
CHANGED
|
@@ -25,7 +25,7 @@ import { createHash } from 'node:crypto';
|
|
|
25
25
|
import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';
|
|
26
26
|
import { mkdir, open, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
27
27
|
import { homedir } from 'node:os';
|
|
28
|
-
import { dirname, join, resolve, sep } from 'node:path';
|
|
28
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
29
29
|
import { gzipSync } from 'node:zlib';
|
|
30
30
|
|
|
31
31
|
const SCHEMA_VERSION = 1;
|
|
@@ -411,7 +411,7 @@ function costOf(entry, tier, t) {
|
|
|
411
411
|
// ---------------------------------------------------------------------------
|
|
412
412
|
// Log discovery + incremental reading
|
|
413
413
|
|
|
414
|
-
async function* walk(dir) {
|
|
414
|
+
async function* walk(dir, keep) {
|
|
415
415
|
let entries;
|
|
416
416
|
try {
|
|
417
417
|
entries = await readdir(dir, { withFileTypes: true });
|
|
@@ -420,8 +420,8 @@ async function* walk(dir) {
|
|
|
420
420
|
}
|
|
421
421
|
for (const e of entries) {
|
|
422
422
|
const p = join(dir, e.name);
|
|
423
|
-
if (e.isDirectory()) yield* walk(p);
|
|
424
|
-
else if (e.isFile() && e.name
|
|
423
|
+
if (e.isDirectory()) yield* walk(p, keep);
|
|
424
|
+
else if (e.isFile() && keep(e.name)) yield p;
|
|
425
425
|
}
|
|
426
426
|
}
|
|
427
427
|
|
|
@@ -673,7 +673,7 @@ const PI_PRICE_PREFIXES = [
|
|
|
673
673
|
];
|
|
674
674
|
|
|
675
675
|
// Local models can have a file path as their id; only the file name leaves the machine.
|
|
676
|
-
const
|
|
676
|
+
const localModelName = (model) => {
|
|
677
677
|
const isPath =
|
|
678
678
|
/^([/~.]|[A-Za-z]:)|\\|:\/|\/\//.test(model) ||
|
|
679
679
|
/\.(gguf|ggml|bin|safetensors|pth?|onnx)$/i.test(model);
|
|
@@ -723,7 +723,7 @@ function piLineHandler(state, file, path, resolvePrice, cutoffMs) {
|
|
|
723
723
|
if (u.ts < cutoffMs) return;
|
|
724
724
|
if (state.seen[dedupe] !== undefined) return;
|
|
725
725
|
state.seen[dedupe] = u.ts;
|
|
726
|
-
const model =
|
|
726
|
+
const model = localModelName(u.model);
|
|
727
727
|
addRecord(state, {
|
|
728
728
|
ts: u.ts,
|
|
729
729
|
provider: 'pi',
|
|
@@ -739,16 +739,100 @@ function piLineHandler(state, file, path, resolvePrice, cutoffMs) {
|
|
|
739
739
|
};
|
|
740
740
|
}
|
|
741
741
|
|
|
742
|
+
/** A Grok model id as a snapshot model name: `[grok] ` dropped, a path reduced to its file name. */
|
|
743
|
+
const grokModelName = (raw) => localModelName(raw.replace(/^\[grok\] /, '').trim()) || 'unknown';
|
|
744
|
+
|
|
745
|
+
// Grok Build: a completed turn appends one `turn_completed` line whose usage sums the turn's API
|
|
746
|
+
// requests, split per model in `modelUsage` (top-level usage and the session's current model when
|
|
747
|
+
// that map is missing). inputTokens includes cache reads and cache writes; reasoningTokens is part
|
|
748
|
+
// of outputTokens. The same event can be exported into more than one session, so rows with an
|
|
749
|
+
// eventId are deduplicated by eventId and model, others by their full content within the file.
|
|
750
|
+
function grokLineHandler(state, cutoffMs, path) {
|
|
751
|
+
const sessionDir = basename(dirname(path));
|
|
752
|
+
let currentModel;
|
|
753
|
+
const summaryModel = () => {
|
|
754
|
+
if (currentModel === undefined) {
|
|
755
|
+
try {
|
|
756
|
+
const id = JSON.parse(
|
|
757
|
+
readFileSync(join(dirname(path), 'summary.json'), 'utf8')
|
|
758
|
+
)?.current_model_id;
|
|
759
|
+
currentModel = typeof id === 'string' && id ? id : 'unknown';
|
|
760
|
+
} catch {
|
|
761
|
+
currentModel = 'unknown';
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
return currentModel;
|
|
765
|
+
};
|
|
766
|
+
const str = (v) => (typeof v === 'string' && v ? v : undefined);
|
|
767
|
+
// Token counts are integers; ccusage reads anything else as 0, and the schema rejects it.
|
|
768
|
+
const count = (v) => (Number.isInteger(v) ? num(v) : 0);
|
|
769
|
+
return (line) => {
|
|
770
|
+
if (!line.includes('"turn_completed"')) return;
|
|
771
|
+
const o = parseJson(line);
|
|
772
|
+
const p = o?.params;
|
|
773
|
+
const usage = p?.update?.sessionUpdate === 'turn_completed' ? p.update.usage : undefined;
|
|
774
|
+
if (!usage || typeof usage !== 'object') return;
|
|
775
|
+
// agentTimestampMs is milliseconds; the envelope timestamp is Unix seconds.
|
|
776
|
+
const ts = num(p._meta?.agentTimestampMs) || num(o.timestamp) * 1000;
|
|
777
|
+
if (!ts || ts < cutoffMs) return;
|
|
778
|
+
const eventId = str(p._meta?.eventId);
|
|
779
|
+
const session = str(p.sessionId) ?? sessionDir;
|
|
780
|
+
const perModel =
|
|
781
|
+
usage.modelUsage &&
|
|
782
|
+
typeof usage.modelUsage === 'object' &&
|
|
783
|
+
Object.keys(usage.modelUsage).length;
|
|
784
|
+
// Top-level usage is keyed without its model: summary.json can appear or change between
|
|
785
|
+
// runs, and the same event must not count again under the new name.
|
|
786
|
+
const byModel = perModel
|
|
787
|
+
? Object.entries(usage.modelUsage).map(([raw, u]) => [raw, raw, u])
|
|
788
|
+
: [[summaryModel(), '', usage]];
|
|
789
|
+
for (const [raw, keyModel, u] of byModel) {
|
|
790
|
+
const inputAll = count(u?.inputTokens);
|
|
791
|
+
const cacheRead = Math.min(count(u?.cachedReadTokens), inputAll);
|
|
792
|
+
const cacheWrite = Math.min(count(u?.cacheCreationTokens), inputAll - cacheRead);
|
|
793
|
+
const input = inputAll - cacheRead - cacheWrite;
|
|
794
|
+
const output = count(u?.outputTokens);
|
|
795
|
+
const reasoning = count(u?.reasoningTokens);
|
|
796
|
+
if (!input && !cacheRead && !cacheWrite && !output && !reasoning) continue;
|
|
797
|
+
// Without an eventId, identical rows count once per file, as in ccusage.
|
|
798
|
+
const id = eventId
|
|
799
|
+
? `${eventId}|${keyModel}`
|
|
800
|
+
: `${path}|${ts}|${raw}|${input}|${output}|${cacheRead}|${cacheWrite}|${reasoning}`;
|
|
801
|
+
const dedupe = `g:${shortHash(id)}`;
|
|
802
|
+
if (state.seen[dedupe] !== undefined) continue;
|
|
803
|
+
state.seen[dedupe] = ts;
|
|
804
|
+
addRecord(state, {
|
|
805
|
+
ts,
|
|
806
|
+
provider: 'grok',
|
|
807
|
+
model: grokModelName(raw),
|
|
808
|
+
// xAI picks the long-context rate per request, and a turn sums several requests, so
|
|
809
|
+
// its total cannot pick a tier. Grok's own costUsdTicks bill such turns at base rate.
|
|
810
|
+
tier: 0,
|
|
811
|
+
session: shortHash(`grok:${session}`),
|
|
812
|
+
input,
|
|
813
|
+
cacheRead,
|
|
814
|
+
cacheWrite5m: cacheWrite,
|
|
815
|
+
cacheWrite1h: 0,
|
|
816
|
+
output
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
const isJsonl = (name) => name.endsWith('.jsonl');
|
|
823
|
+
|
|
742
824
|
/**
|
|
743
|
-
* A reader for JSONL logs that only grow: walks
|
|
744
|
-
* bytes added since the last run. `lines`
|
|
825
|
+
* A reader for JSONL logs that only grow: walks the files under each path whose name `keep`
|
|
826
|
+
* accepts (any `*.jsonl` by default) and parses only the bytes added since the last run. `lines`
|
|
827
|
+
* builds the line handler for one file.
|
|
745
828
|
* @param {(ctx: ReadContext, file: FileState, path: string) => (line: string) => void} lines
|
|
829
|
+
* @param {(name: string) => boolean} [keep]
|
|
746
830
|
*/
|
|
747
|
-
const jsonlReader = (lines) => async (paths, ctx, provider) => {
|
|
831
|
+
const jsonlReader = (lines, keep) => async (paths, ctx, provider) => {
|
|
748
832
|
const { state } = ctx;
|
|
749
833
|
for (const dir of paths) {
|
|
750
834
|
if (!existsSync(dir)) continue;
|
|
751
|
-
for await (const path of walk(dir)) {
|
|
835
|
+
for await (const path of walk(dir, keep ?? isJsonl)) {
|
|
752
836
|
if (ctx.seenPaths.has(path)) continue;
|
|
753
837
|
ctx.seenPaths.add(path);
|
|
754
838
|
let st;
|
|
@@ -867,6 +951,17 @@ const SOURCES = {
|
|
|
867
951
|
)
|
|
868
952
|
];
|
|
869
953
|
}
|
|
954
|
+
},
|
|
955
|
+
grok: {
|
|
956
|
+
paths: [join(HOME, '.grok', 'sessions')],
|
|
957
|
+
envPaths: () => fromEnv('GROK_HOME', 'sessions'),
|
|
958
|
+
read: jsonlReader(
|
|
959
|
+
(ctx, _file, path) => grokLineHandler(ctx.state, ctx.cutoffMs, path),
|
|
960
|
+
(name) => name === 'updates.jsonl'
|
|
961
|
+
),
|
|
962
|
+
// Grok Build logs `grok-4.5-build`; LiteLLM prices it as `xai/grok-4.5`.
|
|
963
|
+
priceKeys: (model) =>
|
|
964
|
+
[model, model.replace(/-build$/, '')].flatMap((base) => [base, `xai/${base}`])
|
|
870
965
|
}
|
|
871
966
|
};
|
|
872
967
|
|
package/dist/schema.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare const EARLIEST_DATE = "2020-01-01T00:00:00Z";
|
|
|
3
3
|
export declare const MAX_ROWS = 50000;
|
|
4
4
|
export declare const DEVICE_ID: RegExp;
|
|
5
5
|
export declare const CURSOR_DEVICE_ID = "cursor";
|
|
6
|
-
export declare const PROVIDERS: readonly ['claude', 'codex', 'cursor', 'pi'];
|
|
6
|
+
export declare const PROVIDERS: readonly ['claude', 'codex', 'cursor', 'pi', 'grok'];
|
|
7
7
|
export declare const RANGES: readonly ['7d', '30d', '90d', 'all'];
|
|
8
8
|
export declare const usageRowSchema: z.ZodObject<{
|
|
9
9
|
hour: z.ZodISODateTime;
|
|
@@ -15,6 +15,7 @@ export declare const usageRowSchema: z.ZodObject<{
|
|
|
15
15
|
claude: "claude";
|
|
16
16
|
codex: "codex";
|
|
17
17
|
cursor: "cursor";
|
|
18
|
+
grok: "grok";
|
|
18
19
|
pi: "pi";
|
|
19
20
|
}>;
|
|
20
21
|
model: z.ZodString;
|
|
@@ -38,6 +39,7 @@ export declare const limitWindowSchema: z.ZodObject<{
|
|
|
38
39
|
claude: "claude";
|
|
39
40
|
codex: "codex";
|
|
40
41
|
cursor: "cursor";
|
|
42
|
+
grok: "grok";
|
|
41
43
|
pi: "pi";
|
|
42
44
|
}>;
|
|
43
45
|
window: z.ZodString;
|
|
@@ -61,6 +63,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
61
63
|
claude: "claude";
|
|
62
64
|
codex: "codex";
|
|
63
65
|
cursor: "cursor";
|
|
66
|
+
grok: "grok";
|
|
64
67
|
pi: "pi";
|
|
65
68
|
}>;
|
|
66
69
|
model: z.ZodString;
|
|
@@ -85,6 +88,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
85
88
|
codex: z.ZodNumber;
|
|
86
89
|
all: z.ZodNumber;
|
|
87
90
|
cursor: z.ZodOptional<z.ZodNumber>;
|
|
91
|
+
grok: z.ZodOptional<z.ZodNumber>;
|
|
88
92
|
pi: z.ZodOptional<z.ZodNumber>;
|
|
89
93
|
}, z.core.$strict>>;
|
|
90
94
|
'7d': z.ZodObject<{
|
|
@@ -92,6 +96,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
92
96
|
codex: z.ZodNumber;
|
|
93
97
|
all: z.ZodNumber;
|
|
94
98
|
cursor: z.ZodOptional<z.ZodNumber>;
|
|
99
|
+
grok: z.ZodOptional<z.ZodNumber>;
|
|
95
100
|
pi: z.ZodOptional<z.ZodNumber>;
|
|
96
101
|
}, z.core.$strict>;
|
|
97
102
|
'30d': z.ZodObject<{
|
|
@@ -99,6 +104,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
99
104
|
codex: z.ZodNumber;
|
|
100
105
|
all: z.ZodNumber;
|
|
101
106
|
cursor: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
grok: z.ZodOptional<z.ZodNumber>;
|
|
102
108
|
pi: z.ZodOptional<z.ZodNumber>;
|
|
103
109
|
}, z.core.$strict>;
|
|
104
110
|
'90d': z.ZodObject<{
|
|
@@ -106,6 +112,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
106
112
|
codex: z.ZodNumber;
|
|
107
113
|
all: z.ZodNumber;
|
|
108
114
|
cursor: z.ZodOptional<z.ZodNumber>;
|
|
115
|
+
grok: z.ZodOptional<z.ZodNumber>;
|
|
109
116
|
pi: z.ZodOptional<z.ZodNumber>;
|
|
110
117
|
}, z.core.$strict>;
|
|
111
118
|
}, z.core.$strict>>;
|
|
@@ -114,6 +121,7 @@ export declare const usageSnapshotSchema: z.ZodObject<{
|
|
|
114
121
|
claude: "claude";
|
|
115
122
|
codex: "codex";
|
|
116
123
|
cursor: "cursor";
|
|
124
|
+
grok: "grok";
|
|
117
125
|
pi: "pi";
|
|
118
126
|
}>;
|
|
119
127
|
window: z.ZodString;
|
package/dist/schema.js
CHANGED
|
@@ -14,7 +14,7 @@ export const MAX_ROWS = 50_000;
|
|
|
14
14
|
export const DEVICE_ID = /^[a-z0-9][a-z0-9-]{0,31}$/;
|
|
15
15
|
// Collectors send the Cursor account's usage under this id, so no machine may use it.
|
|
16
16
|
export const CURSOR_DEVICE_ID = 'cursor';
|
|
17
|
-
export const PROVIDERS = ['claude', 'codex', 'cursor', 'pi'];
|
|
17
|
+
export const PROVIDERS = ['claude', 'codex', 'cursor', 'pi', 'grok'];
|
|
18
18
|
// Public ranges. 7d, 30d and 90d are bucketed by UTC day, `all` by ISO week. Nothing is finer
|
|
19
19
|
// than a day: an hourly view would show when each machine is in use.
|
|
20
20
|
export const RANGES = ['7d', '30d', '90d', 'all'];
|