ai-cost-audit 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.
Files changed (3) hide show
  1. package/README.md +177 -0
  2. package/dist/cli.js +1286 -0
  3. package/package.json +50 -0
package/README.md ADDED
@@ -0,0 +1,177 @@
1
+ # ai-cost-audit
2
+
3
+ **Know what every AI coding request costs before your developers send it.**
4
+
5
+ `ai-cost-audit` is a context cost **profiler and linter** for AI coding tools. It scans a
6
+ repository, finds everything that gets loaded into a model's context before a developer
7
+ types anything meaningful — `CLAUDE.md`, agent and skill definitions, MCP server configs,
8
+ Cursor rules, Copilot instructions, referenced docs — and tells you:
9
+
10
+ - how large your **guaranteed baseline** is (tokens loaded on *every* request),
11
+ - what it **costs per request and per day**, with and without prompt caching,
12
+ - how long your **monthly budget** actually lasts,
13
+ - where the **waste** is (duplicated guidance, oversized files, unbounded MCP configs),
14
+ - and it **fails CI** when the baseline exceeds your budget or grows too fast.
15
+
16
+ Everything runs **offline and deterministically** — no AI calls, no API key, no network.
17
+
18
+ ```bash
19
+ npx ai-cost-audit scan
20
+ ```
21
+
22
+ ## Example output
23
+
24
+ ```text
25
+ ## Guaranteed Context (loaded on every request)
26
+
27
+ | Source kind | Tokens |
28
+ |-----------------------------------|-------:|
29
+ | Repository instructions | 9,140 |
30
+ | Agent definitions | 12,870 |
31
+ | MCP configuration | 31,260 |
32
+ | Referenced documentation | 7,890 |
33
+ | **Repo baseline (CI-gated)** | **61,160** |
34
+ | Global user files (not gated) | 18,420 |
35
+ | **Total baseline** | **79,580** |
36
+
37
+ ## Estimated cost per request (baseline input only)
38
+
39
+ | Model | Uncached | With caching (typical) |
40
+ |-----------------|---------:|-----------------------:|
41
+ | claude-opus-4-8 | $0.40 | $0.09 |
42
+
43
+ At 200 requests/day per developer (3 developers), your $100/month budget lasts ~1.9 days.
44
+
45
+ ## High-impact findings
46
+
47
+ 1. [warn] duplicate-content — 3,400 redundant tokens: identical content in CLAUDE.md, docs/standards.md
48
+ 2. [warn] near-duplicate-content — ~2,100 redundant tokens: nearly identical content in 4 agent files
49
+ 3. [warn] large-source — CLAUDE.md alone is 31% of the baseline
50
+ 4. [info] unmeasured-mcp — 14 MCP server(s) counted by config size only
51
+ ```
52
+
53
+ ## What it scans
54
+
55
+ | Adapter | Sources | Classification |
56
+ |---|---|---|
57
+ | **claude-code** | `CLAUDE.md`, `CLAUDE.local.md`, `~/.claude/CLAUDE.md`, `.claude/agents/`, `.claude/skills/`, `.claude/commands/`, files pulled in via `@imports` and local links | Skills are split: descriptions load every session (**guaranteed**), bodies load on demand (**conditional**) |
58
+ | **generic-mcp** | `.mcp.json`, `mcp.json` | Configured server JSON counted; live tool schemas flagged as unmeasured (see Honesty below) |
59
+ | **instructions** | `AGENTS.md`, `.cursor/rules/*.mdc`, `.cursorrules` (legacy), `.github/copilot-instructions.md`, `.github/**/*.instructions.md` | By tool convention |
60
+
61
+ Sources are bucketed three ways, and the report never pretends otherwise:
62
+
63
+ - **guaranteed** — loads on every request; measured from files.
64
+ - **conditional** — loads for some tasks (skills, commands, path-scoped rules); measured, reported separately.
65
+ - **variable** — conversation history, task files; **shown as configurable ranges, never point estimates**.
66
+
67
+ ## CI budget gate
68
+
69
+ ```bash
70
+ # One-time: write and commit the snapshot (lockfile pattern)
71
+ npx ai-cost-audit scan --update-snapshot
72
+ git add .ai-cost-audit/snapshot.json
73
+
74
+ # In CI:
75
+ npx ai-cost-audit scan --ci
76
+ ```
77
+
78
+ The gate compares the **repo-scoped** baseline (global user files are reported but
79
+ excluded, so local and CI runs agree) against `baselineTokenLimit` and against the
80
+ committed snapshot's growth threshold. On failure it names the cause:
81
+
82
+ ```text
83
+ AI context budget failed: baseline grew 72% (threshold 20%).
84
+ Previous: 24,310 tokens
85
+ Current: 41,822 tokens
86
+ Change: +72%
87
+ Primary cause: Added backend-api-agent.md (+14,280 tokens)
88
+ ```
89
+
90
+ Exit codes: `0` pass, `1` budget/growth violation, `2` execution error.
91
+
92
+ ## Configuration
93
+
94
+ Drop an `ai-cost-audit.json` in your repo root (all fields optional — zero-config works):
95
+
96
+ ```json
97
+ {
98
+ "providers": ["anthropic"],
99
+ "models": ["claude-opus-4-8", "claude-sonnet-5"],
100
+ "monthlyBudget": 100,
101
+ "developers": 3,
102
+ "baselineTokenLimit": 30000,
103
+ "growthThresholdPct": 20,
104
+ "requestsPerDay": [50, 200, 1000],
105
+ "cache": { "enabled": true, "requestsPerSession": 10 },
106
+ "variable": {
107
+ "conversationHistory": [8000, 25000],
108
+ "taskFiles": [5000, 15000]
109
+ },
110
+ "mcp": { "knownSchemaTokens": { "github": 12000 } },
111
+ "pricingOverrides": { "gpt": { "inputPerMTok": 2.5 } }
112
+ }
113
+ ```
114
+
115
+ ## CLI
116
+
117
+ ```text
118
+ ai-cost-audit scan [path]
119
+ -c, --config <file> config file (default: ./ai-cost-audit.json)
120
+ -f, --format <format> md | json | html (default: md)
121
+ -o, --out <file> write report to a file
122
+ --ci run the budget gate (exit 1 on violation)
123
+ --update-snapshot write .ai-cost-audit/snapshot.json
124
+ --no-global skip user-global files (~/.claude)
125
+ --ref-depth <n> levels of @imports/links to follow (default: 3)
126
+ ```
127
+
128
+ ## The cost model (and why caching matters)
129
+
130
+ A naive `tokens × price × requests` estimate overstates real spend by up to ~10×,
131
+ because baseline context is exactly the part that prompt caching serves cheaply.
132
+ The report shows both figures:
133
+
134
+ - **Uncached:** `baseline_tokens × input_price` per request.
135
+ - **With caching (typical):** the first request of a session pays the cache-write
136
+ multiplier (1.25× for Anthropic's 5-minute TTL), subsequent requests pay the read
137
+ multiplier (0.1×). With `requestsPerSession = n`:
138
+ `effective = (write + read × (n−1)) / n` — about **0.215×** at n=10.
139
+
140
+ The formula is printed in every report so the math is auditable. Baseline cost is
141
+ input-side only; output tokens depend on what the model generates and are out of scope.
142
+
143
+ ## Honesty (read this)
144
+
145
+ - **Token counts are offline estimates.** There is no public offline tokenizer for
146
+ Claude models. We count with `o200k_base` (exact for GPT models) and apply a
147
+ disclosed per-provider calibration factor (anthropic ×1.2 by default, configurable).
148
+ Expect ±~15–20% vs Anthropic's exact counts. No number in the report is presented
149
+ as exact for Claude.
150
+ - **MCP schemas are usually the biggest unknown.** The tokens that actually enter
151
+ context are each server's live tool schemas, which require a running server to
152
+ measure. We count the configured JSON, flag every server `confidence: low`, and let
153
+ you pin measured sizes via `mcp.knownSchemaTokens`. The real number is usually
154
+ *much* larger — treat MCP lines as a floor, not a measurement.
155
+ - **Variable context is shown as ranges** you configure, never fake-precise numbers.
156
+ - **Pricing is date-stamped.** If the built-in table is more than 90 days old, the
157
+ report warns you to verify and override via `pricingOverrides`.
158
+
159
+ ## Roadmap
160
+
161
+ - `--accurate` flag: exact Anthropic counts via the `count_tokens` API (opt-in, needs a key)
162
+ - Bundled catalog of measured schema sizes for popular MCP servers
163
+ - Per-task scenario modeling ("Fix Angular component → 97k–114k tokens")
164
+ - Predicted-vs-actual reconciliation against provider usage APIs
165
+ - Opt-in AI pass: semantic duplication, condensed-instruction suggestions, executive summary
166
+ - Finding suppression (`--ignore <rule>`), historical trend charts
167
+
168
+ ## Development
169
+
170
+ ```bash
171
+ npm install
172
+ npm test # vitest
173
+ npm run build # tsup -> dist/cli.js
174
+ node dist/cli.js scan test/fixtures/sample-repo
175
+ ```
176
+
177
+ MIT