big-spender 0.2.0 → 0.5.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 CHANGED
@@ -9,6 +9,15 @@ snapshot into that device's stored history, adds the devices together, and serve
9
9
  JSON and as a plain HTML page. You can also embed the server side in your own app and draw the
10
10
  numbers however you like.
11
11
 
12
+ ![A 30-day bar chart of daily AI coding cost, each bar stacked by machine (MacBook, VPS, Home agent), with a $3,148 total](docs/usage-card.png)
13
+
14
+ The card above is on [jeffreyyvdb.com](https://jeffreyyvdb.com). The site mounts
15
+ `big-spender/server` and draws its own chart from the summary, here 30 days of daily cost stacked
16
+ by machine.
17
+
18
+ Cursor keeps usage on its servers, so one machine can also read your Cursor account's usage from
19
+ Cursor's API and send it as its own device. See [Cursor](#cursor).
20
+
12
21
  The collector has no dependencies and needs Node 20 or newer. The package depends on `zod` only.
13
22
 
14
23
  ## What leaves the machine
@@ -19,12 +28,19 @@ their counts are sent. The server rejects any snapshot with extra fields, so not
19
28
  stored or served even by a modified collector.
20
29
 
21
30
  The server never publishes a device's report time (it would show when each machine is online), and
22
- every range is bucketed by UTC day, never by hour. Device ids are published only if you leave
31
+ nothing is bucketed finer than a UTC day: 7, 30 and 90 days are per day, all time is per ISO week
32
+ (Monday UTC). Device ids are published only if you leave
23
33
  `unknownDevices` at its default, see [Device names and colours](#device-names-and-colours).
24
34
 
25
35
  Before the first send you can see exactly what would leave the machine with
26
36
  `big-spender push <device> --dry-run`.
27
37
 
38
+ With [Cursor](#cursor) set up, the collector also calls Cursor's API with your Cursor API key. It
39
+ sends Cursor only the key and a date range. From the answer it keeps model names, token counts,
40
+ cost and timestamps, the same as from local logs. Conversation ids are hashed on the device and
41
+ only counted, like session ids. Everything else in the answer, such as your Cursor user id, is
42
+ dropped.
43
+
28
44
  ## Quick start
29
45
 
30
46
  ### 1. Run the server
@@ -83,6 +99,48 @@ Remove the schedule with `big-spender uninstall`. The env file and the state are
83
99
 
84
100
  `big-spender status` prints the last 24 hours on this machine and sends nothing.
85
101
 
102
+ ### Cursor
103
+
104
+ Cursor keeps usage on its servers, per account, so the collector reads it from Cursor's API
105
+ instead of local files. The server must run the big-spender release that added Cursor, or newer.
106
+ An older one answers the `cursor` snapshot with `422`. Set it up on one machine only:
107
+
108
+ ```sh
109
+ npx big-spender install --device laptop --cursor
110
+ ```
111
+
112
+ Only the account's personal usage is read. Usage billed to a Cursor team is not read yet.
113
+
114
+ It asks for a Cursor user API key with a hidden prompt (or reads `BIG_SPENDER_CURSOR_API_KEY`) and
115
+ adds it to the env file. Create the key in your Cursor dashboard. Running `install` again without
116
+ `--cursor` keeps the key; with `--cursor` it asks for a new one.
117
+
118
+ Cursor usage is sent as its own device, `cursor`, not as the machine that reads it, because one
119
+ account covers every machine you use Cursor on. If you move the key to another machine, remove it
120
+ from the first one, or both would send the same usage. Add `cursor` to your
121
+ [devices file](#device-names-and-colours) to give it a name and colour.
122
+
123
+ The first run sends all of the account's history that has token counts. Cursor billed per request
124
+ until about June 2025 and reported no tokens for those events, so they are skipped. Later runs
125
+ send the last 90 days, and the server keeps the older days. The full history is sent again until a
126
+ server has stored it, so a failed first run, a new ingest URL or a new API key gets it on the next run.
127
+
128
+ Cost is Cursor's own price for each event (`totalCents`), not LiteLLM's. Cursor's model names
129
+ mostly don't match LiteLLM's, and models such as Composer have no API price at all. Cursor reports
130
+ one price per event, so the split over input, cache read, cache write and output uses fixed ratios
131
+ of 1, 0.1, 1.25 and 5. The total is Cursor's, up to rounding each part to a millionth of a dollar.
132
+
133
+ A Cursor failure, such as a revoked key, does not stop the machine's own snapshot. The run still
134
+ posts it, then exits with an error that names Cursor. That includes a Cursor answer with fewer
135
+ events than it counted: posting it would replace complete days on the server with partial ones. The
136
+ device id `cursor` is reserved for this snapshot, so no machine can report under it. To switch to
137
+ another Cursor account, stop the server, run `big-spender install --cursor` with the new key, remove
138
+ `cursor.json` from the server's data directory, and start the server again. The install's own first
139
+ run fails while the server is down, which is expected. The next run sends the new account's full
140
+ history. The server keeps snapshots in memory, and the old key keeps posting until it is replaced,
141
+ so in any other order the two accounts' histories can be merged. `big-spender push` never reads
142
+ Cursor.
143
+
86
144
  ## The server
87
145
 
88
146
  `big-spender serve` is a small `node:http` server:
@@ -90,7 +148,7 @@ Remove the schedule with `big-spender uninstall`. The env file and the state are
90
148
  | Route | What it does |
91
149
  | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
92
150
  | `POST /api/usage/ingest` | Collectors post a snapshot here (gzip or plain JSON, bearer token). |
93
- | `GET /api/usage` | Merged totals for 7, 30 and 90 days as JSON, with `cache-control: public, max-age=300, stale-while-revalidate=900`. |
151
+ | `GET /api/usage` | Merged totals for 7, 30 and 90 days and all time as JSON, with `cache-control: public, max-age=300, stale-while-revalidate=900`. |
94
152
  | `GET /` | A plain server-rendered page: totals per range, devices with colour swatches, top models. No JavaScript, no external assets, light and dark. |
95
153
  | `GET /healthz` | `ok`, for load balancers and Docker. |
96
154
 
@@ -107,13 +165,15 @@ Configuration comes from flags or environment variables (flags win):
107
165
  The data directory holds one `<device>.json` file per device, written atomically. Put it on a
108
166
  persistent volume. Each snapshot is merged into the device's stored history. A day the snapshot
109
167
  reports replaces that day for each provider and model. Days it does not report are kept as day
110
- rows, so the server keeps usage from beyond the collector's 90-day window. The public ranges still
111
- end at 90 days. A device file holds at most 50,000 rows. Past that, the oldest kept days are dropped
168
+ rows, so the server keeps usage from beyond the collector's 90-day window, and the all-time range
169
+ shows it. All time has no session count (`sessions: null`): collectors count distinct sessions
170
+ on the device for 7, 30 and 90 days only. A device file holds at most 50,000 rows. Past that, the oldest kept days are dropped
112
171
  and the server logs a warning. A snapshot older than the stored one is ignored (`"result":
113
172
  "stale"`), so retries and duplicate posts are harmless. Files in the directory that are not valid snapshots are ignored with a warning.
114
173
 
115
174
  Ingest limits: request bodies up to 2 MB, 16 MB after decompression, `gzip` or `identity` encoding
116
- only. An invalid snapshot gets `422` with the first five problems.
175
+ only. An invalid snapshot gets `422` with the first five problems. Dates before 2020-01-01 are
176
+ invalid.
117
177
 
118
178
  ### Device names and colours
119
179
 
@@ -204,6 +264,9 @@ big-spender status # last 24 hours on this machine, not
204
264
  node ~/.local/share/big-spender/collector.mjs --dry-run # the full snapshot JSON, nothing sent
205
265
  ```
206
266
 
267
+ With [Cursor](#cursor) set up, `--dry-run` reads Cursor too and prints the `cursor` snapshot after
268
+ this machine's.
269
+
207
270
  Compare with [ccusage](https://github.com/ryoppippi/ccusage) on the same machine. Use the `claude`
208
271
  subcommand: plain `npx ccusage` also adds other CLIs that the collector does not read. Claude Code
209
272
  tokens and cost should match per model:
@@ -218,18 +281,35 @@ about 15 to 20 percent less.
218
281
 
219
282
  ## How it counts
220
283
 
221
- | Source | Files | Notes |
222
- | ----------- | ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
223
- | 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. |
224
- | 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`. |
225
-
226
- **Pricing.** LiteLLM `model_prices_and_context_window.json`, cached for 24 hours in
227
- `~/.cache/big-spender/`. Lookup tries the exact model, then `anthropic/` or `openai/` prefixes, then
228
- without a date suffix. Unknown models count with cost 0 and log a warning. Long-context tiers
284
+ | Source | Files | Notes |
285
+ | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
286
+ | 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. |
287
+ | 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`. |
288
+ | 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 `-`. |
289
+ | 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. |
290
+
291
+ **Pricing.** Claude Code, Codex and pi use LiteLLM `model_prices_and_context_window.json`, cached
292
+ for 24 hours in `~/.cache/big-spender/`. Each source lists the LiteLLM keys to try for a model. Claude
293
+ Code and Codex try the exact model, then the `anthropic/` or `openai/` prefix, then without a date
294
+ suffix. pi tries the LiteLLM prefixes of the providers pi supports too (`openrouter/`, `fireworks_ai/`,
295
+ `groq/` and others). For an OpenRouter id such as `anthropic/claude-sonnet-4.5` it then tries
296
+ `claude-sonnet-4.5` and `claude-sonnet-4-5`. Unknown models count with cost 0 and log a warning.
297
+ Long-context tiers
229
298
  (`*_above_<N>k_tokens`) are applied per request; 1-hour cache writes use
230
299
  `cache_creation_input_token_cost_above_1hr`. Cache savings are cache-read tokens times (input price
231
300
  minus cache-read price).
232
301
 
302
+ **Adding a provider.** Add an entry to `SOURCES` in `src/collector.mjs`, the provider id to
303
+ `PROVIDERS` in `src/schema.ts`, a row to the table above, and a test with a fixture log. The entry
304
+ says where the data lives, how it is read, and which LiteLLM keys price its models. Append-only JSONL
305
+ uses `jsonlReader`, which parses only new bytes. Other formats are read whole on every run and skip
306
+ what they counted before through `state.seen`. A SQLite file opens with `openSqlite`, which skips the
307
+ source with one warning on a Node without `node:sqlite`. Add the matching ccusage agent to `AGENTS`
308
+ in `scripts/parity-ccusage.mjs` and run `node scripts/parity-ccusage.mjs <provider>`: tokens must
309
+ match ccusage exactly. `node scripts/parity-ref.mjs` shows that the other providers did not change.
310
+ Deploy the server before the collectors, because a server rejects rows from a provider it does not
311
+ list.
312
+
233
313
  **Incremental.** `~/.cache/big-spender/state.json` keeps per-file byte offsets plus the hourly
234
314
  aggregates, so each run parses only new bytes but still sends the full 90-day snapshot. Rows older
235
315
  than 7 days are rolled up to daily rows. Because the aggregates live in the state file, usage stays