{"slug": "how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still", "title": "How claude-spend-guard stops runaway Claude Code sessions — and where it can still fail", "summary": "A developer released claude-spend-guard, an MIT-licensed npm wrapper that reads Claude Code's stream-json output, parses the total_cost_usd field, and kills the process group with SIGTERM then SIGKILL once cumulative cost crosses a user-set cap. The tool has no runtime dependencies, makes no network calls, and documents three visible states plus fallback modes for other CLIs that lack a confirmed cost signal. The developer notes graceful shutdown is weaker on platforms where SIGTERM handling differs.", "body_md": "Claude Code reports its own running cost. When run with `--output-format stream-json`, it emits a `total_cost_usd` value that `claude-spend-guard` can monitor. The tool reads Claude Code's stdout, parses that field, and compares it with a cap you set.\n\n```\nnpx claude-spend-guard --cap 5 -- claude -p \"refactor the auth module\"\n```\n\nWhen reported cumulative cost crosses the cap, the wrapper sends `SIGTERM` to the process group, waits for a grace period, then sends `SIGKILL` to anything still running. There is no dashboard, account, or server: it is a local process wrapper.\n\nUsage dashboards such as `claude-spend` and `claude-usage` help you review spending after the fact. SDK middleware such as `llm-spend-guard` and `budget-guard` applies when your own code calls a provider API. I wanted a guard for the `claude` CLI process itself, including unattended terminal runs.\n\nThat scope is why the confirmed dollar-cap enforcement claim is for Claude Code. Other CLIs do not expose the same confirmed cost signal in their output, so their support uses weaker fallback modes instead of pretending the same guarantee applies.\n\nA tool meant to stop runaway spend has to be clear when it cannot do that job. It has three visible states:\n\nThe failure mode I wanted to avoid was a tool that stops enforcing the cap but still claims it is protecting the run.\n\nThe package has no runtime dependencies. Its test suite stubs Node networking primitives and checks that the wrapper does not make network calls; a dependency blocklist also guards against adding common telemetry packages later.\n\n`SIGTERM`, so graceful shutdown is weaker there.\nThose limitations are documented in the README.\n\n```\nnpx claude-spend-guard --cap 5 -- claude -p \"your prompt\"\n```\n\nThe tool is MIT licensed and runs locally. If you run Claude Code unattended, I’d like to hear which edge cases you would test first.\n\nnpm: [https://www.npmjs.com/package/claude-spend-guard](https://www.npmjs.com/package/claude-spend-guard)\n\nGitHub: [https://github.com/toxot-net/claude-spend-guard](https://github.com/toxot-net/claude-spend-guard)", "url": "https://wpnews.pro/news/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still", "canonical_source": "https://dev.to/oscar_lansh_net/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still-fail-53od", "published_at": "2026-11-01 18:20:17+00:00", "updated_at": "2026-11-01 18:22:34.563998+00:00", "lang": "en", "topics": ["ai-tools", "ai-agents", "developer-tools", "ai-products"], "entities": ["claude-spend-guard", "Claude Code", "Anthropic", "npm", "GitHub", "claude-spend", "claude-usage", "llm-spend-guard"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still", "markdown": "https://wpnews.pro/news/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still.md", "text": "https://wpnews.pro/news/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still.txt", "jsonld": "https://wpnews.pro/news/how-claude-spend-guard-stops-runaway-claude-code-sessions-and-where-it-can-still.jsonld"}}