Middleware that automatically retries failed tool calls with configurable backoff.
Supports retrying on specific exceptions and exponential backoff.
toolRetryMiddleware(config: __type = {}): AgentMiddleware| Name | Type | Description |
|---|---|---|
config | __type | Default: {}Configuration options for the retry middleware |
import { createAgent, toolRetryMiddleware } from "langchain";
const agent = createAgent({
model: "openai:gpt-4o",
tools: [searchTool],
middleware: [toolRetryMiddleware()],
});import { toolRetryMiddleware } from "langchain";
const retry = toolRetryMiddleware({
maxRetries: 4,
retryOn: [TimeoutError, NetworkError],
backoffFactor: 1.5,
});function shouldRetry(error: Error): boolean {
// Only retry on 5xx errors
if (error.name === "HTTPError" && "statusCode" in error) {
const statusCode = (error as any).statusCode;
return 500 <= statusCode && statusCode < 600;
}
return false;
}
const retry = toolRetryMiddleware({
maxRetries: 3,
retryOn: shouldRetry,
});const formatError = (error: Error) =>
"Database temporarily unavailable. Please try again later.";
const retry = toolRetryMiddleware({
maxRetries: 4,
tools: ["search_database"],
onFailure: formatError,
});import { tool } from "@langchain/core/tools";
import { z } from "zod";
const searchDatabase = tool(
async ({ query }) => {
// Search implementation
return results;
},
{
name: "search_database",
description: "Search the database",
schema: z.object({ query: z.string() }),
}
);
const retry = toolRetryMiddleware({
maxRetries: 4,
tools: [searchDatabase], // Pass BaseTool instance
});const retry = toolRetryMiddleware({
maxRetries: 5,
backoffFactor: 0.0, // No exponential growth
initialDelayMs: 2000, // Always wait 2 seconds
});const retry = toolRetryMiddleware({
maxRetries: 2,
onFailure: "raise", // Re-raise exception instead of returning message
});