langchain.js
    Preparing search index...

    Function modelRetryMiddleware

    • Middleware that automatically retries failed model calls with configurable backoff.

      Supports retrying on specific exceptions and exponential backoff.

      Parameters

      • config: {
            backoffFactor?: number;
            initialDelayMs?: number;
            jitter?: boolean;
            maxDelayMs?: number;
            maxRetries?: number;
            onFailure?:
                | "continue"
                | "error"
                | ((...args: [Error, ...unknown[]]) => string);
            retryOn?:
                | ((...args: [Error, ...unknown[]]) => boolean)
                | (new (...args: any[]) => Error)[];
        } = {}

        Configuration options for the retry middleware

        • OptionalbackoffFactor?: number

          Multiplier for exponential backoff. Each retry waits initialDelayMs * (backoffFactor ** retryNumber) milliseconds. Set to 0.0 for constant delay. Default is 2.0.

        • OptionalinitialDelayMs?: number

          Initial delay in milliseconds before first retry. Default is 1000 (1 second).

        • Optionaljitter?: boolean

          Whether to add random jitter (±25%) to delay to avoid thundering herd. Default is true.

        • OptionalmaxDelayMs?: number

          Maximum delay in milliseconds between retries. Caps exponential backoff growth. Default is 60000 (60 seconds).

        • OptionalmaxRetries?: number

          Maximum number of retry attempts after the initial call. Default is 2 retries (3 total attempts). Must be >= 0.

        • OptionalonFailure?: "continue" | "error" | ((...args: [Error, ...unknown[]]) => string)

          Behavior when all retries are exhausted. Options:

          • "continue" (default): Return an AIMessage with error details, allowing the agent to potentially handle the failure gracefully.
          • "error": Re-raise the exception, stopping agent execution.
          • Custom function: Function that takes the exception and returns a string for the AIMessage content, allowing custom error formatting.
        • OptionalretryOn?:
              | ((...args: [Error, ...unknown[]]) => boolean)
              | (new (...args: any[]) => Error)[]

          Either an array of error constructors to retry on, or a function that takes an error and returns true if it should be retried. Default is to retry on all errors.

      Returns AgentMiddleware

      A middleware instance that handles model failures with retries

      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
      });