claude-code-cost-monitor
CLAUDE.md1 starsChanged 8 months agoMITView on GitHub
Zero-dependency CLI tool for tracking Claude Code API costs via OpenTelemetry. Single Python script (cost-monitor.py) + Docker Compose stack (OTel Collector, Prometheus, Jaeger). Tests use mock HTTP servers and patched functions — no live backends needed. Types: feat, fix, docs, chore, refactor, test Add an entry to _MODEL_PRICING in cost-monitor.py. The list uses longest-substring matching, so order doesn't matter but more specific patterns should be listed. Edit docker-compose.yml: - Prometheus: --storage.tsdb.retention.time=60d - Jaeger: BADGER_SPAN_STORE_TTL: "1440h" (hours) Edit the _fetch_all_traces function's…
In this repositoryOne other file it gives its agents.
- cost-monitorSkill
The file
We split this CLAUDE.md into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 39
All ports on 127.0.0.1 — do NOT bind to 0.0.0.0
- Distinctive: line 40
No prompts or code content in telemetry — privacy-first
- Very specific: line 66
Add an entry to _MODEL_PRICING in cost-monitor.py.
≈ “Common” is an estimate: at least 1 in 5 of the 28,029 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
claude-code-cost-monitor — Agent Instructions
Project overview
Zero-dependency CLI tool for tracking Claude Code API costs via OpenTelemetry.
Single Python script (cost-monitor.py) + Docker Compose stack (OTel Collector,
Prometheus, Jaeger).
Architecture
cost-monitor.py # all logic — 3 commands: cost, turns, status
docker-compose.yml # 3-service stack, loopback-only, named volumes
otel-collector-config.yaml # OTLP/HTTP receiver → Jaeger + Prometheus
prometheus.yml # scrape collector:8889
setup.sh # env vars for Claude Code telemetry
tests/test_cost_monitor.py # 42 pytest testsData flow
- Claude Code → OTLP/HTTP (:4318) → OTel Collector
- Collector → Jaeger (:16686, traces) + Prometheus (:9090, metrics)
costcommand → PromQLlast_over_time()query → per-session tableturnscommand → Jaeger/api/traces→ per-turn tool breakdownstatuscommand → HTTP probes to all 3 endpoints
Key design decisions
- Prometheus for costs (not Jaeger) — single HTTP call, instant, no pagination bugs
- Jaeger for turns — trace spans carry per-tool detail that metrics don't
- Auto-retry on Jaeger NaN bug — retries with smaller limits (50→20→10→5→2→1)
- Auto-detect metric namespace — works with or without collector namespace prefix
- Zero dependencies — stdlib
urllib+jsononly, no pip packages
Constraints
- Python 3.8+ stdlib only — do NOT add external dependencies
- All ports on
127.0.0.1— do NOT bind to0.0.0.0 - No prompts or code content in telemetry — privacy-first
Running tests
python3 -m pytest -q tests/Tests use mock HTTP servers and patched functions — no live backends needed.
Commit convention
<type>(<scope>): <short description>
[optional body]
Developed by: thewizardTypes: feat, fix, docs, chore, refactor, test
Common tasks
Add a new Claude model
Add an entry to _MODEL_PRICING in cost-monitor.py. The list uses longest-substring
matching, so order doesn't matter but more specific patterns should be listed.
Change retention
Edit docker-compose.yml:
- Prometheus:
--storage.tsdb.retention.time=60d - Jaeger:
BADGER_SPAN_STORE_TTL: "1440h"(hours)
Adjust Jaeger retry limits
Edit the _fetch_all_traces function's retry list:
for limit in [JAEGER_SAFE_LIMIT, 20, 10, 5, 2, 1]:Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

