claude-cache-tax 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Muhammad Mairaj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,49 @@
1
- # Temporary Holding Version
1
+ # claude-cache-tax
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ See when your Claude Code prompt cache gets rebuilt.
4
+
5
+ Every reply in Claude Code either reads the cached conversation (cheap) or writes it again (the expensive part). This tool reads your local Claude Code session logs and shows where those cache writes come from: the first reply of a session, quick back-and-forth, or coming back after a pause long enough for the cache to expire.
6
+
7
+ ```
8
+ npx claude-cache-tax
9
+ ```
10
+
11
+ ```
12
+ Sessions: 430, replies: 47766, from 2026-09-25 to 2026-12-04
13
+ Cache writes: 236.4M tokens, cache reads: 15232.2M tokens (64 reads per write)
14
+ Cache lifetime of the writes: 1 hour 74.4%, 5 minutes 25.6%
15
+
16
+ Where the cache writes happen, by the pause before the reply:
17
+ first reply of a session 426 replies 5.9% of writes
18
+ after under 1 min 40892 replies 34.1% of writes
19
+ after 1 to 5 min 4702 replies 6.5% of writes
20
+ after 5 to 60 min 1362 replies 7.8% of writes
21
+ after over 60 min 384 replies 45.8% of writes
22
+ ```
23
+
24
+ (That is my own output. Under 1% of replies came after a pause of more than an hour, and they caused almost half of all cache writes.)
25
+
26
+ ## Options
27
+
28
+ | Option | What it does |
29
+ | --- | --- |
30
+ | `--dir <path>` | Folder with the session logs. Default: `~/.claude/projects` (or `$CLAUDE_CONFIG_DIR/projects`). |
31
+ | `--days <n>` | Only count the last n days. |
32
+ | `--json` | Print the raw numbers as JSON. |
33
+
34
+ ## Privacy
35
+
36
+ It reads only the timestamp, model name and token counts of each reply. Prompts, replies and file contents are never read into the results, and nothing leaves your machine. No dependencies.
37
+
38
+ ## How it counts
39
+
40
+ - One row per assistant reply; a reply logged twice with the same message id is counted once.
41
+ - The pause is the time since the previous reply in the same session file. Subagent logs are separate files, so their first reply counts as a session start.
42
+ - "Cache lifetime" comes from the `ephemeral_1h_input_tokens` and `ephemeral_5m_input_tokens` fields Claude Code records for each write.
43
+
44
+ ## What it does not do
45
+
46
+ - It does not price anything. Token prices and cache multipliers change; multiply the token counts by your plan's prices yourself.
47
+ - It cannot see requests made outside Claude Code (the API, other tools).
48
+
49
+ MIT licensed.
package/bin/cli.js ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { scan, defaultDir } = require('../src/scan');
5
+
6
+ const HELP = `claude-cache-tax: see when your Claude Code prompt cache gets rebuilt
7
+
8
+ Usage: npx claude-cache-tax [options]
9
+
10
+ --dir <path> folder with Claude Code session logs (default: ${defaultDir()})
11
+ --days <n> only count the last n days
12
+ --json print the raw numbers as JSON
13
+ -h, --help show this help
14
+
15
+ Reads only timestamps, model names and token counts from the local session
16
+ logs. Nothing is sent anywhere.`;
17
+
18
+ function parseArgs(argv) {
19
+ const opts = { dir: defaultDir(), days: null, json: false };
20
+ for (let i = 0; i < argv.length; i++) {
21
+ const a = argv[i];
22
+ if (a === '-h' || a === '--help') opts.help = true;
23
+ else if (a === '--json') opts.json = true;
24
+ else if (a === '--dir') opts.dir = argv[++i];
25
+ else if (a === '--days') opts.days = Number(argv[++i]);
26
+ else throw new Error(`Unknown option: ${a}`);
27
+ }
28
+ if (opts.days !== null && !(opts.days > 0)) throw new Error('--days needs a positive number');
29
+ return opts;
30
+ }
31
+
32
+ const pct = (part, whole) => (whole ? ((100 * part) / whole).toFixed(1) + '%' : '-');
33
+ const mtok = (n) => (n / 1e6).toFixed(1) + 'M';
34
+
35
+ function render(r) {
36
+ const t = r.total;
37
+ const lines = [];
38
+ lines.push(`Sessions: ${r.sessions}, replies: ${t.turns}, from ${r.from?.slice(0, 10)} to ${r.to?.slice(0, 10)}`);
39
+ lines.push(`Cache writes: ${mtok(t.write)} tokens, cache reads: ${mtok(t.read)} tokens (${(t.read / (t.write || 1)).toFixed(0)} reads per write)`);
40
+ lines.push(`Cache lifetime of the writes: 1 hour ${pct(t.write1h, t.write)}, 5 minutes ${pct(t.write5m, t.write)}`);
41
+ lines.push('');
42
+ lines.push('Where the cache writes happen, by the pause before the reply:');
43
+ lines.push(` ${'first reply of a session'.padEnd(26)} ${String(r.firstTurns.turns).padStart(7)} replies ${pct(r.firstTurns.write, t.write).padStart(6)} of writes`);
44
+ for (const b of r.buckets) {
45
+ lines.push(` ${('after ' + b.label).padEnd(26)} ${String(b.turns).padStart(7)} replies ${pct(b.write, t.write).padStart(6)} of writes`);
46
+ }
47
+ lines.push('');
48
+ lines.push('By model:');
49
+ for (const [m, v] of Object.entries(r.models).sort((a, b) => b[1].write - a[1].write)) {
50
+ lines.push(` ${m.padEnd(26)} ${String(v.turns).padStart(7)} replies writes ${mtok(v.write).padStart(7)} reads ${mtok(v.read).padStart(8)}`);
51
+ }
52
+ return lines.join('\n');
53
+ }
54
+
55
+ async function main() {
56
+ let opts;
57
+ try {
58
+ opts = parseArgs(process.argv.slice(2));
59
+ } catch (e) {
60
+ console.error(e.message + '\n\n' + HELP);
61
+ process.exit(2);
62
+ }
63
+ if (opts.help) return console.log(HELP);
64
+ const since = opts.days ? Date.now() - opts.days * 86400e3 : null;
65
+ try {
66
+ const r = await scan(opts.dir, { since });
67
+ console.log(opts.json ? JSON.stringify(r, null, 2) : render(r));
68
+ } catch (e) {
69
+ console.error(e.message);
70
+ process.exit(1);
71
+ }
72
+ }
73
+
74
+ main();
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "claude-cache-tax",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "See when your Claude Code prompt cache gets rebuilt: cache writes by the pause before each reply, 1-hour vs 5-minute cache, per model. Reads local logs only.",
5
+ "bin": {
6
+ "claude-cache-tax": "bin/cli.js"
7
+ },
8
+ "main": "src/scan.js",
9
+ "files": [
10
+ "bin",
11
+ "src"
12
+ ],
13
+ "scripts": {
14
+ "test": "node --test"
15
+ },
16
+ "engines": {
17
+ "node": ">=18"
18
+ },
19
+ "keywords": [
20
+ "claude",
21
+ "claude-code",
22
+ "prompt-caching",
23
+ "anthropic",
24
+ "llm-cost",
25
+ "cli"
26
+ ],
27
+ "author": "Muhammad Mairaj (https://thisismairaj.com)",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/thisismairaj/claude-cache-tax.git"
32
+ },
33
+ "homepage": "https://github.com/thisismairaj/claude-cache-tax#readme",
34
+ "bugs": "https://github.com/thisismairaj/claude-cache-tax/issues"
35
+ }
package/src/scan.js ADDED
@@ -0,0 +1,126 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const os = require('node:os');
6
+ const readline = require('node:readline');
7
+
8
+ const BUCKETS = [
9
+ { label: 'under 1 min', lo: 0, hi: 60 },
10
+ { label: '1 to 5 min', lo: 60, hi: 300 },
11
+ { label: '5 to 60 min', lo: 300, hi: 3600 },
12
+ { label: 'over 60 min', lo: 3600, hi: Infinity },
13
+ ];
14
+
15
+ function defaultDir() {
16
+ const base = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
17
+ return path.join(base, 'projects');
18
+ }
19
+
20
+ function listSessionFiles(dir) {
21
+ const out = [];
22
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
23
+ const full = path.join(dir, entry.name);
24
+ if (entry.isDirectory()) out.push(...listSessionFiles(full));
25
+ else if (entry.isFile() && entry.name.endsWith('.jsonl')) out.push(full);
26
+ }
27
+ return out;
28
+ }
29
+
30
+ // Reads only the timestamp, model and usage numbers of each assistant turn.
31
+ // Prompts and replies are never read into the result.
32
+ function turnFromLine(line) {
33
+ let d;
34
+ try {
35
+ d = JSON.parse(line);
36
+ } catch {
37
+ return null;
38
+ }
39
+ const m = d && d.type === 'assistant' ? d.message : null;
40
+ const u = m && typeof m === 'object' ? m.usage : null;
41
+ if (!u || !d.timestamp) return null;
42
+ const cc = u.cache_creation || {};
43
+ return {
44
+ id: m.id || null,
45
+ at: Date.parse(d.timestamp),
46
+ model: m.model || 'unknown',
47
+ write: u.cache_creation_input_tokens || 0,
48
+ read: u.cache_read_input_tokens || 0,
49
+ input: u.input_tokens || 0,
50
+ write1h: cc.ephemeral_1h_input_tokens || 0,
51
+ write5m: cc.ephemeral_5m_input_tokens || 0,
52
+ };
53
+ }
54
+
55
+ async function readSession(file) {
56
+ const turns = [];
57
+ const seen = new Set();
58
+ const rl = readline.createInterface({ input: fs.createReadStream(file, 'utf8'), crlfDelay: Infinity });
59
+ for await (const line of rl) {
60
+ if (!line.includes('"usage"')) continue;
61
+ const t = turnFromLine(line);
62
+ if (!t || Number.isNaN(t.at)) continue;
63
+ // A streamed reply can be logged more than once with the same message id.
64
+ if (t.id) {
65
+ if (seen.has(t.id)) continue;
66
+ seen.add(t.id);
67
+ }
68
+ turns.push(t);
69
+ }
70
+ turns.sort((a, b) => a.at - b.at);
71
+ let prev = null;
72
+ for (const t of turns) {
73
+ t.gap = prev === null ? null : (t.at - prev) / 1000;
74
+ prev = t.at;
75
+ }
76
+ return turns;
77
+ }
78
+
79
+ function summarize(turns, { since = null } = {}) {
80
+ const list = since ? turns.filter((t) => t.at >= since) : turns;
81
+ const total = { turns: list.length, write: 0, read: 0, input: 0, write1h: 0, write5m: 0 };
82
+ const buckets = BUCKETS.map((b) => ({ label: b.label, turns: 0, write: 0 }));
83
+ const firstTurns = { turns: 0, write: 0 };
84
+ const models = {};
85
+ let first = Infinity;
86
+ let last = -Infinity;
87
+ for (const t of list) {
88
+ total.write += t.write;
89
+ total.read += t.read;
90
+ total.input += t.input;
91
+ total.write1h += t.write1h;
92
+ total.write5m += t.write5m;
93
+ first = Math.min(first, t.at);
94
+ last = Math.max(last, t.at);
95
+ const mm = (models[t.model] = models[t.model] || { turns: 0, write: 0, read: 0 });
96
+ mm.turns += 1;
97
+ mm.write += t.write;
98
+ mm.read += t.read;
99
+ if (t.gap === null) {
100
+ firstTurns.turns += 1;
101
+ firstTurns.write += t.write;
102
+ continue;
103
+ }
104
+ const i = BUCKETS.findIndex((b) => t.gap >= b.lo && t.gap < b.hi);
105
+ buckets[i].turns += 1;
106
+ buckets[i].write += t.write;
107
+ }
108
+ return {
109
+ from: list.length ? new Date(first).toISOString() : null,
110
+ to: list.length ? new Date(last).toISOString() : null,
111
+ total,
112
+ firstTurns,
113
+ buckets,
114
+ models,
115
+ };
116
+ }
117
+
118
+ async function scan(dir = defaultDir(), opts = {}) {
119
+ if (!fs.existsSync(dir)) throw new Error(`No Claude Code sessions found at ${dir}`);
120
+ const files = listSessionFiles(dir);
121
+ const turns = [];
122
+ for (const f of files) turns.push(...(await readSession(f)));
123
+ return { dir, sessions: files.length, ...summarize(turns, opts) };
124
+ }
125
+
126
+ module.exports = { BUCKETS, defaultDir, listSessionFiles, turnFromLine, readSession, summarize, scan };