@@ -319,6 +319,10 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
319319 go func() {
320320 for {
321321 attempts++
322+ // Kujtim: fixes an issue with anthropig models on openrouter
323+ if len(params.Tools) == 0 {
324+ params.Tools = nil
325+ }
322326 openaiStream := o.client.Chat.Completions.NewStreaming(
323327 ctx,
324328 params,
@@ -331,8 +335,14 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
331335 var currentToolCallID string
332336 var currentToolCall openai.ChatCompletionMessageToolCall
333337 var msgToolCalls []openai.ChatCompletionMessageToolCall
338+ currentToolIndex := 0
334339 for openaiStream.Next() {
335340 chunk := openaiStream.Current()
341+ // Kujtim: this is an issue with openrouter qwen, its sending -1 for the tool index
342+ if len(chunk.Choices) > 0 && len(chunk.Choices[0].Delta.ToolCalls) > 0 && chunk.Choices[0].Delta.ToolCalls[0].Index == -1 {
343+ chunk.Choices[0].Delta.ToolCalls[0].Index = int64(currentToolIndex)
344+ currentToolIndex++
345+ }
336346 acc.AddChunk(chunk)
337347 // This fixes multiple tool calls for some providers
338348 for _, choice := range chunk.Choices {
@@ -348,6 +358,14 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
348358 if currentToolCallID == "" {
349359 if toolCall.ID != "" {
350360 currentToolCallID = toolCall.ID
361+ eventChan <- ProviderEvent{
362+ Type: EventToolUseStart,
363+ ToolCall: &message.ToolCall{
364+ ID: toolCall.ID,
365+ Name: toolCall.Function.Name,
366+ Finished: false,
367+ },
368+ }
351369 currentToolCall = openai.ChatCompletionMessageToolCall{
352370 ID: toolCall.ID,
353371 Type: "function",
@@ -359,13 +377,21 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
359377 }
360378 } else {
361379 // Delta tool use
362− if toolCall.ID == "" {
380+ if toolCall.ID == "" || toolCall.ID == currentToolCallID {
363381 currentToolCall.Function.Arguments += toolCall.Function.Arguments
364382 } else {
365383 // Detect new tool use
366384 if toolCall.ID != currentToolCallID {
367385 msgToolCalls = append(msgToolCalls, currentToolCall)
368386 currentToolCallID = toolCall.ID
387+ eventChan <- ProviderEvent{
388+ Type: EventToolUseStart,
389+ ToolCall: &message.ToolCall{
390+ ID: toolCall.ID,
391+ Name: toolCall.Function.Name,
392+ Finished: false,
393+ },
394+ }
369395 currentToolCall = openai.ChatCompletionMessageToolCall{
370396 ID: toolCall.ID,
371397 Type: "function",
@@ -378,7 +404,8 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
378404 }
379405 }
380406 }
381− if choice.FinishReason == "tool_calls" {
407+ // Kujtim: some models send finish stop even for tool calls
408+ if choice.FinishReason == "tool_calls" || (choice.FinishReason == "stop" && currentToolCallID != "") {
382409 msgToolCalls = append(msgToolCalls, currentToolCall)
383410 if len(acc.Choices) > 0 {
384411 acc.Choices[0].Message.ToolCalls = msgToolCalls
@@ -461,31 +488,41 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
461488}
462489
463490func (o *openaiClient) shouldRetry(attempts int, err error) (bool, int64, error) {
464− var apiErr *openai.Error
465− if !errors.As(err, &apiErr) {
466− return false, 0, err
467− }
468−
469491 if attempts > maxRetries {
470492 return false, 0, fmt.Errorf("maximum retry attempts reached for rate limit: %d retries", maxRetries)
471493 }
494+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
495+ return false, 0, err
496+ }
497+ var apiErr *openai.Error
498+ retryMs := 0
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)
504+ if err != nil {
505+ return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
506+ }
507+ o.client = createOpenAIClient(o.providerOptions)
508+ return true, 0, nil
509+ }
472510
473− // Check for token expiration (401 Unauthorized)
474− if apiErr.StatusCode == 401 {
475− o.providerOptions.apiKey, err = config.Get().Resolve(o.providerOptions.config.APIKey)
476− if err != nil {
477− return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
511+ if apiErr.StatusCode != 429 && apiErr.StatusCode != 500 {
512+ return false, 0, err
478513 }
479− o.client = createOpenAIClient(o.providerOptions)
480− return true, 0, nil
481− }
482514
483− if apiErr.StatusCode != 429 && apiErr.StatusCode != 500 {
484− return false, 0, err
515+ retryAfterValues = apiErr.Response.Header.Values("Retry-After")
485516 }
486517
487− retryMs := 0
488− retryAfterValues := apiErr.Response.Header.Values("Retry-After")
518+ if apiErr != nil {
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)
522+ }
523+ } else {
524+ slog.Warn("OpenAI API error", "error", err.Error())
525+ }
489526
490527 backoffMs := 2000 * (1 << (attempts - 1))
491528 jitterMs := int(float64(backoffMs) * 0.2)