Add retry support for OpenAI-compatible LLM providers (#37891)

Automatically retry the agent's LLM completion requests when the
provider returns 429 Too Many Requests. Uses the Retry-After header to
determine the retry delay if it is available.

Many providers are frequently overloaded or have low rate limits. These
providers are essentially unusable without automatic retries.

Tested with Cerebras configured via openai_compatible.

Related: #31531 

Release Notes:

- Added automatic retries for OpenAI-compatible LLM providers

---------

Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
This commit is contained in:
Tim McLean
2025-11-13 14:15:46 +00:00
committed by GitHub
co-authored by Bennet Bo Fenner
parent 92e64f9cf0
commit fb90b12073
9 changed files with 114 additions and 46 deletions
@@ -345,6 +345,27 @@ impl From<anthropic::ApiError> for LanguageModelCompletionError {
}
}
impl From<open_ai::RequestError> for LanguageModelCompletionError {
fn from(error: open_ai::RequestError) -> Self {
match error {
open_ai::RequestError::HttpResponseError {
provider,
status_code,
body,
headers,
} => {
let retry_after = headers
.get(http::header::RETRY_AFTER)
.and_then(|val| val.to_str().ok()?.parse::<u64>().ok())
.map(Duration::from_secs);
Self::from_http_status(provider.into(), status_code, body, retry_after)
}
open_ai::RequestError::Other(e) => Self::Other(e),
}
}
}
impl From<OpenRouterError> for LanguageModelCompletionError {
fn from(error: OpenRouterError) -> Self {
let provider = LanguageModelProviderName::new("OpenRouter");