{"slug": "per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts", "title": "Per-customer limits in a Python API: DRF's auth order and LiteLLM's token counts", "summary": "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.", "body_md": "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.\n\nI 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).\n\nI 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.\n\nThe 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.\n\nThe fix I'd use is to run the authentication class yourself in the middleware:\n\n``` python\nfrom rest_framework.authentication import TokenAuthentication\nfrom rest_framework.exceptions import AuthenticationFailed\n\ndef customer_id(request):\n    try:\n        result = TokenAuthentication().authenticate(request)\n    except AuthenticationFailed:\n        return None\n    if result is None:\n        return None\n    user, _token = result\n    return str(user.pk)\n```\n\nIn 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).\n\n`resolve()` returns for a router\nIf 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:\n\n``` php\n/v1/items/       -> v1/items/$\n/v1/items/7/     -> v1/items/(?P<pk>[^/.]+)/$\n/v1/items/7.json -> v1/items/(?P<pk>[^/.]+)\\.(?P<format>[a-z0-9]+)/?$\n/v1/items.json   -> v1/items\\.(?P<format>[a-z0-9]+)/?$\n```\n\nThe 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.\n\nFor a non-streaming LiteLLM call, the token count is on the response:\n\n```\nresponse = await litellm.acompletion(model=model, messages=messages)\ntokens = response.usage.total_tokens\n```\n\nThe 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.\n\nWith `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.\n\nA 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.\n\nI 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](https://usagerail.com/docs/django), including a DRF section.\n\nThese results are from one machine and the versions above. Newer releases of DRF or LiteLLM may behave differently, so rerun the checks on yours.\n\n*I used AI to help draft this post.*", "url": "https://wpnews.pro/news/per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts", "canonical_source": "https://dev.to/jackmally/per-customer-limits-in-a-python-api-drfs-auth-order-and-litellms-token-counts-1blf", "published_at": "2026-11-20 16:31:35+00:00", "updated_at": "2026-11-20 16:44:35.334296+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "large-language-models"], "entities": ["Django REST framework", "LiteLLM", "Django", "Hono", "FastAPI"], "also_reported_by": [], "takeaway": "In DRF, run TokenAuthentication().authenticate(request) inside middleware to get the customer id, and reserve input tokens plus max_tokens before each LiteLLM call, settling against response.usage.total_tokens afterward.", "recommendation": {"advice": "Reserve quota for the full prompt plus max_tokens before each LiteLLM call and settle against usage.total_tokens after, so concurrent requests can't both pass on the same remaining balance.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts", "markdown": "https://wpnews.pro/news/per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts.md", "text": "https://wpnews.pro/news/per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts.txt", "jsonld": "https://wpnews.pro/news/per-customer-limits-in-a-python-api-drf-s-auth-order-and-litellm-s-token-counts.jsonld"}}