@@ -319,6 +319,10 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
319 go func() {
319 go func() {
321 attempts++
321 attempts++
322+ // Kujtim: fixes an issue with anthropig models on openrouter
323+ if len(params.Tools) == 0 {
322 openaiStream := o.client.Chat.Completions.NewStreaming(
326 openaiStream := o.client.Chat.Completions.NewStreaming(
@@ -484,31 +488,41 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
486func (o *openaiClient) shouldRetry(attempts int, err error) (bool, int64, error) {
490func (o *openaiClient) shouldRetry(attempts int, err error) (bool, int64, error) {
487− var apiErr *openai.Error
488− if !errors.As(err, &apiErr) {
489− return false, 0, err
492 if attempts > maxRetries {
491 if attempts > maxRetries {
493 return false, 0, fmt.Errorf("maximum retry attempts reached for rate limit: %d retries", maxRetries)
492 return false, 0, fmt.Errorf("maximum retry attempts reached for rate limit: %d retries", maxRetries)
494+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
495+ return false, 0, err
497+ var apiErr *openai.Error
499+ retryAfterValues := []string{}
500+ if errors.As(err, &apiErr) {
501+ // Check for token expiration (401 Unauthorized)
502+ if apiErr.StatusCode == 401 {
503+ o.providerOptions.apiKey, err = config.Get().Resolve(o.providerOptions.config.APIKey)
505+ return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
507+ o.client = createOpenAIClient(o.providerOptions)
496− // Check for token expiration (401 Unauthorized)
497− if apiErr.StatusCode == 401 {
511+ if apiErr.StatusCode != 429 && apiErr.StatusCode != 500 {
498− o.providerOptions.apiKey, err = config.Get().Resolve(o.providerOptions.config.APIKey)
500− return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
512+ return false, 0, err
502− o.client = createOpenAIClient(o.providerOptions)
506− if apiErr.StatusCode != 429 && apiErr.StatusCode != 500 {
515+ retryAfterValues = apiErr.Response.Header.Values("Retry-After")
507− return false, 0, err
510− retryMs := 0
518+ if apiErr != nil {
511− retryAfterValues := apiErr.Response.Header.Values("Retry-After")
519+ slog.Warn("OpenAI API error", "status_code", apiErr.StatusCode, "message", apiErr.Message, "type", apiErr.Type)
520+ if len(retryAfterValues) > 0 {
521+ slog.Warn("Retry-After header", "values", retryAfterValues)
524+ slog.Warn("OpenAI API error", "error", err.Error())
513 backoffMs := 2000 * (1 << (attempts - 1))
527 backoffMs := 2000 * (1 << (attempts - 1))
514 jitterMs := int(float64(backoffMs) * 0.2)
528 jitterMs := int(float64(backoffMs) * 0.2)