> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/main-components/middleware/retry.md).

# RetryMiddleware

Retry failed LLM and tool calls with exponential backoff controls

Class: `bxModules.bxai.models.middleware.core.RetryMiddleware`

Wraps both LLM and tool invocations and retries failures with exponential backoff.

## Features

* Retries around `wrapLLMCall`
* Retries around `wrapToolCall`
* Exponential backoff with maximum delay cap
* Skip retries for configured exception types
* Logs retry attempts and terminal failure

## Constructor

```javascript
middleware = new bxModules.bxai.models.middleware.core.RetryMiddleware(
    maxRetries       : 3,
    initialDelay     : 1000,
    backoffMultiplier: 2,
    maxDelay         : 30000,
    nonRetryableTypes: "InvalidInput,MaxInteractionsExceeded"
)
```

## Configuration

| Option              | Type    | Default                                  | Description                                       |
| ------------------- | ------- | ---------------------------------------- | ------------------------------------------------- |
| `maxRetries`        | numeric | `3`                                      | Retry attempts after initial failure              |
| `initialDelay`      | numeric | `1000`                                   | First backoff delay in milliseconds               |
| `backoffMultiplier` | numeric | `2`                                      | Multiplier for each subsequent delay              |
| `maxDelay`          | numeric | `30000`                                  | Maximum backoff delay in milliseconds             |
| `nonRetryableTypes` | string  | `"InvalidInput,MaxInteractionsExceeded"` | Comma-delimited exception types to bypass retries |

## Hooks Used

* `wrapLLMCall`
* `wrapToolCall`

## Example

```javascript
agent = aiAgent(
    name      : "resilient-agent",
    middleware: [
        new bxModules.bxai.models.middleware.core.RetryMiddleware(
            maxRetries       : 5,
            initialDelay     : 500,
            backoffMultiplier: 1.5,
            maxDelay         : 10000,
            nonRetryableTypes: "InvalidInput,BXAI.SecurityViolation"
        )
    ]
)
```

## Notes

* Retries are applied to exceptions only; successful responses are returned immediately.
* Non-retryable exception types are matched case-insensitively via list lookup.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/main-components/middleware/retry.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
