@@ -494,7 +494,18 @@ func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, err
496 if apiErr.StatusCode == http.StatusUnauthorized {
496 if apiErr.StatusCode == http.StatusUnauthorized {
497− return false, 0, err
497+ prev := a.providerOptions.apiKey
498+ // in case the key comes from a script, we try to re-evaluate it.
499+ a.providerOptions.apiKey, err = config.Get().Resolve(a.providerOptions.config.APIKey)
501+ return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
503+ // if it didn't change, do not retry.
504+ if prev == a.providerOptions.apiKey {
505+ return false, 0, err
507+ a.client = createAnthropicClient(a.providerOptions, a.tp)
500 // Handle context limit exceeded error (400 Bad Request)
511 // Handle context limit exceeded error (400 Bad Request)
@@ -509,18 +520,7 @@ func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, err
509 isOverloaded := strings.Contains(apiErr.Error(), "overloaded") || strings.Contains(apiErr.Error(), "rate limit exceeded")
520 isOverloaded := strings.Contains(apiErr.Error(), "overloaded") || strings.Contains(apiErr.Error(), "rate limit exceeded")
510 // 529 (unofficial): The service is overloaded
521 // 529 (unofficial): The service is overloaded
511 if apiErr.StatusCode != http.StatusTooManyRequests && apiErr.StatusCode != 529 && !isOverloaded {
522 if apiErr.StatusCode != http.StatusTooManyRequests && apiErr.StatusCode != 529 && !isOverloaded {
512− prev := a.providerOptions.apiKey
523+ return false, 0, err
513− // in case the key comes from a script, we try to re-evaluate it.
514− a.providerOptions.apiKey, err = config.Get().Resolve(a.providerOptions.config.APIKey)
516− return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
518− // if it didn't change, do not retry.
519− if prev == a.providerOptions.apiKey {
520− return false, 0, err
522− a.client = createAnthropicClient(a.providerOptions, a.tp)
526 retryMs := 0
526 retryMs := 0