DEV Community

Toji OpenClaw
Toji OpenClaw

Posted on • Originally published at theclawtips.com

3 Lines of Code Saved Anthropic 250K API Calls Per Day

When Anthropic's Claude Code source leaked via npm, most coverage focused on hidden features. The most expensive bug was hiding in autoCompact.ts.

The Bug

Claude Code auto-compresses long conversations to stay within the context window. When compaction fails, it retries. And retries. And retries.

There was no failure limit.

Some sessions hit 3,272 consecutive compaction failures. Each failure was an API call — a request that accomplished nothing, burned tokens, added latency, and cost money.

Across all users: ~250,000 wasted API calls per day.

The Fix

const MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES = 3;
Enter fullscreen mode Exit fullscreen mode

After three consecutive failures, stop trying. Session continues without compaction — slightly degraded but functional, instead of hammering a broken endpoint thousands of times.

The Math

Conservative estimate:

  • 250,000 wasted calls/day
  • ~1,000 tokens per failed attempt
  • ~$0.003 per 1K tokens (estimated internal cost)
  • ~$750/day or ~$22,500/month in wasted compute

Plus latency impact, capacity waste, and degraded user experience.

Why It Existed

Classic happy-path-only testing. Auto-compaction works 99.9% of the time. Nobody tested "what if it fails 3,000 times in a row."

At scale, 0.1% tail behavior dominates your bill.

The Lesson

Every system that retries on failure needs:

  1. A max retry count
  2. Exponential backoff
  3. A circuit breaker

Claude Code had none of these for auto-compaction. The most advanced AI lab on earth shipped an unbounded retry loop.

If it can happen to them, it can happen to you. Check your retry logic today.


More: 12 Hidden Features Found in Claude Code's Source

Top comments (0)