Django AI Gateway
Project status: read this first
This is an alpha. Here's what that means for you:
- Expect breaking changes. APIs will shift as we find better patterns.
- Migrations might be reset. Don't rely on database schema stability between versions.
- Not for production. Use this for experimentation, prototypes, and side projects. Keep critical workloads elsewhere.
- Watch the repo. Things change quickly. Star & watch to stay in the loop.
- Your feedback shapes the gateway. Break things, open issues, tell us what hurts.
We'd love to have you along for the ride, just keep your seatbelt on.
What it does
Each project gets one virtual key. Apps send every model call to one URL with that key, in the API style they already use: OpenAI, Anthropic or Ollama. The gateway:
- sends the call to the project's active provider, such as OpenAI, Anthropic, OpenRouter, a local Ollama or any API with one of those styles, with the project's own key for that provider;
- streams the reply back unchanged;
- maps model aliases, so
pony-xscan mean a different model name on each provider; - limits each project's open streams, requests per minute, and tokens per day and month;
- records every call's model, tokens, status and duration.
Switching a project to another provider is one choice in the dashboard. The proxy uses the new provider within a second, and the apps change nothing.
The dashboard is a set of Django pages for providers, projects, keys, limits and usage. Only active staff users can sign in, and by default they need a passkey or an authenticator app.
Quick start
cp demo/.env.example demo/.env # then fill in SECRET_KEY and GATEWAY_FERNET_KEYS
just migrate
uv run python demo/manage.py createsuperuser
just serve # the proxy on http://localhost:8080
just dashboard # the dashboard on http://localhost:8000, in a second terminal
Generate the two values with:
uv run python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
uv run python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
With Docker instead, after the same demo/.env:
docker compose -f compose.yaml up -d --build
docker compose -f compose.yaml exec dashboard python demo/manage.py createsuperuser
That runs the proxy on 127.0.0.1:8080 and the dashboard on 127.0.0.1:8000, with the SQLite database in a Docker volume.
Behind portside, just up serves the proxy at https://inference.gateway.test and the dashboard at https://admin.gateway.test. Once, run just certs and add 127.0.0.1 gateway.test inference.gateway.test admin.gateway.test to /etc/hosts.
Then sign in, add a passkey, add a provider and a project, and copy the project's virtual key. The quick start walks through each step.
Point your app at it
Change the base URL and the key. Nothing else.
django-ai-sdk, for the OpenAI generators:
OPENAI_API_URL=https://inference.gateway.test/v1
OPENAI_API_KEY=<virtual key>
| Generator | Settings |
|---|---|
openai_chat, openai_responses_chat, openrouter_chat, mistral_chat |
*_API_URL=https://inference.gateway.test/v1, *_API_KEY=<virtual key> |
anthropic_chat |
environment variables ANTHROPIC_BASE_URL=https://inference.gateway.test, ANTHROPIC_API_KEY=<virtual key> |
ollama_chat |
OLLAMA_API_URL=https://inference.gateway.test, environment variable OLLAMA_API_KEY=<virtual key> |
OpenAI package:
from openai import OpenAI
client = OpenAI(base_url="https://inference.gateway.test/v1", api_key="<virtual key>")
Anthropic package:
from anthropic import Anthropic
client = Anthropic(base_url="https://inference.gateway.test", api_key="<virtual key>")
curl:
curl https://inference.gateway.test/v1/chat/completions \
-H "Authorization: Bearer <virtual key>" \
-H "Content-Type: application/json" \
-d '{"model": "pony-xs", "messages": [{"role": "user", "content": "Hello"}]}'
Use your own GATEWAY_PUBLIC_URL in place of https://inference.gateway.test. It is http://localhost:8080 by default. The dashboard shows these snippets with the URL and key filled in. Point apps at the gateway lists the routes the proxy serves and the errors it returns.
Self-hosting
The gateway is a Django app, django_ai_gateway. To host it, make a Django project that installs the package, as demo/ does:
pip install django-ai-gateway
Copy demo/settings.py, demo/urls.py and demo/asgi.py as a starting point. They add django_ai_gateway to INSTALLED_APPS next to allauth, allauth MFA and axes (demo/auth.py holds the sign-in rules), set every GATEWAY_* setting, and include the dashboard with include("django_ai_gateway.urls"). The app's tables start with gateway_.
The project runs as two processes with the same settings and database:
- The proxy, the
gatewaycommand, is an aiohttp server for the model calls. It reads its configuration from a copy in memory, never from the database during a call. It checks for changes once a second and writes usage in the background. - The dashboard,
uvicorn demo.asgi:application, serves the gateway's own pages. There is no Django admin.
Run one proxy process. The limits are counted in its memory, and a second process would need shared counters first.
Self-hosting covers the Docker image, the database and a production checklist.
The dashboard
The dashboard has these pages:
- Overview: calls, tokens and failed calls over 24 hours, 7 days or 30 days.
- Projects: each project's connected providers and their keys, the active provider, limits, virtual key and usage.
- Providers: endpoints and model aliases. Only superusers can change them.
- Usage: every call, filtered by project, provider, model and status code.
- Passkeys and 2FA: the signed-in user's passkeys, authenticator app and recovery codes.
What a staff user sees and changes follows Django's model permissions on the gateway models. Provider keys are write-only. Every change is written to the gateway.audit logger.
Users are managed on the command line: manage.py createsuperuser, manage.py changepassword <user>, or manage.py shell for staff users and permissions. There is no signup, password reset or password change page.
The templates use Basecoat with Tailwind 4. The built stylesheet and Basecoat's scripts are committed in src/django_ai_gateway/static/gateway/, so neither Docker nor the server needs Node. After you change classes in a template, rebuild the stylesheet with just css, which needs pnpm. CI fails when the committed stylesheet is out of date. Dark mode follows the system until the toggle sets a theme cookie.
Settings
Required: SECRET_KEY, unless DEBUG is on, and GATEWAY_FERNET_KEYS, the Fernet keys that encrypt provider keys, newest first.
| Setting | Default | What |
|---|---|---|
DATABASE_URL |
SQLite file | Database |
ALLOWED_HOSTS |
localhost, 127.0.0.1, admin.gateway.test |
Dashboard host names |
CSRF_TRUSTED_ORIGINS |
https://admin.gateway.test |
Dashboard origins for form posts |
GATEWAY_BEHIND_TLS_PROXY |
false | Trust X-Forwarded-Proto. Only behind portside or another TLS proxy. |
GATEWAY_PUBLIC_URL |
http://localhost:8080 |
The proxy address apps call, shown in the dashboard snippets |
GATEWAY_REQUIRE_MFA |
true | Staff need a passkey or an authenticator app before using the dashboard |
GATEWAY_WEBAUTHN_RP_ID |
request host | Passkey relying party ID. Set it, for example to admin.gateway.test, to pin passkeys to one host. |
GATEWAY_WEBAUTHN_RP_NAME |
AI Gateway |
Name the browser shows when creating a passkey |
GATEWAY_READ_TIMEOUT_SECONDS |
300 | Provider silence before 504 |
GATEWAY_TOTAL_TIMEOUT_SECONDS |
900 | Longest call |
GATEWAY_MAX_BODY_BYTES |
16 MiB | Largest request body |
GATEWAY_MAX_PROVIDER_CONNECTIONS |
1000 | Open provider connections per process |
GATEWAY_PLAIN_HTTP_HOSTS |
localhost, 127.0.0.1, host.docker.internal |
Hosts an endpoint may use over plain http |
GATEWAY_LOG_LEVEL, GATEWAY_CALL_LOG_LEVEL |
INFO | The gateway log, and the one-line-per-call log. WARNING on the call log saves about 5% CPU. |
SECURE_SSL_REDIRECT, SECURE_HSTS_SECONDS |
off | Set them in production |
gateway reads DJANGO_SETTINGS_MODULE like any Django tool, and also GATEWAY_HOST, GATEWAY_PORT, GATEWAY_TLS_CERT and GATEWAY_TLS_KEY.
Commands: issue_virtual_key <project>, rotate_keys, report_usage [--project] [--since] [--by project|model|provider]. See Settings and Commands.
Security
- Virtual keys are stored as SHA-256 hashes. The dashboard shows a new key once. A new key replaces the old one within a second.
- Provider keys are encrypted with Fernet. To rotate the Fernet key, put the new key first in
GATEWAY_FERNET_KEYSfor both the proxy and the dashboard and restart them, runrotate_keys, then remove the old key.rotate_keysrefuses to run while any stored key cannot be decrypted. - The proxy drops the app's key, cookies and forwarding headers before a call leaves, adds the provider key, and refuses compressed bodies and paths with dot segments or encoded slashes. It does not store request or reply bodies. Endpoints must use https, except for
GATEWAY_PLAIN_HTTP_HOSTS. - Sign-in is django-allauth with passkeys, authenticator apps and recovery codes. Five failed passwords lock that username and IP address for an hour (django-axes,
manage.py axes_reset). Sessions end when the browser closes. - Passkeys belong to one host name. Use
http://localhost:8000, not127.0.0.1, for local passkeys. - Containers run as a user without root rights, on a read-only file system, and publish ports on 127.0.0.1 only.
Security has the sign-in steps, what to do when a user loses every factor, and the full list of what the proxy forwards.
Development
src/django_ai_gateway/ the package: models, proxy, dashboard, templates, static files, commands
demo/ the example project: settings, sign-in (allauth, MFA, axes), manage.py, Dockerfile
tests/ pytest, with tests/settings.py
frontend/ Tailwind source for the dashboard stylesheet
docs/ the documentation, built with dokk
just lists every recipe. The main ones:
| Recipe | What |
|---|---|
just serve [port] |
The proxy on the host, port 8080 by default |
just dashboard [port] |
collectstatic, then the dashboard on port 8000 by default |
just migrate |
Apply migrations |
just test |
pytest |
just check |
What CI runs: gitleaks, djLint, ruff, ty, Django checks, missing migrations, and pytest with coverage, which fails under 85% |
just lint |
gitleaks, then fix and format with djLint and ruff, then ty and pytest |
just css |
Rebuild the dashboard stylesheet, with pnpm |
just up, just down, just logs |
Docker behind portside |
just certs |
The local TLS certificate for *.gateway.test, with mkcert |
just hooks |
Install the git hooks |
just hooks installs prek, which runs the hooks in .pre-commit-config.yaml on each commit: gitleaks on the staged changes, djLint on the templates, ruff check and format, and ty.
Plans and design notes are in plans/index.html.
Is it any good?
License
Metadata
Release files for django-ai-gateway 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_ai_gateway-0.0.2.tar.gz | 629.5 kB | With provenance | Details for django_ai_gateway-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_ai_gateway-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for django_ai_gateway-0.0.2-py3-none-any.whl |
Total release size: 847.6 kB
Release files / django_ai_gateway-0.0.2.tar.gz
| Download URL | django_ai_gateway-0.0.2.tar.gz |
|---|---|
| Size | 629.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15390f6b78adb745b4aa94d8d05c3087b1fa200c03a162b5663f7f67af0d26eb
|
|
BLAKE2b-256 checksum How to use checksums |
cc704066178ade60638aea7895b1b5616b8482499fb1917e26abd4df880cd9c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logRelease files / django_ai_gateway-0.0.2-py3-none-any.whl
| Download URL | django_ai_gateway-0.0.2-py3-none-any.whl |
|---|---|
| Size | 218.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa4ccd51182e8bcddb267e3eea4896cbc142f71af1bed4f1d2fce8522581cbfe
|
|
BLAKE2b-256 checksum How to use checksums |
62207e1a9a6800dad045e273a95311dea11c2174799a39c2363154843e8752c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logSecurity for django-ai-gateway 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for django_ai_gateway-0.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.