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.
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:
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.
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:
/v1/items/ -> v1/items/$
/v1/items/7/ -> v1/items/(?P<pk>[^/.]+)/$
/v1/items/7.json -> v1/items/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$
/v1/items.json -> v1/items\.(?P<format>[a-z0-9]+)/?$
The detail route is a regex, not v1/items/<pk>/, and a format suffix produces a separate pattern. If you meter by pattern and want either URL to cost 10 quota units, set a weight of 10 on both patterns.
For a non-streaming LiteLLM call, the token count is on the response:
response = await litellm.acompletion(model=model, messages=messages)
tokens = response.usage.total_tokens
The order is the awkward part. Whether a customer may make the request has to be decided before the call, but the token count only exists after it. So before the first model call, reserve enough of the customer's quota for the whole API request. For one call, that's the full model input you accept, including system messages and tool definitions, plus max_tokens. If a request can make several calls, cap how many and reserve for all of them. When the request ends, settle against the actual usage. Checking the balance without reserving lets two concurrent requests both pass on the same remaining quota.
With mock_response=..., LiteLLM reported 10 prompt tokens and 20 completion tokens, 30 in total, for a one-word prompt and for one 200 words long. That's enough to check that your code reads usage, but it can't tell you how big a quota should be. Use real calls for that.
A tool loop that makes two model calls returns two usage objects, so add up total_tokens yourself. With mock_response=Exception("boom"), acompletion raised InternalServerError. If the second call in a loop fails like that, you still have the first call's token count. If you write your own meter, decide whether it counts.
I work on UsageRail, which handles the key check, the per-customer rate limit and the quota in a Django or FastAPI middleware. For DRF token auth, you set auth to custom and point consumer at a function like customer_id above. It adds a leading slash to the pattern from resolve(), so the detail route's weight goes on /v1/items/(?P<pk>[^/.]+)/$. For a token quota, set the app to count units and enter the most tokens one request can use as its "Reserve with track()" value under Settings > Route weights. Then request.usagerail.track(n) records a whole number from 0 to 10,000,000 for the current request. One detail from its source: track() keeps the last value, it doesn't add, so in a tool loop you add up total_tokens from each successful call and pass the sum once. If the API returns a 4xx or 5xx, the request counts zero units, whatever you passed to track(). The Django guide covers metering and rate limits for Django APIs, including a DRF section.
These results are from one machine and the versions above. Newer releases of DRF or LiteLLM may behave differently, so rerun the checks on yours.
I used AI to help draft this post.