Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.

fix(anthropic): better 429/529 handling - #22

Merged
kujtimiihoxha merged 4 commits into
opencode-ai:mainfrom
adamdotdevin:adam/retries
Apr 8, 2025
Merged

kujtimiihoxha merged 4 commits into
opencode-ai:mainfrom
adamdotdevin:adam/retries

Conversation

@adamdotdevin

Copy link
Copy Markdown

i'm getting a lot of 429 throttling responses from Anthropic, and it looks like the Anthropic SDK (along with all Stainless SDKs) defaults to just 2 retries. i've noticed claude code uses max 8 retries, so i went with that. (note: it handles the retries using exponential backoff with jitter.)

@adamdotdevin

Copy link
Copy Markdown
Author

hold off on merging this, it doesn't seem to resolve the 429 issue, still digging

@kujtimiihoxha

Copy link
Copy Markdown
Contributor

Are you able to figure out what rate limit we are hitting? I think it could be multiple things, maybe I am not managing something as I should 🤔

@kujtimiihoxha

Copy link
Copy Markdown
Contributor

@adamdottv if you need to get more info you could log more stuff here https://github.com/kujtimiihoxha/termai/blob/main/internal/llm/agent/agent.go#L95

@kujtimiihoxha

Copy link
Copy Markdown
Contributor

It is weird that I have not seen the same issues? (you are using it more than me 😂 💯 )

@adamdotdevin

Copy link
Copy Markdown
Author

going to take a look this morning but realizing i'm not sure if the streaming response API call does retries at all, going to ask the stainless folks.

@kujtimiihoxha

kujtimiihoxha commented Apr 7, 2025 •

Copy link
Copy Markdown
Contributor

@adamdottv we might have to handle it ourselves 😅
Screenshot 2025-04-07 at 13 12 41

@adamdotdevin

Copy link
Copy Markdown
Author

yep! looks that way, i'll try to take this on this morning!

@adamdotdevin

Copy link
Copy Markdown
Author

ok, i've got anthropic 429s handled with eb8f2f5.

i'll need to make similar changes to the other providers, will get on that shortly.

Screenshot 2025-04-07 at 10 44 19 AM

@kujtimiihoxha

Copy link
Copy Markdown
Contributor

That is awesome 🙌

}

// 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

@adamdotdevin

Copy link
Copy Markdown
Author

@kujtimiihoxha sorry, this week getting busy real quick, back and forth getting bits of time on this and the current code is working, albeit with some minor UX issues (the status messages disappearing can feel bad when you've got a Retry-After=180 lol).

lmk what you think, once we're good with the anthropic version i can take a look at the other providers.

@adamdotdevin

Copy link
Copy Markdown
Author

forgot to call out that efea3f8 has the Retry-After support. i'll move exponential backoff stuff into a util when i add support for other providers.

@adamdotdevin

Copy link
Copy Markdown
Author

also noticed that anthropic will send 529's when they're "overloaded", so handling that in the same way as 429

}

// 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.

@kujtimiihoxha
kujtimiihoxha merged commit e51c3c7 into opencode-ai:main Apr 8, 2025
@kujtimiihoxha

Copy link
Copy Markdown
Contributor

@adamdottv here is what I changed 99630a5

Thanks again for helping out here, I did not even reach these limits so would never found this really appreciate your help man.

@adamdotdevin

Copy link
Copy Markdown
Author

THANK YOU lol, i started a task i didn't end up having consistent time to finish up, but this is huge for me to use the tool, i hit the throttle constantly (when not using Bedrock)

kujtimiihoxha added a commit that referenced this pull request Apr 17, 2025
fix(anthropic): better 429/529 handling
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants