Skip to main content
Pre-release

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

  1. Open OpenRouter → OpenRouter settings. Paste an API key or leave it empty and set OPENROUTER_API_KEY in the environment.
  2. Run python manage.py sync_models to pull the model catalog (GET /api/v1/models).
  3. Create a Usage profile (slug translation, chat, …), pick a model, optional fallbacks, limits and budgets.
  4. 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:

  1. settings.OPENROUTER["API_KEY"]
  2. settings.OPENROUTER_API_KEY
  3. 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

django_openrouter-0.1.0b1.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_openrouter-0.1.0b1-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

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

Hashes for django_openrouter-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 c287609b895bd9c42677f8e4e3380a2b74907a0d30e3af77c7c08ec815598bb5
MD5 e6b1da8ec5cca15315584237fb47da38
BLAKE2b-256 910059e1e683816530dafd16bb809e089fc169668415d0f0bf5f2caf025df483

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_openrouter-0.1.0b1.tar.gz:

Publisher: publish.yml on svalench/django-openrouter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_openrouter-0.1.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_openrouter-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 85ca5560d717c5a4de3aafb94eae0f3f8651415e004f859034787a6ddaa60752
MD5 b1fe2f07f4c65d6abac8babb21167ddb
BLAKE2b-256 c628599cce612259363340eca3ac3b4227c9bf89325b00ba6ff5fdf63921dd9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_openrouter-0.1.0b1-py3-none-any.whl:

Publisher: publish.yml on svalench/django-openrouter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

This release

0.1.0b1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page