Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func setupSubscriptions(app *app.App) (chan tea.Msg, func()) {
wg.Done()
}()
}
{
sub := app.Status.Subscribe(ctx)
wg.Add(1)
go func() {
for ev := range sub {
ch <- ev
}
wg.Done()
}()
}
return ch, func() {
cancel()
wg.Wait()
Expand Down
4 changes: 4 additions & 0 deletions internal/app/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/kujtimiihoxha/termai/internal/lsp/watcher"
"github.com/kujtimiihoxha/termai/internal/message"
"github.com/kujtimiihoxha/termai/internal/permission"
"github.com/kujtimiihoxha/termai/internal/pubsub"
"github.com/kujtimiihoxha/termai/internal/session"
"github.com/kujtimiihoxha/termai/internal/tui/util"
)

type App struct {
Expand All @@ -25,6 +27,7 @@ type App struct {

Logger logging.Interface

Status *pubsub.Broker[util.InfoMsg]
ceanups []func()
}

Expand All @@ -43,6 +46,7 @@ func New(ctx context.Context, conn *sql.DB) *App {
Messages: messages,
Permissions: permission.Default,
Logger: log,
Status: pubsub.NewBroker[util.InfoMsg](),
LSPClients: make(map[string]*lsp.Client),
}

Expand Down
19 changes: 18 additions & 1 deletion internal/llm/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/kujtimiihoxha/termai/internal/llm/provider"
"github.com/kujtimiihoxha/termai/internal/llm/tools"
"github.com/kujtimiihoxha/termai/internal/message"
"github.com/kujtimiihoxha/termai/internal/pubsub"
"github.com/kujtimiihoxha/termai/internal/tui/util"
)

type Agent interface {
Expand Down Expand Up @@ -92,9 +94,24 @@ func (c *agent) processEvent(
assistantMsg.AppendContent(event.Content)
return c.Messages.Update(*assistantMsg)
case provider.EventError:
// TODO: remove when realease
log.Println("error", event.Error)
c.App.Status.Publish(pubsub.UpdatedEvent, util.InfoMsg{
Type: util.InfoTypeError,
Msg: event.Error.Error(),
})
return event.Error

case provider.EventWarning:
c.App.Status.Publish(pubsub.UpdatedEvent, util.InfoMsg{
Type: util.InfoTypeWarn,
Msg: event.Info,
})
return nil
case provider.EventInfo:
c.App.Status.Publish(pubsub.UpdatedEvent, util.InfoMsg{
Type: util.InfoTypeInfo,
Msg: event.Info,
})
case provider.EventComplete:
assistantMsg.SetToolCalls(event.Response.ToolCalls)
assistantMsg.AddFinish(event.Response.FinishReason)
Expand Down
234 changes: 164 additions & 70 deletions internal/llm/provider/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
Expand Down Expand Up @@ -68,21 +70,24 @@ func (a *anthropicProvider) SendMessages(ctx context.Context, messages []message
anthropicMessages := a.convertToAnthropicMessages(messages)
anthropicTools := a.convertToAnthropicTools(tools)

response, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{
Model: anthropic.Model(a.model.APIModel),
MaxTokens: a.maxTokens,
Temperature: anthropic.Float(0),
Messages: anthropicMessages,
Tools: anthropicTools,
System: []anthropic.TextBlockParam{
{
Text: a.systemMessage,
CacheControl: anthropic.CacheControlEphemeralParam{
Type: "ephemeral",
response, err := a.client.Messages.New(
ctx,
anthropic.MessageNewParams{
Model: anthropic.Model(a.model.APIModel),
MaxTokens: a.maxTokens,
Temperature: anthropic.Float(0),
Messages: anthropicMessages,
Tools: anthropicTools,
System: []anthropic.TextBlockParam{
{
Text: a.systemMessage,
CacheControl: anthropic.CacheControlEphemeralParam{
Type: "ephemeral",
},
},
},
},
})
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,83 +126,171 @@ func (a *anthropicProvider) StreamResponse(ctx context.Context, messages []messa
temperature = anthropic.Float(1)
}

stream := a.client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
Model: anthropic.Model(a.model.APIModel),
MaxTokens: a.maxTokens,
Temperature: temperature,
Messages: anthropicMessages,
Tools: anthropicTools,
Thinking: thinkingParam,
System: []anthropic.TextBlockParam{
{
Text: a.systemMessage,
CacheControl: anthropic.CacheControlEphemeralParam{
Type: "ephemeral",
},
},
},
})

eventChan := make(chan ProviderEvent)

go func() {
defer close(eventChan)

accumulatedMessage := anthropic.Message{}
const maxRetries = 8
attempts := 0

for stream.Next() {
event := stream.Current()
err := accumulatedMessage.Accumulate(event)
if err != nil {
eventChan <- ProviderEvent{Type: EventError, Error: err}
return
for {
// If this isn't the first attempt, we're retrying
if attempts > 0 {
if attempts > maxRetries {
eventChan <- ProviderEvent{
Type: EventError,
Error: errors.New("maximum retry attempts reached for rate limit (429)"),
}
return
}

// Inform user we're retrying with attempt number
eventChan <- ProviderEvent{

@kujtimiihoxha kujtimiihoxha Apr 7, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so I did not want to go through the trouble of forking the fork but here is a diff making some changes to how I handle the status bar messages that will allow you to now send messages to the status bar letting the user know about stuff like this.

I did not directly test this but let me know if it works for you.

Also we should most likely move the backoff logic to some util function.

Another thing to check is if there is a way to get that retry-after header to use here.

Our rate limits for the Messages API are measured in requests per minute (RPM), input tokens per minute (ITPM), and output tokens per minute (OTPM) for each model class. If you exceed any of the rate limits you will get a 429 error describing which rate limit was exceeded, along with a retry-after header indicating how long to wait.

diff.diff.zip

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait I was able to push 😂 08108c8

Type: EventWarning,
Info: fmt.Sprintf("[Retrying due to rate limit... attempt %d of %d]", attempts, maxRetries),
}

// Calculate backoff with exponential backoff and jitter
backoffMs := 2000 * (1 << (attempts - 1)) // 2s, 4s, 8s, 16s, 32s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since we now have retry-after exponential backoff should only be used when the error was the overloaded_error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you tried handling this by

// Fall back to exponential backoff if Retry-After parsing failed

But i think the code will always reach here and wait for an additional amount of time

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge what is here so far and clean in up after.

jitterMs := int(float64(backoffMs) * 0.2)
totalBackoffMs := backoffMs + jitterMs

// Sleep with backoff, respecting context cancellation
select {
case <-ctx.Done():
eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
return
case <-time.After(time.Duration(totalBackoffMs) * time.Millisecond):
// Continue with retry
}
}

switch event := event.AsAny().(type) {
case anthropic.ContentBlockStartEvent:
eventChan <- ProviderEvent{Type: EventContentStart}
attempts++

// Create new streaming request
stream := a.client.Messages.NewStreaming(
ctx,
anthropic.MessageNewParams{
Model: anthropic.Model(a.model.APIModel),
MaxTokens: a.maxTokens,
Temperature: temperature,
Messages: anthropicMessages,
Tools: anthropicTools,
Thinking: thinkingParam,
System: []anthropic.TextBlockParam{
{
Text: a.systemMessage,
CacheControl: anthropic.CacheControlEphemeralParam{
Type: "ephemeral",
},
},
},
},
)

case anthropic.ContentBlockDeltaEvent:
if event.Delta.Type == "thinking_delta" && event.Delta.Thinking != "" {
eventChan <- ProviderEvent{
Type: EventThinkingDelta,
Thinking: event.Delta.Thinking,
// Process stream events
accumulatedMessage := anthropic.Message{}
streamSuccess := false

// Process the stream until completion or error
for stream.Next() {
event := stream.Current()
err := accumulatedMessage.Accumulate(event)
if err != nil {
eventChan <- ProviderEvent{Type: EventError, Error: err}
return // Don't retry on accumulation errors
}

switch event := event.AsAny().(type) {
case anthropic.ContentBlockStartEvent:
eventChan <- ProviderEvent{Type: EventContentStart}

case anthropic.ContentBlockDeltaEvent:
if event.Delta.Type == "thinking_delta" && event.Delta.Thinking != "" {
eventChan <- ProviderEvent{
Type: EventThinkingDelta,
Thinking: event.Delta.Thinking,
}
} else if event.Delta.Type == "text_delta" && event.Delta.Text != "" {
eventChan <- ProviderEvent{
Type: EventContentDelta,
Content: event.Delta.Text,
}
}
} else if event.Delta.Type == "text_delta" && event.Delta.Text != "" {
eventChan <- ProviderEvent{
Type: EventContentDelta,
Content: event.Delta.Text,

case anthropic.ContentBlockStopEvent:
eventChan <- ProviderEvent{Type: EventContentStop}

case anthropic.MessageStopEvent:
streamSuccess = true
content := ""
for _, block := range accumulatedMessage.Content {
if text, ok := block.AsAny().(anthropic.TextBlock); ok {
content += text.Text
}
}
}

case anthropic.ContentBlockStopEvent:
eventChan <- ProviderEvent{Type: EventContentStop}
toolCalls := a.extractToolCalls(accumulatedMessage.Content)
tokenUsage := a.extractTokenUsage(accumulatedMessage.Usage)

case anthropic.MessageStopEvent:
content := ""
for _, block := range accumulatedMessage.Content {
if text, ok := block.AsAny().(anthropic.TextBlock); ok {
content += text.Text
eventChan <- ProviderEvent{
Type: EventComplete,
Response: &ProviderResponse{
Content: content,
ToolCalls: toolCalls,
Usage: tokenUsage,
FinishReason: string(accumulatedMessage.StopReason),
},
}
}
}

toolCalls := a.extractToolCalls(accumulatedMessage.Content)
tokenUsage := a.extractTokenUsage(accumulatedMessage.Usage)
// If the stream completed successfully, we're done
if streamSuccess {
return
}

eventChan <- ProviderEvent{
Type: EventComplete,
Response: &ProviderResponse{
Content: content,
ToolCalls: toolCalls,
Usage: tokenUsage,
FinishReason: string(accumulatedMessage.StopReason),
},
// Check for stream errors
err := stream.Err()
if err != nil {
var apierr *anthropic.Error
if errors.As(err, &apierr) {
if apierr.StatusCode == 429 || apierr.StatusCode == 529 {
// Check for Retry-After header
if retryAfterValues := apierr.Response.Header.Values("Retry-After"); len(retryAfterValues) > 0 {
// Parse the retry after value (seconds)
var retryAfterSec int
if _, err := fmt.Sscanf(retryAfterValues[0], "%d", &retryAfterSec); err == nil {
retryMs := retryAfterSec * 1000

// Inform user of retry with specific wait time
eventChan <- ProviderEvent{
Type: EventWarning,
Info: fmt.Sprintf("[Rate limited: waiting %d seconds as specified by API]", retryAfterSec),
}

// Sleep respecting context cancellation
select {
case <-ctx.Done():
eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
return
case <-time.After(time.Duration(retryMs) * time.Millisecond):
// Continue with retry after specified delay
continue
}
}
}

// Fall back to exponential backoff if Retry-After parsing failed
continue
}
}
}
}

if stream.Err() != nil {
eventChan <- ProviderEvent{Type: EventError, Error: stream.Err()}
// For non-rate limit errors, report and exit
eventChan <- ProviderEvent{Type: EventError, Error: err}
return
}
}
}()

Expand Down Expand Up @@ -311,3 +404,4 @@ func (a *anthropicProvider) convertToAnthropicMessages(messages []message.Messag

return anthropicMessages
}

5 changes: 5 additions & 0 deletions internal/llm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
EventContentStop EventType = "content_stop"
EventComplete EventType = "complete"
EventError EventType = "error"
EventWarning EventType = "warning"
EventInfo EventType = "info"
)

type TokenUsage struct {
Expand All @@ -40,6 +42,9 @@ type ProviderEvent struct {
ToolCall *message.ToolCall
Error error
Response *ProviderResponse

// Used for giving users info on e.x retry
Info string
}

type Provider interface {
Expand Down
Loading