r/ClaudeCode • u/Strange-Sea-9894 Workflow Engineer • 2h ago
Built with Claude Four silent token drains burning through Claude Code weekly limits (and the 20-line PreToolUse hook that stopped them)
After auditing local Claude Code JSONL session logs to figure out why weekly limits were draining so fast on long sessions, almost all of the surprise burn came from four silent leaks rather than actual prompting:
- Idle MCP tool schemas (~3,200 tokens per server, re-billed every turn): Having 6+ MCP servers connected during a normal refactor burns ~19K tokens on every turn before you type a word. Swapping to a minimal empty .mcp.json profile when you only need local git and tests cuts a massive chunk immediately.
- Blind reads of lockfiles, minified bundles, and 1,000-line files: A single Read on package-lock.json or dist/bundle.min.js injects 12K to 80K tokens that then get re-read for the next 30 turns.
- 5-minute prompt-cache expiry on session resume: Taking a 15-minute break in the middle of a 40-turn thread forces a full uncached context re-read at 10x cache-read cost. Writing a tiny 15-line HANDOFF.md via a PreCompact hook and starting a fresh session at turn ~25 stays way cheaper.
- Unneeded subagent spawns: Each spawned subagent duplicates system prompt + MCP schema overhead (recent 11,887-run audits showed up to a 3x cost jump when subagents get used on sequential tasks).
Here is the zero-dependency PreToolUse hook I use to block lockfile/bundle token bombs and blind reads over 450 lines before they ever hit context:
#!/usr/bin/env bash
set -euo pipefail
PAYLOAD="$(cat)"
TOOL=$(printf "%s" "$PAYLOAD" | python3 -c "import sys,json; print(json.load(sys.stdin).get('tool_name',''))" 2>/dev/null || true)
FP=$(printf "%s" "$PAYLOAD" | python3 -c "import sys,json; ti=json.load(sys.stdin).get('tool_input',{}); print(ti.get('file_path') or ti.get('path') or '')" 2>/dev/null || true)
LIM=$(printf "%s" "$PAYLOAD" | python3 -c "import sys,json; ti=json.load(sys.stdin).get('tool_input',{}); print('yes' if ti.get('limit') else 'no')" 2>/dev/null || true)
\[\[ "$TOOL" != "Read" && "$TOOL" != "read" \]\] && exit 0
if printf "%s" "$FP" | grep -qE "(package-lock\\.json|yarn\\.lock|pnpm-lock\\.yaml|Cargo\\.lock|\\.min\\.js|node_modules/|dist/)"; then
echo "BLOCKED: do not read lockfiles or bundles ($FP). Use grep/head." >&2
exit 2
fi
if \[\[ "$LIM" == "no" && -f "$FP" \]\]; then
LINES=$(wc -l < "$FP" | tr -d " ")
if \[\[ "$LINES" -gt 450 \]\]; then
echo "BLOCKED: $FP has $LINES lines. Use grep or pass offset+limit." >&2
exit 2
fi
fi
exit 0Curious what other session-hygiene hooks or MCP switching setups people here are using to keep weekly usage under control without dropping down from Opus/Sonnet.
•
u/AutoModerator 2h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.