# Add Retry Logic to a Shared Axios Client

Yes. An agent adds axios-retry to a shared Axios instance for network errors and 5xx on GET, keeps mutations unretried, and proves it against the MSW server.

[Run this on Rush](https://cloud.getrush.ai/run/add-retry-logic/axios/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

In a React app laid out like alan2207/bulletproof-react, `apps/react-vite/src/lib/api-client.ts` builds one instance with `Axios.create({ baseURL: env.API_URL })` (`axios` is `1.6.8` in `yarn.lock`). A request interceptor sets `Accept` and `withCredentials`; a response interceptor returns `response.data` and, on error, pushes an error notification through `useNotifications.getState().addNotification` and sends the browser to the login page on a 401. Sixteen call sites use it: twelve in `src/features/*/api` (`api.get`, `api.post`, `api.patch`, `api.delete` for discussions, comments, users and teams) and four in `src/lib/auth.tsx` (`/auth/me`, `/auth/login`, `/auth/register`, `/auth/logout`). `src/lib/react-query.ts` sets `retry: false` for every query, so a transient failure today is one toast and a dead screen. Tests run under Vitest with MSW: `src/testing/setup-tests.ts` starts the mock server with `onUnhandledRequest: 'error'`, and the handlers in `src/testing/mocks/handlers` wait `networkDelay()`, 200 ms under test.

The agent adds `axios-retry` (its current release's peer range is `axios 0.x || 1.x`) and attaches it to the instance in `api-client.ts` with a `retryCondition` of its own: a network error, or a 5xx on a request whose method is `get`. The library's default condition also retries `PUT` and `DELETE` as idempotent; this app's `deleteDiscussion` and `deleteComment` are `DELETE` calls where a retry after a server that already acted answers 404, so they stay unretried along with every `POST` and `PATCH`. `retryDelay` is `axiosRetry.exponentialDelay`, since the library's default is no delay at all, and the library's `retries` default stays. Ordering is the part that matters: Axios runs response interceptors in the order they were added, so `axiosRetry(api, ...)` is called before `api.interceptors.response.use(...)`, otherwise every failed attempt would fire the notification before the retry ran. With the retry first, the toast and the 401 redirect see only the final outcome.

The proof is a test at `src/lib/__tests__/api-client.test.ts` using the mock server already in place. One case registers a handler for a `GET` that answers `HttpResponse.error()` for the first attempts and succeeds after, and asserts the promise resolves, the handler was called more than once, and `useNotifications.getState().notifications` is empty. A second case answers a `POST` with status 500 once and asserts the promise rejects, the handler ran exactly once, and one error notification was added. A third answers a `GET` 401 and asserts no retry happened before the redirect. Each uses `server.use` so the override is reset by `afterEach` in the setup file. The agent runs `yarn test`, `yarn lint` and `yarn check-types`, the three steps the `all-cli-checks` job in `.github/workflows/react-vite-ci.yml` runs.

What it leaves alone: `retry: false` in `src/lib/react-query.ts`, since the retry now lives in the transport and a second layer in React Query would multiply attempts; the feature API functions, which call `api` exactly as before; and the e2e suite, which drives the mock server through `@mswjs/http-middleware` and needs no change.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add retry logic to the shared Axios client in apps/react-vite of this React app. Read src/lib/api-client.ts, src/lib/react-query.ts, src/lib/auth.tsx, every file under src/features/*/api, src/testing/setup-tests.ts, src/testing/mocks/utils.ts and .github/workflows/react-vite-ci.yml first. Install axios-retry and attach it to the instance before the existing response interceptor, with exponential delay and a retry condition limited to network errors and 5xx responses on GET requests; never retry POST, PATCH or DELETE, and say why in the pull request. Keep React Query's retry: false. Add src/lib/__tests__/api-client.test.ts using the MSW server from the test setup: a GET that fails then succeeds resolves with no notification, a POST that answers 500 rejects after one attempt with one notification, and a 401 is not retried. Run yarn test, yarn lint and yarn check-types and quote the results. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `src/lib/api-client.ts`, adds `axios-retry` to `apps/react-vite/package.json` and `yarn.lock`, and adds one test file. No feature file changes. The description shows the interceptor order and says why it matters, lists which methods retry and which do not with the reason for `DELETE`, and quotes the `yarn test`, `yarn lint` and `yarn check-types` results.

The run's record keeps the test runs, so a reviewer can see the retried handler's call count and the empty notification store in the test output rather than infer them from the diff.

Before merging, a reviewer decides two things the pull request leaves as written: whether the retry count and delay suit their API's recovery time, and whether any `GET` in the app is expensive enough that retrying it under load is worse than failing fast. Both are one option on the `axiosRetry` call.

## Run it on every repository with a shared Axios client

A team with several front ends built on the same pattern runs this once per repository from a handler on their own server: for each repository it calls `POST /o/{org}/sessions` with this page's body and that repository's URL in `resources`, with a `request_id` that is a UUID v5 of the repository name so a re-run of the script returns the runs already started instead of opening duplicates.

Each run reads that repository's own client and call sites, so an app whose mutations use `PUT` gets them excluded for the same reason as `DELETE` here, and an app whose client has no error interceptor gets a simpler change. The key's `run.completed` webhook reports each run, with its answer, when it finishes.

## Why run it on Rush?

Retry logic is easy to add and easy to get subtly wrong: retried mutations, a toast on every attempt, a redirect loop on 401. The agent reads every call site before deciding what retries, and proves the ordering with a mock server that can fail on demand, which a laptop pointed at a real API cannot do on cue.

The tests and their output stay in the run's record and the pull request quotes them, so review is checking a retry condition against the list of methods rather than reasoning about interceptor order from memory. A team with several apps on the same client pattern runs the job on each at once, one sandbox and one pull request each.

## Frequently asked questions

### Why retry only GET when axios-retry would also retry PUT and DELETE?

Because this app's `DELETE` calls act on a single discussion or comment, and a retry after a server that deleted it but failed to answer comes back as a 404 the user sees as an error. The library's default treats `PUT` and `DELETE` as idempotent in the HTTP sense; the agent narrows it to `GET` for this app and says so, and a reviewer who wants `DELETE` back changes one condition.

### Will users see a toast for each failed attempt?

No. The retry interceptor is attached before the app's error interceptor, and Axios runs response interceptors in order, so the notification and the 401 redirect only see the final result. One of the new tests asserts the notification store is empty after a request that failed and then succeeded.

### Should React Query retry as well?

Not on top of this. `retry: false` in `src/lib/react-query.ts` stays, so a query fails once as far as React Query is concerned while the transport underneath it has already tried more than once. Two layers would multiply the attempts and the delay.

### Does this change the e2e tests?

No. The Playwright suite drives the mock server through `@mswjs/http-middleware` and never fails a request on purpose, so it runs as before. The retry behavior is proved by the Vitest cases against the in-process MSW server.

## More Axios jobs

- [Fix "Package subpath './lib/adapters/http' is not defined by \"exports\"" after upgrading axios](https://getrush.ai/run/fix-package-subpath-is-not-defined-by-exports/axios/)
- [Replace Axios With Native fetch in a Node.js Library](https://getrush.ai/run/migrate-axios-to-fetch/axios/)
- [Upgrade axios 0.x to 1.x in a React App](https://getrush.ai/run/upgrade-axios/axios/)

## The same job on other stacks

- [Add Retry Logic for Throttled Shopify Admin API Calls](https://getrush.ai/run/add-retry-logic/shopify-app/)

## More jobs like this: Hand off a long task

- [Add Retry Policies With Exponential Backoff to Airflow DAGs](https://getrush.ai/run/add-retry-policies/airflow/)
- [Add Retry Policies to the Flaky Assets in a Dagster Project](https://getrush.ai/run/add-retry-policies/dagster/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
