Implement exponential back-off when hitting rate limits #1172

Open
opened 2026-02-16 17:29:51 -05:00 by yindo · 12 comments
Owner

Originally created by @BigMitchGit on GitHub (Aug 8, 2025).

Originally assigned to: @thdxr on GitHub.

OpenAI has some brutal RPM rate limits on tier 1, which will often abruptly stop opencode and requires me to manually resume by sending 'continue' message.

An exponential back-off with auto-retry would keep the agent loop running, it'd just take a bit longer to complete when rate limits are hit.

Originally created by @BigMitchGit on GitHub (Aug 8, 2025). Originally assigned to: @thdxr on GitHub. OpenAI has some brutal RPM rate limits on tier 1, which will often abruptly stop opencode and requires me to manually resume by sending 'continue' message. An exponential back-off with auto-retry would keep the agent loop running, it'd just take a bit longer to complete when rate limits are hit.
Author
Owner

@daliusd commented on GitHub (Aug 8, 2025):

If that helps here is error you get when you hit rate limit:

{"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit reached
for gpt-5-mini in organization org-XXXXXXXXXXXXXX on tokens per min (TPM): Limit 200000, Used 124035,
Requested 79017. Please try again in 915ms. Visit https://platform.openai.com/account/rate-limits to learn
more.","param":null}}
@daliusd commented on GitHub (Aug 8, 2025): If that helps here is error you get when you hit rate limit: ``` {"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5-mini in organization org-XXXXXXXXXXXXXX on tokens per min (TPM): Limit 200000, Used 124035, Requested 79017. Please try again in 915ms. Visit https://platform.openai.com/account/rate-limits to learn more.","param":null}} ```
Author
Owner

@AlexeyYar commented on GitHub (Aug 8, 2025):

Same here:
{"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit
reached for gpt-5 in organization org-************* on tokens per min (TPM): Limit 30000, Used 14582,
Requested 21045. Please try again in 11.254s. Visit https://platform.openai.com/account/rate-limits to learn
more.","param":null}}

@AlexeyYar commented on GitHub (Aug 8, 2025): Same here: {"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5 in organization org-************* on tokens per min (TPM): Limit 30000, Used 14582, Requested 21045. Please try again in 11.254s. Visit https://platform.openai.com/account/rate-limits to learn more.","param":null}}
Author
Owner

@WilliamAGH commented on GitHub (Aug 11, 2025):

Yeah, Cerebras and Groq are currently unusable with Opencode right now still because of this.

Constant errors like:

AI_RetryError: Failed after 4 attempts. Last error: Too Many Requests

@WilliamAGH commented on GitHub (Aug 11, 2025): Yeah, Cerebras and Groq are currently unusable with Opencode right now still because of this. Constant errors like: > AI_RetryError: Failed after 4 attempts. Last error: Too Many Requests
Author
Owner

@JC1738 commented on GitHub (Aug 14, 2025):

Same, AI_RetryError: Failed after 4 attempts. Last error: Requests per second limit exceeded - too many requests sent.

cerebras with Qwen 3 Coder 480B

I can't get through the /init without the being blocked by this error, and unfortunately you can't resume the init.

@JC1738 commented on GitHub (Aug 14, 2025): Same, AI_RetryError: Failed after 4 attempts. Last error: Requests per second limit exceeded - too many requests sent. cerebras with Qwen 3 Coder 480B I can't get through the /init without the being blocked by this error, and unfortunately you can't resume the init.
Author
Owner

@jsugg-qventus commented on GitHub (Sep 11, 2025):

Same here. On each message.

@jsugg-qventus commented on GitHub (Sep 11, 2025): Same here. On each message.
Author
Owner

@sra commented on GitHub (Dec 9, 2025):

After these rate limit stops, what do you type to make it continue? I've not had great luck.. Cerebras is awesome until the brick wall per minute limit

@sra commented on GitHub (Dec 9, 2025): After these rate limit stops, what do you type to make it continue? I've not had great luck.. Cerebras is awesome until the brick wall per minute limit
Author
Owner

@rekram1-node commented on GitHub (Dec 9, 2025):

We have backoff for ratelimits, and we respect the retry-after type headers so once it is ready to retry it should automatically go through

@rekram1-node commented on GitHub (Dec 9, 2025): We have backoff for ratelimits, and we respect the retry-after type headers so once it is ready to retry it should automatically go through
Author
Owner

@jcgottlieb commented on GitHub (Jan 9, 2026):

My experience as of today with opencode latest /connected to the OpenAI API @ Tier 1 with model 5.1-codex-mini (200K TPM rate limit), even VERY SIMPLE requests are rate limited and fail every time, making the OpenAI API unusable without throwing money at a higher tier. Anyone solved this without $++ yet?

@jcgottlieb commented on GitHub (Jan 9, 2026): My experience as of today with opencode latest /connected to the OpenAI API @ Tier 1 with model 5.1-codex-mini (200K TPM rate limit), even `VERY SIMPLE` requests are rate limited and fail every time, making the OpenAI API unusable without throwing money at a higher tier. Anyone solved this without $++ yet?
Author
Owner

@tgrushka commented on GitHub (Jan 14, 2026):

Exponential backoff seems way overkill.

To state the obvious: exponential backoff means OpenCode waits TWICE THE LENGTH OF TIME it waited the last time on every retry.

This means, after only 7 retries, you wait over a minute.
On the 8th retry, you wait 2 minutes.

I think it should be linear, such as retry after 1 second, then 2 seconds, then 3 seconds, ... up to some kind of user configurable maximum such as 10 or 15 seconds.

"Exponential backoff" seems to be the go-to solution for everything nowadays. Even AI suggests it strongly to me for anything and everything network-related, indiscriminately. I don't know where that came from ... perhaps the 1980s, when networks were really unreliable, and retrying a network backup or something made sense to eventually take hours to make a retry attempt. But nowadays? In 2026? Exponential? Maybe in some low priority cloud services such as backups (even then, why exponential and not linear?), but it really hits the user hard in terms of experience in user-facing apps.

@tgrushka commented on GitHub (Jan 14, 2026): Exponential backoff seems way overkill. To state the obvious: exponential backoff means OpenCode waits TWICE THE LENGTH OF TIME it waited the last time on every retry. This means, after only 7 retries, you wait over a minute. On the 8th retry, you wait 2 minutes. I think it should be linear, such as retry after 1 second, then 2 seconds, then 3 seconds, ... up to some kind of user configurable maximum such as 10 or 15 seconds. "Exponential backoff" seems to be the go-to solution for everything nowadays. Even AI suggests it **strongly** to me for anything and everything network-related, indiscriminately. I don't know where that came from ... perhaps the 1980s, when networks were really unreliable, and retrying a network backup or something made sense to eventually take hours to make a retry attempt. But nowadays? In 2026? Exponential? Maybe in some low priority cloud services such as backups (even then, why exponential and not linear?), but it really hits the user hard in terms of experience in user-facing apps.
Author
Owner

@shaneqld commented on GitHub (Jan 27, 2026):

We have backoff for ratelimits, and we respect the retry-after type headers so once it is ready to retry it should automatically go through

Is it possible that this behaviour isn't consistent across providers? I've run into rate limits errors fairly frequently (multiple times per day) using OpenAI models and the agent loop completely stops (ie no retries).

In my case, it happens primarily using gpt-5.2-codex, either directly via OpenAI or via OpenCode Zen.

Example error when using OpenAI provider:

{"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5.2-codex in organization org-xyz on tokens per min (TPM): Limit 500000, Used 404104, Requested 107839. Please try again in 1.433s. Visit https://platform.openai.com/account/rate-limits to learn more.","param":null}}

Example error when using OpenCode Zen provider:

{"type":"error","sequence_number":2,"error":{"type":"server_error","code":"rate_limit_exceeded","message":"The model was unable to complete inference due to an internal error.","param":null}}
@shaneqld commented on GitHub (Jan 27, 2026): > We have backoff for ratelimits, and we respect the retry-after type headers so once it is ready to retry it should automatically go through Is it possible that this behaviour isn't consistent across providers? I've run into rate limits errors fairly frequently (multiple times per day) using OpenAI models and the agent loop completely stops (ie no retries). In my case, it happens primarily using `gpt-5.2-codex`, either directly via OpenAI or via OpenCode Zen. Example error when using OpenAI provider: ```json {"type":"error","sequence_number":2,"error":{"type":"tokens","code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5.2-codex in organization org-xyz on tokens per min (TPM): Limit 500000, Used 404104, Requested 107839. Please try again in 1.433s. Visit https://platform.openai.com/account/rate-limits to learn more.","param":null}} ``` Example error when using OpenCode Zen provider: ```json {"type":"error","sequence_number":2,"error":{"type":"server_error","code":"rate_limit_exceeded","message":"The model was unable to complete inference due to an internal error.","param":null}} ```
Author
Owner

@ownself commented on GitHub (Feb 1, 2026):

Is there a way to explicitly configurate RPM limit for certain model in OpenCode.json? Our API Provider lower the some Open sourced models that I want to use.
But right now it keeps hitting Request limit, sign..

@ownself commented on GitHub (Feb 1, 2026): Is there a way to explicitly configurate RPM limit for certain model in OpenCode.json? Our API Provider lower the some Open sourced models that I want to use. But right now it keeps hitting Request limit, sign..
Author
Owner

@alainivars commented on GitHub (Feb 7, 2026):

Is there a way to explicitly configure RPM limit for certain model in OpenCode.json? Our API Provider lower the some Open sourced models that I want to use. But right now it keeps hitting Request limit, sign..

Yes, I totally agree, we should be able to set by model, in the opencode.json something like
"model_name" :
...
"rateLimit": {
"maxRequests": 20,
"timeWindow": "1m"
}
}

@alainivars commented on GitHub (Feb 7, 2026): > Is there a way to explicitly configure RPM limit for certain model in OpenCode.json? Our API Provider lower the some Open sourced models that I want to use. But right now it keeps hitting Request limit, sign.. Yes, I totally agree, we should be able to set by model, in the opencode.json something like "model_name" : ... "rateLimit": { "maxRequests": 20, "timeWindow": "1m" } }
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1172