Middleware that automatically retries failed model calls with configurable backoff.
Supports retrying on specific exceptions and exponential backoff.
modelRetryMiddleware(config: __type = {}): AgentMiddleware| Name | Type | Description |
|---|---|---|
config | __type | Default: {}Configuration options for the retry middleware |
import { createAgent, modelRetryMiddleware } from "langchain";
const agent = createAgent({
model: "openai:gpt-4o",
tools: [searchTool],
middleware: [modelRetryMiddleware()],
});import { modelRetryMiddleware } from "langchain";
const retry = modelRetryMiddleware({
maxRetries: 4,
retryOn: [TimeoutError, NetworkError],
backoffFactor: 1.5,
});function shouldRetry(error: Error): boolean {
// Only retry on rate limit errors
if (error.name === "RateLimitError") {
return true;
}
// Or check for specific HTTP status codes
if (error.name === "HTTPError" && "statusCode" in error) {
const statusCode = (error as any).statusCode;
return statusCode === 429 || statusCode === 503;
}
return false;
}
const retry = modelRetryMiddleware({
maxRetries: 3,
retryOn: shouldRetry,
});const retry = modelRetryMiddleware({
maxRetries: 4,
onFailure: "continue", // Return AIMessage with error instead of throwing
});const formatError = (error: Error) =>
`Model call failed: ${error.message}. Please try again later.`;
const retry = modelRetryMiddleware({
maxRetries: 4,
onFailure: formatError,
});const retry = modelRetryMiddleware({
maxRetries: 5,
backoffFactor: 0.0, // No exponential growth
initialDelayMs: 2000, // Always wait 2 seconds
});const retry = modelRetryMiddleware({
maxRetries: 2,
onFailure: "error", // Re-raise exception instead of returning message
});