-
Notifications
You must be signed in to change notification settings - Fork 139
Detect retry-after for Anthropic errors #11719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timtmok
wants to merge
7
commits into
main
Choose a base branch
from
11001-anthropic-retry-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
789c391
Implement Anthropic rate limit error handling
timtmok 058b7fc
Rate limiting retry for Vercel
timtmok a6ebb9e
Add retry-after handling for Posit provider
timtmok c609225
Cleanup
timtmok 55ca160
Fix copyrights
timtmok d6805b0
Update test url
timtmok d0190df
Check for retry error to get actual error
timtmok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,61 @@ | |
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import Anthropic from '@anthropic-ai/sdk'; | ||
| import * as ai from 'ai'; | ||
| import * as vscode from 'vscode'; | ||
| import { getAllModelDefinitions } from '../../modelDefinitions.js'; | ||
| import { createModelInfo, markDefaultModel } from '../../modelResolutionHelpers.js'; | ||
| import { ModelCapabilities } from '../base/modelProviderTypes.js'; | ||
| import { ModelProviderLogger } from '../base/modelProviderLogger.js'; | ||
|
|
||
| /** | ||
| * Checks if an error is a rate limit error (HTTP 429) from the native Anthropic SDK | ||
| * and throws a user-friendly error with retry-after information if available. | ||
| * | ||
| * @param error - The error to check | ||
| * @param providerName - The name of the provider for the error message prefix | ||
| * @returns true if the error was handled (and thrown), false otherwise | ||
| */ | ||
| export function handleNativeSdkRateLimitError(error: unknown, providerName: string): boolean { | ||
| if (error instanceof Anthropic.APIError && error.status === 429) { | ||
| const retryAfter = error.headers?.get('retry-after'); | ||
| if (retryAfter) { | ||
| throw new Error(`[${providerName}] Rate limit exceeded. Please retry after ${retryAfter} seconds.`); | ||
| } | ||
| throw new Error(`[${providerName}] Rate limit exceeded. Please try again later.`); | ||
|
Comment on lines
+25
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add some logging here and in the vercel version below so the error is logged in output as well? |
||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an error is a rate limit error (HTTP 429) from the Vercel AI SDK | ||
| * and throws a user-friendly error with retry-after information if available. | ||
| * | ||
| * Handles both direct APICallError and RetryError (which wraps multiple attempts). | ||
| * When the SDK exhausts retries, it throws a RetryError containing the lastError. | ||
| * | ||
| * @param error - The error to check | ||
| * @param providerName - The name of the provider for the error message prefix | ||
| * @returns true if the error was handled (and thrown), false otherwise | ||
| */ | ||
| export function handleVercelSdkRateLimitError(error: unknown, providerName: string): boolean { | ||
| // Check for RetryError first - the Vercel SDK wraps retried errors in this type | ||
| // when maxRetries is exceeded | ||
| let apiError: unknown = error; | ||
| if (ai.RetryError.isInstance(error) && error.lastError) { | ||
| apiError = error.lastError; | ||
| } | ||
|
|
||
| if (ai.APICallError.isInstance(apiError) && apiError.statusCode === 429) { | ||
| const retryAfter = apiError.responseHeaders?.['retry-after']; | ||
| if (retryAfter) { | ||
| throw new Error(`[${providerName}] Rate limit exceeded. Please retry after ${retryAfter} seconds.`); | ||
| } | ||
| throw new Error(`[${providerName}] Rate limit exceeded. Please try again later.`); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export const DEFAULT_ANTHROPIC_MODEL_NAME = 'Claude Sonnet 4'; | ||
| export const DEFAULT_ANTHROPIC_MODEL_MATCH = 'claude-sonnet-4'; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this and the vercel version of this still return boolean? the true case isn't returned for either. Maybe we can just not return anything?