Skip to content

Instantly share code, notes, and snippets.

@zirkelc
Created September 24, 2025 14:14
Show Gist options
  • Select an option

  • Save zirkelc/b830c8da8c40eeab6fb9a92825221bd4 to your computer and use it in GitHub Desktop.

Select an option

Save zirkelc/b830c8da8c40eeab6fb9a92825221bd4 to your computer and use it in GitHub Desktop.
Retryable for Anthropic overloaded error
import type { LanguageModelV2 } from '@ai-sdk/provider';
import { APICallError } from 'ai';
import type { Retryable, RetryModel } from '../create-retryable-model.js';
import { isErrorAttempt, isResultAttempt } from '../create-retryable-model.js';
import { isObject, isString } from '../utils.js';
/**
* Fallback to a different model if Anthropic returns an "overloaded" error.
*
* ```
* HTTP 200 OK
* {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}
* ```
*/
export function overloaded(
model: LanguageModelV2,
options?: Omit<RetryModel, 'model'>,
): Retryable {
return (context) => {
const { current } = context;
if (isErrorAttempt(current)) {
const { error } = current;
if (
APICallError.isInstance(error) &&
isObject(error.data) &&
isObject(error.data.error) &&
isString(error.data.error.message) &&
error.data.error.message.toLowerCase() === 'content_filter'
) {
return { model, maxAttempts: 1, ...options };
}
}
if (isResultAttempt(current)) {
const { result } = current;
if (
result.response &&
isObject(result.response.body) &&
isObject(result.response.body.error) &&
isString(result.response.body.error.message) &&
result.response.body.error.message.toLowerCase() === 'overloaded'
) {
return { model, maxAttempts: 1, ...options };
}
}
return undefined;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment