r/ClaudeCode • u/Strange-Sea-9894 Workflow Engineer • 3h 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 0
Curious 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 3h 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.
1
u/2bluenucleus 1h ago
Two gaps worth closing in that hook:
cat package-lock.jsonorhead -c 200000 dist/app.jsthrough Bash, and that output lands in context exactly the same way. Add a Bash branch that readstool_input.commandand blocks cat/head/tail/less on the same patterns.wc -lsays 1 and the read goes straight through. Check bytes as well:wc -c < "$FP"works on both macOS and Linux, and around 60 KB is a reasonable cutoff for a read with no limit.Smaller one: if the matcher in settings.json is empty or
*, the script starts on every tool call. Setting it toRead|Bashskips the rest, and parsing the payload once (one python3 call that prints all three fields, or jq) saves starting Python three times per call.1
u/AgentYurt 49m ago
The one that moved the needle most for me was the resume side of #3. Instead of resuming a long session, I keep a short handoff note — decisions made, next step, and what I already ruled out — and start fresh around turn 25. The new session reads about 30 lines instead of re-billing the whole history, and the cache expiry stops mattering because there's barely anything to re-cache.
On the MCP side I only connect the servers the current task actually needs, and I switch that at session start rather than keeping all of them on. Six idle servers is a lot of tokens per turn for tools you'll never call.
And the cheap version of your read guard: when I do need to re-check something, I re-read one row or one small slice instead of the whole file. Re-checking one row is way cheaper than re-billing the whole thing.