Per-customer limits in a Python API: DRF's auth order and LiteLLM's token counts A developer documented two pitfalls encountered while adding per-customer rate limits to a Python API built on Django REST framework and LiteLLM: middleware placed after Django's AuthenticationMiddleware sees AnonymousUser for token-authenticated requests because DRF resolves the token only when the view runs, and quota checks must reserve tokens before a model call since usage is only known afterward. The writeup, tested on Django 6.1.3, DRF 3.18.3 and LiteLLM 1.110.2, recommends invoking TokenAuthentication directly inside the middleware and reserving the full input plus max_tokens up front, settling against actual usage when the request ends. In a 40-line API key and rate limit middleware for Hono https://usagerail.hashnode.dev/api-keys-and-per-customer-rate-limits-for-a-hono-api , the handler already knows which customer is calling and that they're under their limit. Doing the same in a Python API with Django REST framework and an LLM behind it, I hit two snags. In my DRF test, a middleware saw AnonymousUser even when the request had a valid token. And with LiteLLM, the quota check has to happen before the model returns its token count. I ran both cases locally with Django 6.1.3, DRF 3.18.3 and LiteLLM 1.110.2. For the full FastAPI endpoint, see the example of how to count LiteLLM token usage per customer https://github.com/BerriAI/litellm/discussions/50640 . I set DRF's default authentication to TokenAuthentication and added a middleware after Django's AuthenticationMiddleware . Then I sent a request with a valid Authorization: Token ... header. The view saw the user acme . The middleware saw AnonymousUser . Django's own middleware only knows about session logins. DRF reads the token later, when the view runs. If the caller has a DRF token and no Django login session, a per-customer limit that reads request.user in middleware sees AnonymousUser for every request. The fix I'd use is to run the authentication class yourself in the middleware: python from rest framework.authentication import TokenAuthentication from rest framework.exceptions import AuthenticationFailed def customer id request : try: result = TokenAuthentication .authenticate request except AuthenticationFailed: return None if result is None: return None user, token = result return str user.pk In the test, this returned acme 's id in the middleware. When the request reaches the view, DRF looks the token up a second time. A 429 sent from that middleware never reaches DRF's exception handler, so a custom error format has to be set in the middleware itself. I covered that in a DRF thread on putting per-customer quota checks in a plain Django middleware in front of DRF https://github.com/encode/django-rest-framework/discussions/10098 . resolve returns for a router If you count usage per endpoint, you need a stable name for each one. Django's resolve path .route gives you the pattern that matched. With a DefaultRouter registered as items under path "v1/", include router.urls , these were the routes: php /v1/items/ - v1/items/$ /v1/items/7/ - v1/items/ ?P