@@ -494,7 +494,18 @@ func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, err
494494 }
495495
496496 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)
500+ if err != nil {
501+ return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
502+ }
503+ // if it didn't change, do not retry.
504+ if prev == a.providerOptions.apiKey {
505+ return false, 0, err
506+ }
507+ a.client = createAnthropicClient(a.providerOptions, a.tp)
508+ return true, 0, nil
498509 }
499510
500511 // Handle context limit exceeded error (400 Bad Request)
@@ -509,18 +520,7 @@ func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, err
509520 isOverloaded := strings.Contains(apiErr.Error(), "overloaded") || strings.Contains(apiErr.Error(), "rate limit exceeded")
510521 // 529 (unofficial): The service is overloaded
511522 if apiErr.StatusCode != http.StatusTooManyRequests && apiErr.StatusCode != 529 && !isOverloaded {
512− prev := a.providerOptions.apiKey
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)
515− if err != nil {
516− return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
517− }
518− // if it didn't change, do not retry.
519− if prev == a.providerOptions.apiKey {
520− return false, 0, err
521− }
522− a.client = createAnthropicClient(a.providerOptions, a.tp)
523− return true, 0, nil
523+ return false, 0, err
524524 }
525525
526526 retryMs := 0