DEV Community

Cover image for Stop $500+ Runaway Claude Code Loops With a Zero-Dependency PreToolUse FinOps Circuit Breaker
Benni Alencar
Benni Alencar

Posted on Originally published at benni-os.net

Stop $500+ Runaway Claude Code Loops With a Zero-Dependency PreToolUse FinOps Circuit Breaker

By **Benni Alencar* — Chief AI Systems Architect at Benni OS. Canonical article & interactive benchmarks at benni-os.net.*

Why Soft Prompt Caps Fail Under Retry Spirals

Telling an autonomous coding agent "be concise and stay within budget" inside CLAUDE.md fails the moment a test suite enters an infinite fix-fail loop. Each retry re-sends the growing conversation history, turning a $2 task into a $500+ API bill overnight.

Benni-OS/claude-code-finops solves this outside the prompt layer with a zero-dependency Python gate bound to PreToolUse and PostToolUse.

How the FinOps Circuit Breaker Works

  1. Real-Time Token & USD Accounting: PostToolUse logs model usage (claude-opus-4, claude-sonnet-4, claude-haiku-4) into an append-only SHA-256 chained ledger (.claude/finops/finops_ledger.jsonl).
  2. Hard Dollar Ceiling: Before any tool executes, PreToolUse checks cumulative spend against --cap-usd. Once reached, exit code 2 blocks execution deterministically.
  3. Identical-Call Loop Detection: Each tool call is hashed into a signature. If the agent repeats the exact same failing call N times consecutively, the breaker halts the spiral immediately.
git clone https://github.com/Benni-OS/claude-code-finops.git
cd claude-code-finops
pip install -e .
python -m unittest discover -s tests -p "test_*.py" -v
Enter fullscreen mode Exit fullscreen mode
    Production Shortcut: Need pre-built enterprise security rules alongside FinOps caps? Grab the Sovereign Policy & Red-Team Pro Pack ($49) or book a 48h team security & FinOps audit via bennialencar@benni-os.net.
Enter fullscreen mode Exit fullscreen mode




Launch the Benni Master OS v10 Sandbox

Verify 38 canonical skills, 8 deterministic packs, and Reality Lock invariants live in your browser, or contact bennialencar@gmail.com for B2B pilots.

    Open Live Sandbox →
Enter fullscreen mode Exit fullscreen mode




Frequently Asked Questions


      Does claude-code-finops require external databases or cloud proxies?
No. It runs on pure Python standard library with an append-only SHA-256 chained JSONL ledger in under 1 millisecond.
  Can teams combine claude-code-finops with security guardrails?
  Yes. You can pair it with Benni-OS/claude-code-sovereign or drop in the Sovereign Policy & Red-Team Pro Pack for OWASP LLM and infrastructure protection.
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Frequently Asked Questions

Does claude-code-finops require external databases or cloud proxies?

No. It runs on pure Python standard library with an append-only SHA-256 chained JSONL ledger in under 1 millisecond.

Can teams combine claude-code-finops with security guardrails?

Yes. You can pair it with Benni-OS/claude-code-sovereign or drop in the Sovereign Policy & Red-Team Pro Pack for OWASP LLM and infrastructure protection.


Production Packs, B2B Pilots & Open-Source Lab Support

All core tooling in the Benni-OS Claude Code suite remains 100% open source (MIT). To fund ongoing GPU benchmarks and keep these tools free for the community, we offer:

  1. Live Reality Lock Sandbox (Free): Test deterministic verification invariants across 8 engineering packs at benni-master-os.bennialencar.workers.dev.
  2. Sovereign Policy & Red-Team Pro Pack ($49): Drop-in PreToolUse guardrails & OWASP LLM probes at bennie6.gumroad.com/l/monxqo.
  3. Production Hardening & Custom Domain Packs (B2B): Enterprise policy packs, CI/CD Reality Lock gates, and 30-minute pilot reviews via bennialencar@benni-os.net (Architecture Portfolio & Services).
  4. Sponsor the Open-Source Research Lab: Found this architecture useful in production? Support reproducible benchmarks and new MIT releases via PayPal (alencaroyale@gmail.com) or star/follow on GitHub Benni-OS & BenniAlencar.

Top comments (0)