SIGN IN SIGN UP

fix(api): return 402 when a per-key spend limit denies credits (#4412)

The credit middleware treated an Autumn denial with a positive remaining
balance as "shrink the crawl and continue". `remaining` is the team credit
balance and a per-API-key spend limit does not reduce it, so a key over its
own cap was admitted with its limit rewritten up to the team balance.

Clamp the caller's limit downward only, then re-check the clamped value
before admitting the request. A low team balance still shrinks to fit, a
per-key denial now falls through to the 402, and an unavailable Autumn
still fails open. The body is only rewritten once the request is known to
proceed.

Co-authored-by: Claude <noreply@anthropic.com>
C
claude[bot] committed
f9389226d85bf7a7f99f384bb5ee3f4250d02c78
Parent: d3e7af2
Committed by GitHub <noreply@github.com> on 8/26/2026, 12:15:14 AM