Agent Guardrails & Monitoring · Guide
How do I stop Claude Code runaway costs and infinite retry loops?
Unattended Claude Code sessions rarely blow up because of one huge prompt—they burn $50 to $180 overnight by repeating the same failing Bash, test, or build command across a growing 150k-token context window. A deterministic local PreToolUse circuit breaker stops consecutive failures, turn calls, and elapsed minutes in under 2 ms before the next expensive API call happens.
By Gorden Wübbe · Updated 2026-11-25

The math behind a $120 overnight Claude Code bill
Every time an autonomous coding agent retries a failing migration, flaky test, or missing environment variable, the conversation context grows larger. Even with prompt caching, each tool round-trip re-reads tens of thousands of tokens while generating fresh reasoning and edits.
When a session enters a retry spiral—fixing error A, introducing error B, reverting to error A—40 to 120 turns can execute while you sleep. Worse, a soft instruction inside CLAUDE.md like “stop if tests fail 3 times” is probabilistic: the model frequently rationalizes “one more try” and keeps spending.
Cost impact: unprotected loop vs. local circuit breaker
Illustrative comparison based on typical long-context agentic sessions (100k–160k input context + output tokens per retry cycle; exact API pricing varies by model tier and cache hit rate):
| Scenario | Tool calls before stop | Typical duration | Estimated API burn | Outcome |
|---|---|---|---|---|
| Unprotected overnight loop | 120–300+ calls | 2–6 hours | $45 – $180+ | Broken branch, exhausted quota, wake up to surprise bill |
| Prompt instruction only (“stop after 3 fails”) | 15–60 calls (unreliable) | 20–90 min | $8 – $35 | Model often ignores rule when it thinks a fix is close |
Hard timeout only (timeout 2h) | 80–180 calls | 120 min | $30 – $95 | Kills healthy slow builds OR still burns 2 full hours |
Local PreToolUse circuit breaker (guard) | 5 consecutive fails / 80 cap | 1–4 min on loop | < $1.50 – $3.00 | Instant local block (<2 ms) + Telegram/Slack alert |
Do not rely on billing alerts from your model provider—provider usage dashboards lag by minutes to hours. Only a synchronous local PreToolUse hook can intercept the 6th failing tool call before the API request is even sent.
Four deterministic guardrails every autonomous agent needs
1. Consecutive failure cap (`--max-failures 5`): Resets to zero on any successful tool call or new human prompt, so productive sessions run uninterrupted while broken retry loops halt immediately.
2. Per-turn tool call cap (`--max-calls 80`): Prevents agents from endlessly grepping, rewriting, and churning through hundreds of tool calls in a single autonomous turn.
3. Wall-clock turn budget (`--max-minutes 45`): Stops slow-motion stalls where a session dribbles one tool call every few minutes all night.
4. Destructive shell fuse (`--block-destructive on`): Inspects shell commands locally before execution and blocks catastrophic patterns (rm -rf /, force pushes, database drops) without logging or uploading the command content.
Configure the local circuit breaker in 20 seconds
Works identically across Claude Code, Cursor, Gemini CLI, and Antigravity, completely locally with zero cloud account required:
# Install CLI (SHA-256 verified) & run local cost/safety audit curl -fsSL https://agenttracking.co/install.sh | sh -s -- audit # Enable local spend & safety circuit breaker agenttracking guard \ --max-failures 5 \ --max-calls 80 \ --max-minutes 45 \ --block-destructive on # Need an emergency stop right now on this machine? agenttracking pause --reason "investigating loop" agenttracking resume
Combine local caps with instant mobile alerts
Stopping a loop locally saves your budget; getting notified lets you unblock the task in two minutes. Connect the machine with agenttracking connect all to receive instant Telegram, Slack, or Discord alerts on the Free plan, or try the live interactive demo to trigger a simulated 5× loop block right in your browser.
Frequently asked questions
Does `agenttracking guard` slow down normal Claude Code sessions?
No. The local PreToolUse check reads a tiny local state file and returns in under 2 milliseconds with zero network calls.
Does the circuit breaker reset automatically when I reply to Claude?
Yes. Starting a new prompt resets the turn call count, turn timer, and consecutive failure streak automatically.
Will `guard` stop a legitimate 20-minute build or refactor?
Not if tool calls succeed. --max-failures 5 only trips on consecutive failures, and you can tune --max-calls and --max-minutes to match your repository's normal workflow.
Does Agent Tracking read my API billing keys or prompts to cap cost?
No. It caps runaway loops deterministically using local tool failure counts, call budgets, and elapsed turn time—zero prompts or API keys are ever read or uploaded.
Primary sources & documentation
Read next & try live
How do Claude Code hooks work for alerts, stuck sessions and guardrails?How do I safely monitor Claude Code cron jobs and overnight headless agents?How do I audit MCP server security and detect config drift across my team?Try interactive fleet demo (no login) →Start free (incl. Telegram/Slack) →