|
5 | 5 | "encoding/json" |
6 | 6 | "errors" |
7 | 7 | "fmt" |
8 | | - "log" |
9 | 8 | "strings" |
10 | 9 | "time" |
11 | 10 |
|
@@ -88,7 +87,6 @@ func (a *anthropicProvider) SendMessages(ctx context.Context, messages []message |
88 | 87 | }, |
89 | 88 | }, |
90 | 89 | }, |
91 | | - option.WithMaxRetries(8), |
92 | 90 | ) |
93 | 91 | if err != nil { |
94 | 92 | return nil, err |
@@ -256,11 +254,37 @@ func (a *anthropicProvider) StreamResponse(ctx context.Context, messages []messa |
256 | 254 | // Check for stream errors |
257 | 255 | err := stream.Err() |
258 | 256 | if err != nil { |
259 | | - log.Println("error", err) |
260 | | - |
261 | 257 | var apierr *anthropic.Error |
262 | | - if errors.As(err, &apierr) && apierr.StatusCode == 429 { |
263 | | - continue |
| 258 | + if errors.As(err, &apierr) { |
| 259 | + if apierr.StatusCode == 429 || apierr.StatusCode == 529 { |
| 260 | + // Check for Retry-After header |
| 261 | + if retryAfterValues := apierr.Response.Header.Values("Retry-After"); len(retryAfterValues) > 0 { |
| 262 | + // Parse the retry after value (seconds) |
| 263 | + var retryAfterSec int |
| 264 | + if _, err := fmt.Sscanf(retryAfterValues[0], "%d", &retryAfterSec); err == nil { |
| 265 | + retryMs := retryAfterSec * 1000 |
| 266 | + |
| 267 | + // Inform user of retry with specific wait time |
| 268 | + eventChan <- ProviderEvent{ |
| 269 | + Type: EventWarning, |
| 270 | + Info: fmt.Sprintf("[Rate limited: waiting %d seconds as specified by API]", retryAfterSec), |
| 271 | + } |
| 272 | + |
| 273 | + // Sleep respecting context cancellation |
| 274 | + select { |
| 275 | + case <-ctx.Done(): |
| 276 | + eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()} |
| 277 | + return |
| 278 | + case <-time.After(time.Duration(retryMs) * time.Millisecond): |
| 279 | + // Continue with retry after specified delay |
| 280 | + continue |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + // Fall back to exponential backoff if Retry-After parsing failed |
| 286 | + continue |
| 287 | + } |
264 | 288 | } |
265 | 289 |
|
266 | 290 | // For non-rate limit errors, report and exit |
@@ -380,3 +404,4 @@ func (a *anthropicProvider) convertToAnthropicMessages(messages []message.Messag |
380 | 404 |
|
381 | 405 | return anthropicMessages |
382 | 406 | } |
| 407 | + |
0 commit comments