agentleFS
nacorganacorga/claude-code-metrics/CLAUDE.md

claude-code-metrics

CLAUDE.md0 starsChanged 8 months agoMITView on GitHub

Guidance for Claude Code (and contributors) working in this repo. See README.md for the user-facing docs.

The file

We split this CLAUDE.md into the rules it gives an agent

5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    JSONL-only: No SQLite, no embedded DB, no server, no dashboard.

    line 7
  2. Very specific:

    Tests are the only QA gate: No manual "I tested it" — write a unittest case.

    line 34
  3. Very specific:

    Installer patches settings.json via Python's json module: Never regex-replace JSON. Always preserve other hooks and unrelated keys.

    line 36
  4. Very specific:

    Preserve user data on uninstall by default: Only --purge-data may delete ~/.claude/metrics/ contents.

    line 37
  5. Very specific:

    Adding a model to pricing: add a prefix entry in config/pricing.json.

    line 54

≈ “Common” is an estimate: at least 1 in 5 of the 29,248 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

CLAUDE.md

Guidance for Claude Code (and contributors) working in this repo. See README.md for the user-facing docs.

Invariants (never violate without an ADR-level discussion)

  • JSONL-only. No SQLite, no embedded DB, no server, no dashboard. Output is plain JSON lines the user can grep, jq, pipe into a notebook. (One-shot derivative artefacts that read JSONL — markdown, CSV, static self-contained HTML — are fine; long-running processes and live dashboards are not.)
  • Zero runtime deps. Hook uses stdlib Python 3.9+. Installer uses bash + python3. If a feature needs a dep, reconsider the feature.
  • Always local. The hook must never make a network call. No telemetry, no remote reporting, no "opt-in analytics".
  • Never block Claude Code. The hook must always exit 0, even on crashes. Any failure logs to ~/.claude/metrics/hook.log (rotated to hook.log.1 at 1MB; one generation kept) and moves on. Log lines have the shape [<iso8601>] [<LEVEL>] <message>: use log() for informational events ([INFO]) and log_error() for real degradation ([ERROR]). The "recent hook errors" warning in /analyze-metrics and /metrics-report counts only [ERROR] lines (across hook.log and the rotated hook.log.1), so informational notices like _default pricing fallbacks never fire false positives.
  • Return to the shell in <100ms. Heavy work (transcript parsing) runs in a double-forked, detached grandchild. Large sessions (60MB+ transcripts) cannot stall session close. Set CCM_NO_FORK=1 for synchronous execution (tests).
  • Append-only. We never rewrite auto.jsonl / retro.jsonl. Migrations happen by bumping schema_version and letting old + new rows coexist.
  • Repo SKILL.md is ground truth, not the installed copy. ~/.claude/skills/{retrospective,analyze-metrics,metrics-report}/SKILL.md are install artefacts. Edits made directly there are lost on the next scripts/install.sh. Always edit the repo source and re-install.

Critical files

PathPurpose
hooks/session_end.pyCore hook. Parses transcript, estimates cost, appends JSONL row
config/pricing.jsonModel pricing table (prefix match + _default fallback)
skills/retrospective/SKILL.mdManual /retrospective — captures subjective metrics
skills/analyze-metrics/SKILL.mdManual /analyze-metrics — markdown report from local JSONL
skills/analyze-metrics/_helpers.pyShared filter / project-identity / health-signal helpers (also used by /metrics-report)
skills/analyze-metrics/_aggregate.pyShared aggregation logic — single source of truth for every cut both reports show. Imported by /analyze-metrics and /metrics-report to keep the markdown and HTML formats in lockstep.
skills/metrics-report/SKILL.mdManual /metrics-report — invokes _render.py
skills/metrics-report/_render.pyStatic HTML report generator. Stdlib-only, no JS, no CDN
scripts/install.sh / uninstall.shIdempotent; patch ~/.claude/settings.json via Python (json)
schema/{auto,retro}.schema.jsonJSON Schema docs. Source of truth for row shape
tests/Stdlib unittest. Zero external deps. Must stay green

Non-negotiable rules

  • Bump schema_version whenever you change the shape of a JSONL row. Update schema/*.schema.json in the same change. Add a migration note in the schema file.
  • Tests are the only QA gate. No manual "I tested it" — write a unittest case. All cases must pass: python3 -m unittest discover tests/ -v.
  • Cross-platform in hook + tests. fcntl restricts us to Unix (macOS + Linux). Windows is explicitly out of scope for v0.x. Never add code that silently breaks on macOS (e.g. GNU-only date flags).
  • Installer patches settings.json via Python's json module. Never regex-replace JSON. Always preserve other hooks and unrelated keys.
  • Preserve user data on uninstall by default. Only --purge-data may delete ~/.claude/metrics/ contents.
  • No fabricated metrics. The Claude Code transcript records usage only for the main agent; tokens consumed inside a subagent's own context window are not exposed. We measure subagent cost indirectly via the return surface (return_chars_total, duration_s_total, errors) — never by inventing per-subagent token figures.

Workflow

  • Before committing: python3 -m unittest discover tests/ -v (must be 90+ green), then bash -n scripts/*.sh.
  • Adding a metric: decide auto vs retro → update schema → update hook or skill → add test case → bump schema_version.

Schema history

VersionChange
v1Initial.
v2Dropped permission_mode (Claude Code does not send it in SessionEnd payload).
v3Added five conversation-shape signals: tool_errors_count, subagent_invocations, user_msg_count, short_user_followups_count, correction_keyword_hits. Old v1/v2 rows coexist; /analyze-metrics uses field in row guards to gracefully skip them in v3-only sections.
v4Added per-subagent return-surface aggregates: subagent_stats (count / return_chars_total / duration_s_total / errors / maxes per subagent_type) and cheap_subagent_calls (dispatches with <200-char tool_result). subagent_invocations kept additive for back-compat. v3 rows coexist; the skill guards on "subagent_stats" in row.
v5Added two project-identity fields: git_root (absolute path of the containing git working tree) and git_remote_origin (normalized origin URL, e.g. github.com/owner/repo). Captured by the hook via git rev-parse / git config --get remote.origin.url post-fork (timeout 2s, full fallback to empty strings on any failure). Old v3/v4 rows coexist transparently — _project_key in /analyze-metrics falls back to cwd when git fields are missing.
  • Adding a model to pricing: add a prefix entry in config/pricing.json. Prefix matcher picks longest-first, so more specific SKUs can be added without reordering.

Scope rules

If a proposed change would require a DB, a long-running process, or a network call — it does not belong here. "Web UI" specifically means a server, live dashboard, or anything that persists state across requests; a one-shot static HTML file the user opens locally is fine (see /metrics-report). Point the proposer at the roadmap section of the README when in doubt.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.