[BUG] Claude Code generated an agent pipeline with an unbounded API retry loop and an ineffective spending guard, resulting in ~$50 of unintended OpenAI usage

Status Open
Maintainer reply None cached
Activity 1 comment · opened Sep 3, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

I asked Claude Code (Opus 5) to build a multi-agent outreach pipeline.
The generated code contained three compounding defects that together ran
up roughly $50 of unintended third-party API spend before I noticed.

  1. Unbounded retries. Every agent selected records by what they were

missing. When an LLM call failed to produce that output, the code
returned without recording anything, so the same record was selected
and re-charged on every subsequent run — indefinitely. The affected
call was the most expensive one in the system (~15,000 input tokens
on a premium model).

  1. The spending guard did not stop spending. It downgraded premium

requests to a "cheap" tier and continued. Since both tiers were
configured to the same paid provider, this slowed the spend rather
than stopping it. It also only checked the premium tier, leaving the
cheap tier entirely unbounded.

  1. The default ceiling could not bound anything. The generated default

was $25 per day, reset at midnight — $750/month presented as a safety
default, and no bound at all across multiple days, which is how the
spend accumulated without ever tripping the guard.

What I think is worth addressing: when Claude Code writes code that
spends money through a third-party API, the safety defaults it chooses
should be conservative, and a guard it describes as a budget limit
should actually stop spending rather than degrade. It was also never
suggested to me that I set a hard spending cap in the provider's own
dashboard, which would have contained this regardless of the code.

What Should Happen?

I want to ask Anthropic to reimburse third-party API charges $43 dollars if possible

Error Messages/Logs

Steps to Reproduce

"""Minimal reproduction of the unbounded-retry pattern.

No API key, no network, no dependencies. python3 repro.py

The generated pipeline selected records to process by *what they were
missing*, and on a failed model call returned without recording anything.
The record therefore still looked unprocessed and was selected again on the
next run, re-issuing the identical paid call — forever.

billable_call stands in for the real call, which carried ~15,000 input
tokens to a premium model.
"""

class Record:
def __init__(self, name):
self.name = name
self.result = None # what the agent is supposed to produce
self.attempts = {} # only present after the fix

CALLS = {"n": 0}

def billable_call(record):
"""Stands in for the paid API call. Always fails here, which is the case
the generated code did not handle: an unparseable response."""
CALLS["n"] += 1
return None # unparseable / failed response

─────────────────────────────────────────────────────────────

AS GENERATED

─────────────────────────────────────────────────────────────

def select_buggy(records):
return [r for r in records if r.result is None]

def run_buggy(records):
for record in select_buggy(records):
result = billable_call(record)
if not result:
return_value = False # nothing recorded — the defect
continue
record.result = result

─────────────────────────────────────────────────────────────

AFTER THE FIX

─────────────────────────────────────────────────────────────

MAX_ATTEMPTS = 3

def select_fixed(records):
return [
r for r in records
if r.result is None and r.attempts.get("agent", 0) < MAX_ATTEMPTS
]

def run_fixed(records):
for record in select_fixed(records):
record.attempts["agent"] = record.attempts.get("agent", 0) + 1
result = billable_call(record)
if not result:
continue
record.result = result
record.attempts.pop("agent", None)

def measure(runner, label):
CALLS["n"] = 0
records = [Record("Acme Robotics")]
for _ in range(10): # ten scheduled pipeline runs
runner(records)
print(f"{label:22} {CALLS['n']:>3} billable calls for 1 record over 10 runs")
return CALLS["n"]

if __name__ == "__main__":
print()
buggy = measure(run_buggy, "as generated:")
fixed = measure(run_fixed, "with attempt cap:")
print()
print(f"The generated code re-pays for the same record on every run.")
print(f"At ~15,000 input tokens on a premium model, {buggy} calls instead")
print(f"of {fixed} is the whole overspend, multiplied by every record.")
print()
assert buggy == 10, "expected one call per run, unbounded"
assert fixed == MAX_ATTEMPTS, "expected the cap to hold"
print("OK — both assertions hold.")

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

Opus 5

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

_No response_

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗