Surface upstream rate limits from Anthropic (#16118)

This PR makes it so hitting upstream rate limits from Anthropic result
in an HTTP 429 response instead of an HTTP 500.

To do this we need to surface structured errors out of the `anthropic`
crate.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-08-12 11:59:24 -04:00 committed by GitHub
parent fbb533b3e0
commit ebdb755fef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 144 additions and 47 deletions

View file

@ -197,7 +197,20 @@ async fn perform_completion(
request,
None,
)
.await?;
.await
.map_err(|err| match err {
anthropic::AnthropicError::ApiError(ref api_error) => {
if api_error.code() == Some(anthropic::ApiErrorCode::RateLimitError) {
return Error::http(
StatusCode::TOO_MANY_REQUESTS,
"Upstream Anthropic rate limit exceeded.".to_string(),
);
}
Error::Internal(anyhow!(err))
}
anthropic::AnthropicError::Other(err) => Error::Internal(err),
})?;
chunks
.map(move |event| {