Original Reddit post

If you run multi-agent workflows on Claude Pro/Max you’ve hit this: an agent starts a task, burns through your remaining quota, and leaves your codebase half-built. I built a gate system that fixes this using Claude Code’s PreToolUse and SubagentStop hooks: How it works: Before any subagent spawns → hook fires → estimates task cost → checks ledger → blocks if you’d breach the safety floor After agent finishes → hook fires → reads real token usage from the session transcript → writes it back to the ledger No API key needed. No external service. Pure Python reading a local JSON file. Fixes shipped after a code review:

  • Atomic writes (no corrupt JSON on crash)
  • File locking (no lost updates from concurrent agents)
  • file-anchored paths (no more “different hooks reading different ledgers” bug)
  • Window reset no longer re-counts old transcript tokens Tested on: Claude Code v2.1.148, macOS M2, Claude Pro, Python 3.x GitHub: https://github.com/InsaneCoder-69/claude-code-budget-gate Setup is literally 3 lines per session: cd your-project export CLAUDE_PROJECT_DIR=$(pwd) claude submitted by /u/Technical_Wash_2626

Originally posted by u/Technical_Wash_2626 on r/ClaudeCode