All jobs

Add Throttling to a Django REST Framework API from a Linear Issue

An agent adds DRF throttling, 20 a minute anonymous and 1,000 a day signed in, plus a per-IP login scope, with pytest going from 40 to 44 passed.

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 REST_FRAMEWORK in config/django/base.py, the locmem CACHES in config/django/test.py, authentication/apis.py, api/mixins.py and the JWT package's views. It adds AnonRateThrottle and UserRateThrottle as defaults with rates 20/min and 1000/day, and a login rate of 5/min.

A new LoginRateThrottle in styleguide_example/api/throttling.py subclasses SimpleRateThrottle and keys on the client IP whether or not the caller is signed in, so a logged-in client cannot sidestep it. Both UserSessionLoginApi and UserJwtLoginApi set it as their only throttle. The tests live in api/tests/test_throttling.py, and a root conftest.py clears the cache before each test.

Start it with one API call

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

{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add Throttling to a Django REST Framework API. Add DRF throttling: 20 requests per minute for anonymous users and 1,000 per day for authenticated ones, with a stricter scope on the login endpoint.\" Do the work in this repository, add tests for it, run pytest, and open a pull request that links back to the issue.",
  "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

Tried on HackSoftware/Django-Styleguide-Example at a70ef43: 5 files changed, 110 insertions(+) in config/django/base.py, conftest.py, styleguide_example/api/throttling.py, its new tests and styleguide_example/authentication/apis.py. With Postgres 15 started in the sandbox, pytest -q went from 40 passed, 14 warnings in 1.89s to 44 passed, 15 warnings in 1.66s. The run worked from the issue text quoted in the request; there was no Linear issue to link.

For the reviewer: the issue named no login rate, so 5/min is the agent's choice. No CACHES is set outside tests, so each gunicorn worker keeps its own counts in local memory and the real limit multiplies; the limits need a shared cache such as Redis to hold.

Run it on every Linear issue labeled agent

An automation with a linear trigger on linear.issue.labeled and the label agent runs the linear-label-to-pr recipe's prompt when someone in your Rush org labels an issue. Rate limits usually arrive as a security ask; an issue that names the login rate or the endpoints to cover passes those numbers to the agent.

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

Why run it on Rush?

The settings change is nine lines; what this run caught is around it. Its first full run failed the existing JWT login test, because throttle counts leaked between tests through the cache, which conftest.py now clears. And it read the settings to find that production has no shared cache, the gap that would quietly multiply every limit.

Frequently asked questions

How does the test check 1,000 a day without 1,000 requests?

It seeds the user's throttle history in the cache with 1,000 timestamps, expects the next request to get 429, and checks that a second user still gets 200.

What did pytest need on the untouched clone?

A database. Without Postgres it ended 2 passed, 4 warnings, 38 errors in 5.07s on a socket error. The run also installed psycopg2-binary in place of psycopg2, since pg_config was missing, without changing any repository file.

More Django REST Framework jobs

More jobs like this: Turn a Linear label into a PR

Keep reading