In a 40-line API key and rate limit middleware for Hono , 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 .
DRF: the middleware sees an anonymous user
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.…