# Add Rate Limiting to an Axum API

Yes. An agent reads your Axum routers, adds a per-client rate limit to the login and registration handlers as a tower layer, tests it, and opens a pull request.

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

## What the agent does

The agent starts from the manifest and the router, because the right rate limiter depends on which Axum this is. In an API like launchbadge/realworld-axum-sqlx, `Cargo.toml` pins `axum` 0.3.4 with the `tower-log` feature, `tower` 0.4.11, `tower-http` 0.2.0 with `trace`, `tokio` 1.14 and `sqlx` 0.5 on Postgres, and `Cargo.lock` has not moved from those. That rules out the crates written for later Axum versions, so the agent plans a small keyed limiter of its own rather than a dependency bump. It reads `src/http/mod.rs`, where `serve` wraps `api_router()` in a `ServiceBuilder` with `AddExtensionLayer::new(ApiContext { config, db })` and `TraceLayer::new_for_http()` and binds `0.0.0.0:8080`, and `src/http/users.rs`, whose router has `POST /api/users`, `POST /api/users/login` and `GET` and `PUT /api/user`. The two `POST` routes take a password and are the ones worth limiting; the article, comment and profile routes under `src/http/articles/` and `src/http/profiles.rs` stay unlimited.

The change is one module and three edits. The agent adds `src/http/ratelimit.rs` with a tower `Layer` and `Service` that keeps a token bucket per client address in a shared map, keyed on the peer address from `ConnectInfo<SocketAddr>` or the first entry of `X-Forwarded-For` when a `trust_proxy` flag is set, and refuses with `429` once a bucket is empty. The limit and window become two new fields on `Config` in `src/config.rs`, read from the environment through `clap` exactly like `database_url` and `hmac_key`, with defaults and a line each in `.env.sample`. In `src/http/error.rs` it adds a `TooManyRequests` variant to the `Error` enum beside `Unauthorized`, `Forbidden`, `NotFound` and `UnprocessableEntity`, mapped to `429` with a `Retry-After` header so the body shape matches the rest of the API. In `users.rs` it splits the two unauthenticated `POST` routes into their own `Router`, applies the layer there, and merges it back, so nothing else gains the limiter.

Compiling needs a database, because every handler uses the sqlx query macros, which check their SQL against a live schema at build time. The agent does what `.github/workflows/rust.yml` does: starts Postgres in the sandbox, writes `.env` from `.env.sample`, installs `sqlx-cli` with the `postgres` feature and runs `sqlx db setup`, which applies the four files in `migrations/`. Then it runs `cargo build`, `cargo clippy` and `cargo fmt --check`. The repository has no `tests/` directory, so the agent adds unit tests inside `ratelimit.rs` for the bucket itself, with a mocked clock, and one integration test under `tests/` that boots the router with the limiter set to a small number and asserts the response code flips to `429` on the next request and back after the window.

Last it runs the check the workflow trusts most: the RealWorld Postman collection, `run-api-tests.sh` with `APIURL` pointing at `cargo run` on port 8080. The collection logs in many times in a row, so the agent sets the default limit high enough that the suite passes untouched and says so in the pull request, where the reviewer picks the production number. It pushes a branch and opens the pull request with the diff, the commands it ran and their output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add per-client rate limiting to the unauthenticated routes of this Axum API. Read Cargo.toml and Cargo.lock first and respect the pinned axum, tower and tower-http versions: do not upgrade them. Read src/http/mod.rs, src/http/users.rs, src/http/error.rs and src/config.rs. Write a tower Layer and Service in src/http/ratelimit.rs that keeps a token bucket per client address, taking the address from ConnectInfo or from X-Forwarded-For when a trust_proxy config flag is set. Add the limit and window to Config via clap environment variables with sensible defaults and document them in .env.sample. Add a TooManyRequests variant to the Error enum that answers 429 with Retry-After. Apply the layer only to POST /api/users and POST /api/users/login. Start Postgres in the sandbox, run sqlx db setup as .github/workflows/rust.yml does, then cargo build, cargo clippy, cargo fmt --check and cargo test, and run the RealWorld Postman collection against cargo run on port 8080. Add unit tests for the bucket and one integration test for the 429 path. Open a pull request with the commands you ran and their output. Change nothing unrelated.",
  "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 on its own branch. The diff adds `src/http/ratelimit.rs`, touches `src/http/mod.rs` for the module declaration and the `into_make_service_with_connect_info` call that makes the client address available, `src/http/users.rs` for the split router, `src/http/error.rs` for the `TooManyRequests` variant and `src/config.rs` and `.env.sample` for the two new settings. A `tests/` directory appears with the integration test. The description lists the default limit and window and says which routes are limited and which are not.

The proof travels with the pull request. The repository's `Rust` workflow runs on it: Postgres service, `sqlx db setup`, `cargo build`, `cargo test` and the Postman collection against the running server. The session transcript, read at `GET /o/{org}/sessions/{id}/messages`, keeps every `cargo` command the agent ran in the sandbox with its full output, including the clippy pass and the collection's results.

Before merging, a reviewer checks three things: that the limiter keys on the address the deployment actually sees, which is the proxy's unless `trust_proxy` is on; that the `429` body matches the `errors` map shape the RealWorld spec expects from the other error variants; and that the defaults suit production rather than the test suite. A follow-up message to the same session changes any of them on the same branch.

## Run it on every terminal hand-off

This is the kind of change you start before leaving your desk. One `POST /o/{org}/sessions` with this body and your repository in `resources` starts the run in its own sandbox, and `GET /o/{org}/sessions/{id}/events` streams it live while you watch; close the laptop and the run keeps going. When the turn ends the sandbox parks with its disk and memory, so a follow-up through `POST /o/{org}/sessions/{id}/messages` wakes it with the Postgres container, the compiled target directory and the branch still there.

Give the key a `webhook_url` and Rush POSTs `run.completed` to your server when the turn finishes, with the result and the `session_url`, so the pull request link reaches you without polling. Send a `request_id` with the request and a retried call returns the run it already started instead of a second one.

## Why run it on Rush?

On a laptop this job stalls on setup before any code is written: a Postgres instance, `sqlx-cli`, a `.env` that matches, and a first `cargo build` of `sqlx` and `tokio` from scratch. In a Rush sandbox the agent does all of that in its own clone and its own database, leaves your working tree alone, and keeps the compiler output from every attempt, so the pull request arrives with the build already proven rather than with a note asking you to try it.

The result is checkable rather than asserted. The pull request carries the limiter, its tests and the config lines; the repository's own workflow runs the Postman collection against it; and the transcript shows each `cargo clippy` and `cargo test` invocation and what it printed. If the first default was wrong, one follow-up message adjusts it on the same branch without starting over.

## Frequently asked questions

### Why not just add tower-governor or a similar crate?

Because the manifest pins `axum` 0.3.4 and `tower` 0.4, and the ready-made Axum rate limiting crates target newer Axum releases. Pulling one in would force a framework upgrade into a pull request that is supposed to be about rate limiting. The agent writes a small keyed limiter against the `tower` traits the project already uses, and the pull request says so, so a later upgrade can swap it out.

### Which routes get the limit?

Only `POST /api/users` and `POST /api/users/login` in `src/http/users.rs`, the two routes that accept a password without a token. Articles, comments, profiles, tags and the authenticated `GET` and `PUT /api/user` are untouched. If you want the feed or article creation limited too, say so in the prompt and the agent puts those routes behind a second, looser bucket.

### Will it work behind a load balancer?

The limiter keys on the peer address by default, which behind a proxy is the proxy itself. The new `trust_proxy` flag on `Config` switches it to the first address in `X-Forwarded-For`. Leave it off unless every request reaches the API through a proxy you control, because a client can otherwise forge that header to escape the limit.

### Does the RealWorld Postman collection still pass?

The collection logs in repeatedly from one address, so a tight default would make it fail. The agent sets the default high enough for the suite and reports the number it chose; the repository's `Rust` workflow runs the same collection on the pull request, so the result is visible there, not only in the sandbox.

### What if cargo build fails on the query macros?

The sqlx query macros need `DATABASE_URL` to point at a migrated database at compile time. The agent follows `.github/workflows/rust.yml`: start Postgres, `cargo install sqlx-cli --features postgres`, `sqlx db setup`. If the database still cannot be reached, the run ends without a pull request and its final message says exactly which step failed.

## More Axum jobs

- [Add Refresh Tokens to an Axum JWT API](https://getrush.ai/run/add-refresh-tokens/axum/)
- [Add Request Timeouts to an Axum API](https://getrush.ai/run/add-request-timeouts/axum/)
- [Add Request Tracing to an Axum API from a Linear Issue](https://getrush.ai/run/add-request-tracing/axum/)

## The same job on other stacks

- [Add Rate Limiting to better-auth Sign-In](https://getrush.ai/run/add-rate-limiting/better-auth/)
- [Add Rate Limiting to a Go chi API](https://getrush.ai/run/add-rate-limiting/chi/)
- [Add Rate Limiting to a Cloudflare Worker](https://getrush.ai/run/add-rate-limiting/cloudflare-workers/)

## 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/)
