session-health
95% of my Claude Code tokens were cache re-reads — not output. This plugin
detects when a session enters that token-wasting phase and, unlike
monitoring-only tools, closes the loop: it tells the model itself to propose
/compact and delegate to subagents, and surfaces "time to cut this session"
in your statusline and notifications.
日本語版は README.ja.md へ。
At a glance
- Measured: one full day of real work (2026-07-02, 12 projects, 2,592 deduplicated requests) — 95% of all tokens were cache reads, output was 0.4%
- Root cause: session length — worst sessions ran at a 231–313x cacheRead/output ratio
- Mechanism: threshold detection →
additionalContext injection → the model itself starts proposing /compact and delegating
- Status: the closed loop is live; within-session,
/compact cuts live context a median 66% (29 real compactions, monotonic with pre-compaction size). Reproduce with scripts/compaction_effect.py. Across days, a workload-normalized 4-day series shows the per-session median cacheRead/output ratio falling monotonically 233x → 83x after the loop went live — while the raw cache-read share (~94%) and the hot-session rate did not improve (details).
The problem
Prompt caching in Claude Code is automatic and cheap per token — but every
request re-reads the entire cached context. Run one session for a few hours
and the numbers look like this (one full day of real work: 2026-07-02,
12 projects, 2,592 deduplicated requests):
input 1.5M
output 1.6M
cacheRead 347M <- 95% of all tokens
The worst sessions ran at a 231–313x cacheRead/output ratio. A statusline
warning alone didn't change behavior (mine was ignored past 2x the threshold).
What worked was making the model aware: once the session crosses a
threshold, every prompt carries an injected instruction to wrap up, compact,
and delegate.
What it does
One detection engine (scripts/session_health.py), three surfaces:
| Surface | Mechanism | What you see |
|---|
| Model self-correction | UserPromptSubmit hook (installed automatically) | When hot, the model starts proposing /compact / a fresh session at task boundaries and delegates exploration to subagents |
| Statusline | optional, see below | 📜req56·120x → 🟡 → 🔥req231·246x ⚠️/compact |
| Stop notification | optional, see below | your "response done" notification gains | cut point: req231 / re-read 246x -> /compact |
Plus /session-health:usage-report: a token breakdown of the last day across
project × session × subagent × model, with request-level deduplication
(streaming writes each request as multiple JSONL records — naive summing
double-counts 2–3x) and subagent transcripts included (they live in separate
files under <session>/subagents/; miss them and delegation looks like zero).
Everything runs on local transcript files. Nothing leaves your machine.
Performance notes
Full usage reports scan local Claude Code transcript files under
~/.claude/projects/, including subagent transcripts, and can take time on
large histories. For faster checks, narrow the scan:
python3 scripts/usage_report.py --project agent-company-os
python3 scripts/usage_report.py --current ~/.claude/projects/.../session.jsonl
python3 scripts/usage_report.py --transcript ~/.claude/projects/.../session.jsonl
Future work may add an incremental cache for repeated full reports.
Security / What this installs
Claude Code plugins are powerful — they can add commands, hooks, agents, and
MCP servers, so being wary of a plugin from a personal repository is the
correct instinct. Here is exactly what this one adds.
This plugin installs:
- one slash command:
/session-health:usage-report
- one
UserPromptSubmit hook that runs scripts/session_health.py hook
It does not install:
- MCP servers
- external integrations
- background daemons
- network calls
- dependencies outside the Python standard library
All analysis is performed on local Claude Code transcript files under
~/.claude/projects/. Nothing leaves your machine.
If you'd rather verify than trust: read hooks/hooks.json
first — it declares exactly when Claude Code runs what — then the two scripts
under scripts/ and commands/usage-report.md.
Install
/plugin marketplace add House-lovers7/claude-code-session-health
/plugin install session-health@house-lovers7
The hook and /session-health:usage-report work immediately. Two optional surfaces:
Statusline — see scripts/statusline-example.sh,
or add one line to your existing statusline script:
health=$(printf '%s' "$input" | python3 "<plugin>/scripts/session_health.py" statusline 2>/dev/null)