Skip to content

Commit 4c8f834

Browse files
jeffbargclaude
andauthored
feat: automatically respect rate limit headers in retry logic (#7246)
## Description This PR adds automatic support for respecting rate limit headers (`retry-after-ms` and `retry-after`) in the retry logic, following the same approach used by both the [Anthropic client SDK](https://github.com/anthropics/anthropic-sdk-typescript/blob/main/src/client.ts#L747-L775) and [OpenAI client SDK](https://github.com/openai/openai-node/blob/master/src/client.ts#L753-L781). Currently, when providers like Anthropic and OpenAI return 429 rate limit errors with `retry-after` headers, the SDK doesn't automatically respect these headers and instead uses its standard exponential backoff. This can lead to unnecessary retries that fail because they happen before the rate limit window has passed. ## Changes - Added `getRetryDelay` function that parses rate limit headers and determines the appropriate retry delay - Modified retry logic to use the parsed rate limit delay when reasonable (0-60 seconds), falling back to exponential backoff otherwise - This is a behind-the-scenes improvement with no API changes required The implementation follows the exact logic used by both Anthropic and OpenAI SDKs: 1. First checks for `retry-after-ms` header (milliseconds) 2. Falls back to `retry-after` header (seconds or HTTP date) 3. Only uses the header value if it's reasonable (between 0 and 60 seconds) 4. Falls back to exponential backoff if no valid headers or if the delay is unreasonable ## Testing Added comprehensive tests covering: - Reasonable rate limit delays (under 60 seconds) - Unreasonable delays that fall back to exponential backoff - Invalid header values - Multiple retry scenarios - Mocked responses from Anthropic and OpenAI - HTTP date format parsing - Preference for `retry-after-ms` over `retry-after` All tests pass successfully. ## Issue Closes #7247 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent a3b1441 commit 4c8f834

File tree

4 files changed

+519
-5
lines changed

4 files changed

+519
-5
lines changed

‎.changeset/wise-headers-respect.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
feat: automatically respect rate limit headers in retry logic
6+
7+
Added automatic support for respecting rate limit headers (`retry-after-ms` and `retry-after`) in the SDK's retry logic. When these headers are present and contain reasonable values (0-60 seconds), the retry mechanism will use the server-specified delay instead of exponential backoff. This matches the behavior of Anthropic and OpenAI client SDKs and improves rate limit handling without requiring any API changes.

‎packages/ai/src/util/prepare-retries.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { InvalidArgumentError } from '../../src/error/invalid-argument-error';
22
import {
33
RetryFunction,
4-
retryWithExponentialBackoff,
4+
retryWithExponentialBackoffRespectingRetryHeaders,
55
} from '../../src/util/retry-with-exponential-backoff';
66

77
/**
@@ -37,6 +37,8 @@ export function prepareRetries({
3737

3838
return {
3939
maxRetries: maxRetriesResult,
40-
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult }),
40+
retry: retryWithExponentialBackoffRespectingRetryHeaders({
41+
maxRetries: maxRetriesResult,
42+
}),
4143
};
4244
}

0 commit comments

Comments
 (0)