All jobs

Add Rate Limiting to a Litestar API

Yes. An agent adds Litestar's rate limit middleware backed by your Redis, keys it per user or client address, tests the 429 and its headers, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how the application is assembled before it adds middleware. In an app like litestar-org/litestar-fullstack, pyproject.toml depends on litestar with its jwt, structlog and pydantic extras, advanced-alchemy, redis, litestar-saq, litestar-granian and litestar-vite. The application is not built in one file: src/py/app/server/core.py holds ApplicationCore, an InitPluginProtocol whose on_app_init sets the OpenAPI config, applies the JWT auth from domain/accounts/guards.py, sets app_config.cors_config and extends app_config.plugins with the structlog, Granian, SQLAlchemy, Vite, SAQ, problem-details, OAuth2, email and domain plugins from server/plugins.py. lib/settings.py reads every setting from the environment, including SAQ_REDIS_URL with a default of redis://localhost:16379/0, ALLOWED_CORS_ORIGINS and LITESTAR_TRUSTED_PROXIES. The access routes in domain/accounts/controllers/_access.py are POST /api/access/login, /logout, /refresh, /signup, /forgot-password and /reset-password, the first and last few marked exclude_from_auth=True. Two limits already exist at the application level: the password reset service refuses a fourth reset token per user within an hour, and the MFA challenge controller returns 429 after five failed attempts in fifteen minutes by counting audit log rows. Nothing limits requests per client.

Then it adds the middleware the framework ships. RateLimitConfig from litestar.middleware.rate_limit takes rate_limit as a unit and quota, such as ("minute", 120), and its middleware goes into app_config.middleware inside on_app_init, next to the plugins. The agent adds a RATE_LIMIT_PER_MINUTE field to AppSettings in lib/settings.py read with the same get_env helper, and an exclude list for /schema, the health route under domain/system and the /static/ assets Vite serves, so documentation and the SPA shell never count against a client. By default the middleware identifies a client by remote address, which behind a proxy is the proxy; the agent writes an identifier_for_request that returns the user id from the request when the JWT guard has set one and falls back to the address otherwise, and points the pull request at LITESTAR_TRUSTED_PROXIES for the unauthenticated case.

The counter must be shared or it is not a limit. The default store name is rate_limit and, unregistered, it resolves to a fresh in-memory store, which under Granian's workers means each worker counts on its own. The agent registers a RedisStore from litestar.stores.redis under that name in the app's stores, built from the same SAQ_REDIS_URL the queue already uses, so every worker reads one counter and a restart does not reset it. With set_rate_limit_headers left at its default, each response carries RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset and RateLimit-Policy, and the pull request shows one response's headers.

The tests follow the repository's own shape. src/py/tests/integration/conftest.py provides an AsyncTestClient fixture named client, and pyproject.toml runs the suite with coverage that must stay above 65 percent. A new tests/integration/system/routes/test_rate_limit.py sets a quota of three through the settings, sends four requests to /api/access/login, asserts the fourth is 429 with the four headers, and checks that a request to the health route is never limited. The agent runs make lint, which covers ruff, mypy, pyright, slotscheck, biome and codespell, then uv run pytest src/py/tests, and opens the pull request only when both pass.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add per-client rate limiting to this Litestar application. Read pyproject.toml, src/py/app/server/core.py, src/py/app/server/plugins.py, src/py/app/lib/settings.py, src/py/app/domain/accounts/controllers/_access.py, src/py/app/domain/system/controllers.py and src/py/tests/integration/conftest.py first. Add a RATE_LIMIT_PER_MINUTE setting to AppSettings with a sensible default, build a RateLimitConfig from it in ApplicationCore.on_app_init with exclude patterns for the schema, health and static asset routes, and an identifier_for_request that uses the authenticated user's id when present and the remote address otherwise. Register a RedisStore named rate_limit in the app's stores using the existing SAQ Redis URL so Granian workers share one counter. Leave the existing password reset and MFA limits as they are. Write an integration test that lowers the quota, asserts the request over it returns 429 with the RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset and RateLimit-Policy headers, and asserts the health route is never limited. Run make lint and the pytest suite until both pass, then open a pull request that shows one limited response's headers and explains the Redis store choice.",
  "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/py/app/lib/settings.py to add the quota setting, src/py/app/server/core.py to build the config, register the Redis store and append the middleware, and adds src/py/tests/integration/system/routes/test_rate_limit.py. The description shows the status line and four RateLimit-* headers of one 429 response from the test, lists the excluded routes, and explains why the store is Redis rather than the default in-memory one, with the environment variable it reads.

The run's record keeps the make lint output across ruff, mypy, pyright, slotscheck, biome and codespell, and the full pytest run with the coverage line, so a reviewer can see the suite still clears its 65 percent floor with the new test included.

Before merging, a reviewer sets the production quota in the environment, decides whether login and signup deserve a tighter limit than the rest of the API, which the pull request leaves as a follow-up with a route-level exclude_opt_key, and checks the proxy setting so unauthenticated clients are not all counted as one address.

Run it on every Linear issue labelled rate-limit

In the console, add an automation to the Project with this prompt and a linear trigger on linear.issue.labeled, filtered to the rate-limit label. When someone labels an issue such as "signup is being hammered from one address", the trigger puts the issue after the prompt and the run reads the quota and routes it names before writing the config. Only label changes by a person in your Rush org fire it, and the automation starts at most max_runs_per_hour runs in an hour.

From your own server, the same body goes to POST /o/{org}/sessions with the repository in resources and the issue URL beside it, read through your Linear connection, and a request_id that is a UUID v5 of the issue id, so a retried call returns the run already started rather than opening a second pull request.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

The mistake that ships is a rate limiter that works on a laptop: one process, one in-memory counter, every request from 127.0.0.1. The agent reads that this application runs under Granian workers behind a proxy with Redis already configured, and wires the shared store and the per-user identifier in the same change, with a test that produces a real 429 in its own sandbox.

The lint and test output stay with the run, so the pull request can quote the limited response's headers and the coverage line rather than describing them, and the reviewer's decisions are about quotas, not about whether the middleware is attached.

Frequently asked questions

Why Redis and not the default store?

The default rate_limit store is created in memory per process, so each Granian worker would count separately and a restart would reset every counter. The application already has Redis for SAQ at SAQ_REDIS_URL, so a RedisStore registered under the same name gives one shared counter with no new infrastructure.

Will it interfere with the existing MFA and password reset limits?

No. Those limit a specific user's attempts over fifteen minutes and an hour by reading the database, and stay as they are. The middleware limits any client's request rate across the API, and a request refused by it never reaches those handlers.

What does a limited client see?

A 429 response with RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset and RateLimit-Policy headers, which follow the IETF draft and tell the client when to retry. Every allowed response carries the same headers with the remaining count, so a well-behaved client can slow down before being refused.

Can one route have a different limit?

The middleware applies one quota, and exclude and exclude_opt_key take routes out of it. A stricter limit on login alone is a second config or a guard on that controller, which the pull request names as a follow-up rather than adding unasked.

More Litestar jobs

The same job on other stacks

Keep reading