This release is a pre-release and may not be stable for production use.
django-openrouter
Reusable Django application for managing OpenRouter models, usage profiles, budgets and request logs from Django admin.
Your product code (translation, chat, summarization, …) talks to OpenRouter through this library instead of hardcoded settings.py keys and model ids. An operator picks active models, sets daily/monthly limits, and inspects spend in admin.
Why not settings.py?
Hardcoding OPENROUTER_MODEL = "anthropic/claude-3.5-sonnet" means a deploy to change models, budgets or a kill switch. django-openrouter stores that runtime policy in the database, caches it, and enforces limits before every request.
API keys can still come from the environment (OPENROUTER_API_KEY) if you prefer not to keep them in the database.
Quickstart
1. Install
pip install django-openrouter
2. Enable the app and migrate
# settings.py
INSTALLED_APPS = [
...,
"django_openrouter",
]
python manage.py migrate
3. Configure in Django admin
- Open OpenRouter → OpenRouter settings. Paste an API key or leave it empty and set
OPENROUTER_API_KEYin the environment. - Run
python manage.py sync_modelsto pull the model catalog (GET /api/v1/models). - Create a Usage profile (slug
translation,chat, …), pick a model, optional fallbacks, limits and budgets. - Set that profile as default profile if you want
chat(messages=...)without a name.
Usage
from django_openrouter import chat
result = chat(
"translation",
messages=[{"role": "user", "content": "Translate to French: hello"}],
)
print(result.content)
print(result.model_used, result.cost_usd, result.latency_ms)
Sync client:
from django_openrouter import OpenRouterClient
client = OpenRouterClient("chat")
result = client.chat(messages=[{"role": "user", "content": "Hello"}])
Async:
from django_openrouter import achat
result = await achat("chat", messages=[{"role": "user", "content": "Hello"}])
If a profile’s limit or budget is exhausted, chat() raises BudgetExceeded or RateLimitExceeded and does not silently fall back. Fallback models are only used when OpenRouter returns 402, 429 or 5xx.
Streaming (stream=True) raises NotImplementedError in this MVP.
Management command
python manage.py sync_models
python manage.py sync_models --api-key sk-or-...
Upserts the catalog, never deletes rows, and sets is_active=False on models that disappeared from OpenRouter.
settings.OPENROUTER
| Key | Default | Description |
|---|---|---|
CACHE_TIMEOUT |
60 |
Seconds to keep runtime config in Django’s cache |
CACHE_ALIAS |
"default" |
Cache alias (django.core.cache) |
API_KEY |
unset | Fallback API key if the admin field is empty |
HTTP_REFERER |
unset | Sent as HTTP-Referer (OpenRouter app attribution) |
X_TITLE |
unset | Sent as X-Title |
Also accepted, in this order, when the admin API key is empty:
settings.OPENROUTER["API_KEY"]settings.OPENROUTER_API_KEY- Environment variable
OPENROUTER_API_KEY
Example:
OPENROUTER = {
"CACHE_TIMEOUT": 60,
"CACHE_ALIAS": "default",
"HTTP_REFERER": "https://my-app.example",
"X_TITLE": "My App",
}
The default cache backend (LocMemCache) is enough. Redis/Memcached are optional.
Limits
Usage is aggregated from RequestLog for the current calendar day and month (UsageProfile.get_usage("day"|"month")). Null limit fields mean “unlimited”.
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_openrouter-0.1.0b1.tar.gz.
File metadata
- Download URL: django_openrouter-0.1.0b1.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c287609b895bd9c42677f8e4e3380a2b74907a0d30e3af77c7c08ec815598bb5
|
|
| MD5 |
e6b1da8ec5cca15315584237fb47da38
|
|
| BLAKE2b-256 |
910059e1e683816530dafd16bb809e089fc169668415d0f0bf5f2caf025df483
|
Provenance
The following attestation bundles were made for django_openrouter-0.1.0b1.tar.gz:
Publisher:
publish.yml on svalench/django-openrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_openrouter-0.1.0b1.tar.gz -
Subject digest:
c287609b895bd9c42677f8e4e3380a2b74907a0d30e3af77c7c08ec815598bb5 - Sigstore transparency entry: 2708327013
- Sigstore integration time:
-
Permalink:
svalench/django-openrouter@767bfae65a5cdcc2c518c6be1ca8900eb5a21b7c -
Branch / Tag:
refs/tags/v0.1.0b1 - Owner: https://github.com/svalench
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@767bfae65a5cdcc2c518c6be1ca8900eb5a21b7c -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_openrouter-0.1.0b1-py3-none-any.whl.
File metadata
- Download URL: django_openrouter-0.1.0b1-py3-none-any.whl
- Upload date:
- Size: 26.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85ca5560d717c5a4de3aafb94eae0f3f8651415e004f859034787a6ddaa60752
|
|
| MD5 |
b1fe2f07f4c65d6abac8babb21167ddb
|
|
| BLAKE2b-256 |
c628599cce612259363340eca3ac3b4227c9bf89325b00ba6ff5fdf63921dd9d
|
Provenance
The following attestation bundles were made for django_openrouter-0.1.0b1-py3-none-any.whl:
Publisher:
publish.yml on svalench/django-openrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_openrouter-0.1.0b1-py3-none-any.whl -
Subject digest:
85ca5560d717c5a4de3aafb94eae0f3f8651415e004f859034787a6ddaa60752 - Sigstore transparency entry: 2708327055
- Sigstore integration time:
-
Permalink:
svalench/django-openrouter@767bfae65a5cdcc2c518c6be1ca8900eb5a21b7c -
Branch / Tag:
refs/tags/v0.1.0b1 - Owner: https://github.com/svalench
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@767bfae65a5cdcc2c518c6be1ca8900eb5a21b7c -
Trigger Event:
push
-
Statement type: