Claude Code 帳單拆解:39 次失敗循環與 $45.61 超額費用
A 39-run failure loop and $45.61 of overage: taking apart one Claude Code bill
一宗 Claude Code 帳單案例中,定時自動化工作每 5 至 10 分鐘執行,耗盡 361.6M tokens 的週額度,並產生 $45.61 超額費用。
On 16 November 2026 a developer, hirobaw, opened issue #110395 on the anthropics/claude-code repo. The title says most of it: Claude Code built recurring AI jobs with no cost estimate and no spending cap, and the user's weekly allowance plus USD 45.61 of overage went with them.
$45.61 is not a scary number. What makes the issue worth reading is that every step that led to it is ordinary. Nobody did anything reckless. An agent was asked to set up some automations, it did, and the bill arrived later.
This post walks through the numbers in the issue, shows which part of them nobody could have seen from a usage page, and ends with a check you can run on your own logs in about a minute.
What the issue says happened
Everything below is from the issue body (link at the end). In October 2026, Claude Code, running Opus in the Claude desktop app, set up recurring automations on a third-party agent service. The jobs were simple condition checks, things like "new mail?" or "is the process running?". Each one was built as a full AI execution, run every 5 to 10 minutes.
The issue gives four numbers:
- About 580 AI runs a day.
- About 40,000 tokens per run.
- One failure loop that ran 39 times in a row.
- A weekly allowance of 361.6M tokens used up in 3.83 days, followed by USD 45.61 of paid overage.
The issue also notes that the jobs had no run-count or cost estimate, no check against the provider's weekly limit, no spending cap and no brake on repeated failures.
The numbers don't add up, and that's the interesting part
Do the arithmetic on the baseline the issue gives:
580 runs × 40,000 tokens = 23.2M tokens a day.
At that rate, 361.6M tokens would last about 15.6 days. It lasted 3.83 days, which works out to roughly 94M tokens a day, about four times the stated baseline.
The issue doesn't break down where the other three quarters went, so I won't guess. But it tells you something useful about how these bills happen. The designed load, the one you could estimate up front, explains a quarter of the spend. The rest is behaviour: retries, failures, runs that took longer than planned. The 39-in-a-row failure loop is one visible piece of that.
That is the pattern behind most surprise AI bills I've read about. The plan was fine. The exceptions to the plan are what cost money, and exceptions don't show up in a daily total until it's too late.
Why a usage page didn't catch it
A usage dashboard answers "how much did I spend?" It's good at that. It's bad at "why?", because the why lives one level down, in individual tool calls.
A failure loop looks like this in a log: the same tool, called with the same input, again and again, each call failing. On a daily chart it looks like a busy day. In the log it's obvious. The first try is normal. The second try is reasonable. By the third identical failure, every further call is waste, because nothing about the input has changed.
So the useful question isn't "what did I spend this week". It's "where did the same call repeat three or more times, and what did the repeats cost?"
Three settings that would have changed the outcome
The issue's own request lists the fixes it wanted: cost and limit estimation, and spending safeguards when Claude Code creates scheduled or recurring jobs. Until something like that ships, here's what you can do yourself:
-
Tell the agent to stop after two identical failures. Put a line in
CLAUDE.mdlike: "If the same command fails twice with the same input, stop and tell me instead of retrying." It's crude, and it works because the agent readsCLAUDE.mdat the start of every session. -
Cap headless runs. If anything runs unattended, pass
--max-turnsso a run can't loop forever. Claude Code's docs also describe--max-budget-usd, which caps the dollar figure of a run. - Don't use a model for a yes/no check. "Is there new mail?" is a few lines of script. If the answer is "no" 95% of the time, the model should only wake up for the 5%.
None of these need a new tool. They need you to know that a loop is happening, which is the part that's hard.
The 1-minute self-check
Claude Code keeps a JSONL transcript of every session under ~/.claude/projects/. Each assistant message lists the tool calls it made. Repeated identical calls are easy to find.
Save this as loops.py and run python3 loops.py. It reads your logs locally and prints nothing but counts and the first 120 characters of each repeated call.
import json, glob, os
from itertools import groupby
for path in glob.glob(os.path.expanduser("~/.claude/projects/**/*.jsonl"), recursive=True):
calls = []
for line in open(path, encoding="utf-8", errors="replace"):
try: d = json.loads(line)
except ValueError: continue
content = (d.get("message") or {}).get("content")
if d.get("type") != "assistant" or not isinstance(content, list): continue
for b in content:
if isinstance(b, dict) and b.get("type") == "tool_use":
calls.append(b.get("name", "") + " " + json.dumps(b.get("input"), sort_keys=True)[:120])
for call, run in groupby(calls):
n = len(list(run))
if n >= 3: print(n, "x", call, "|", os.path.basename(path))
On a test log with one bad loop, the output is a single line:
10 x Bash {"command": "npm test"} | s2.jsonl
How to read it:
- No output is the good result. No call repeated three times in a row.
- 3x or 4x now and then is normal. Flaky tests happen.
- 10x and up, or the same call showing up across many sessions, is a loop worth fixing. Open that session file and look at what came back each time.
The script counts calls; it doesn't price them. If you want the dollars, the free tool linked below does the same check, prices every call after the third try at list price, and lists your ten most expensive sessions. It runs either as one Python file or in a browser tab with nothing uploaded.
What to take from this
- The bill in #110395 came from exceptions, not from the plan. The stated baseline explains about a quarter of the spend.
- Daily totals can't show a loop. Tool-call logs can.
- Two lines of config (a stop rule in
CLAUDE.md,--max-turnson unattended runs) cover most of the risk.
If you run the self-check and find something odd, post the count (not the content) in the comments. I'm curious how common 10x-plus loops are in the wild.
Sources
- hirobaw, "Claude Code built recurring AI jobs with no cost estimate or spending cap…", anthropics/claude-code issue #110395, 16 Nov 2026. https://github.com/anthropics/claude-code/issues/110395
- Claude Code docs, "Manage costs effectively" (
/usage,--max-budget-usd). https://code.claude.com/docs/en/costs
Free tool (MIT, runs locally): https://github.com/6hn68/ai-bill-watchdog-free
來源:DEV Community · dev.to