Skip to content

fix: detect rate limits on streaming responses for proper failover - #60

Open
wagner-sousa wants to merge 3 commits into
snipeship:mainfrom
wagner-sousa:fix/streaming-rate-limit-failover
Open

fix: detect rate limits on streaming responses for proper failover#60
wagner-sousa wants to merge 3 commits into
snipeship:mainfrom
wagner-sousa:fix/streaming-rate-limit-failover

Conversation

@wagner-sousa

Copy link
Copy Markdown

Problem

When Claude Code makes streaming requests to Anthropic through ccflare and receives a 429 rate limit error, ccflare fails to:

  1. Detect the rate limit - The processProxyResponse function only checked for rate limits on non-streaming responses (if (!isStream && rateLimitInfo.isRateLimited)), so streaming 429 responses were silently ignored.

  2. Trigger failover - Since the rate limit wasn't detected, the account was never marked as rate-limited, and the failover loop never moved to the next account.

This caused the 429 error to be forwarded directly to the user instead of automatically retrying with another available account.

Solution

Removed the !isStream guard from processProxyResponse so rate limits are detected regardless of response type.

Added a fallback check for HTTP 429 status code to handle cases where parseRateLimit may not return isRateLimited for SSE error responses.

Changes

  • packages/proxy/src/handlers/response-processor.ts: Fixed rate limit detection
  • packages/proxy/src/handlers/response-processor.test.ts: Added test for streaming 429 fallback

Testing

All existing tests pass. Added new test case that verifies 429 detection via status code fallback for streaming responses.

Remove the !isStream guard from processProxyResponse so rate limits
are detected regardless of whether the response is streaming or not.

Add a fallback check for HTTP 429 status code to handle cases where
parseRateLimit may not return isRateLimited for SSE error responses.

This fixes the issue where Claude Code streaming requests that receive
429 rate limit errors from Anthropic were not triggering account
failover, causing the error to be forwarded directly to the client.
- Add response.body?.cancel() before failover in proxy-operations.ts
- Add response.body?.cancel() before failover in compat/handler.ts
- Fix type error in response-processor.test.ts (remove null values)
- Update build script to compile worker binary separately

This fixes the memory leak that caused Bun to crash with 34GB RSS
when streaming responses were not consumed during failover.
Anthropic OAuth rate limits typically last ~30 minutes, not 5.
The previous fallback was too short and caused accounts to be
unblocked prematurely in ccflare's view while still rejected
by the API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant