Skip to main content

Cost Monitor Bug: When set -e Silenced the Dashboard

A Claude Code monitoring script reported $234 instead of $48 before set -euo pipefail caused the status bar to go blank for a week.

AI-written
Inewgen
22 Aug 2026Source: Dev.to2 min read (0 views)Last updated 20 Sep 2026
Share
Cost Monitor Bug: When set -e Silenced the Dashboard

Stock photo for illustration only, not from the actual event

Font size
  • A monitoring script previously overcounted spend by 4-6x due to double-summing cumulative log lines.
  • Using set -euo pipefail caused the entire script to exit on timeouts, blanking out the status bar.
  • The new design choice uses fail-open, prints ⚫ n/a, and exits with 0 to prevent total silence.
  • The 212-line script combines Bash and Python3 to manage AI autonomous environment costs.

A monitoring script that dies quietly is worse than no monitoring at all. The author's setup proved this twice: first when it reported a 7-day spend of 234 US dollars when the real figure from ccusage daily was 48 US dollars—a 4 to 6x overcount caused by double-summing cumulative log lines, which was later narrowed down to a 2.3% source-to-source gap.

bash code terminal window

Stock photo for illustration only, not from the actual event

laptop computer programming workspace

Stock photo for illustration only, not from the actual event

The second failure occurred after hardening the script with set -euo pipefail. A single network timeout from ccusage caused the entire launchd job to terminate with an exit code of 1, leaving the status bar completely blank for a full week without the developer noticing that two 5-hour blocks had crossed critical thresholds.

$3,000Weekly cost threshold
212Lines of script code

The fix for both issues came down to the exact same architectural decision: fail open, print ⚫ n/a, and exit 0. This post walks through the 212-line script that powers the author's cost monitoring today and outlines the core design principle that the dashboard must never go silent.

Never miss the latest news?

Subscribe to get news summaries by email - not often enough to be annoying.

โฆษณา

"The property you need from a monitoring script isn't accuracy. It's never going quiet."

Dev.to

In autonomous AI development environments utilizing tools like Claude Code, API expenditures can escalate rapidly if left unchecked. Implementing a fail-open monitoring strategy ensures that missing data or system errors are immediately distinguishable from normal operations, preventing silent failures from accumulating massive financial losses.

The core of the system relies on token-budget-advisor.sh, a 212-line Bash script that internally calls Python3. It pulls official metrics from ccusage blocks --json first, falls back to local JSON logs, and aggregates data through independent layers designed never to let the user interface drop into total silence.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article