AI governance and PII protection for Django — decorators, audit logs, and LLM-agnostic proxy.
Project description
django-agent-governance
AI governance and PII protection for Django — plug in, don't rip out.
Likedjango-allauthfor AI security. Add decorators, audit logs, and a unified LLM proxy to any Django project in minutes.
Features
| Feature | Description |
|---|---|
@redact_pii |
Strip emails, phones, SSNs, credit cards from any view or function |
@audit_llm_call |
Automatically log every LLM prompt, response, token count, and latency |
| LLMProxy | One interface for Ollama, OpenAI, Anthropic, and Gemini — swap with one config line |
| AgentAdmin | Django Admin dashboard with token cost estimates, PII hit rates, and latency charts |
| PIIRedactionMiddleware | Auto-redact all incoming request bodies without touching your views |
| TokenBudget | Per-user/per-app monthly token limits with automatic enforcement |
Install
Lightweight (regex PII engine — no extra downloads):
pip install django-agent-governance
Enterprise (Presidio NLP engine — ~500MB spaCy model):
pip install django-agent-governance[presidio]
python -m spacy download en_core_web_lg
With cloud LLM SDKs:
pip install django-agent-governance[openai,anthropic]
Quick Start
1. Add to INSTALLED_APPS
# settings.py
INSTALLED_APPS = [
...
"agent_governance",
]
AGENT_GOVERNANCE = {
"LLM_BACKEND": "ollama", # Default: local Ollama. No API key needed.
"OLLAMA_BASE_URL": "http://localhost:11434",
"OLLAMA_MODEL": "llama3.2:1b",
# Uncomment to use cloud:
# "LLM_BACKEND": "openai",
# "OPENAI_API_KEY": env("OPENAI_API_KEY"),
}
2. Run migrations
python manage.py migrate agent_governance
3. Use the decorators
from agent_governance.decorators import redact_pii, audit_llm_call
from agent_governance.proxy import LLMProxy
proxy = LLMProxy()
@redact_pii
@audit_llm_call
def chat_view(request):
# request body is already PII-free ✅
prompt = request.POST.get("message", "")
response = proxy.chat(prompt)
return JsonResponse({"reply": response.content})
4. View the Admin dashboard
Visit /admin/agent_governance/ to see your LLM cost tracker, PII hit rates, and response quality.
Configuration Reference
AGENT_GOVERNANCE = {
# ── LLM Backend ───────────────────────────────────────────────
"LLM_BACKEND": "ollama", # "ollama" | "openai" | "anthropic" | "gemini"
# ── Ollama (default — local, free) ────────────────────────────
"OLLAMA_BASE_URL": "http://localhost:11434",
"OLLAMA_MODEL": "llama3.2:1b",
# ── OpenAI ────────────────────────────────────────────────────
"OPENAI_API_KEY": "", # or set env var OPENAI_API_KEY
"OPENAI_MODEL": "gpt-4o-mini",
# ── Anthropic ─────────────────────────────────────────────────
"ANTHROPIC_API_KEY": "", # or set env var ANTHROPIC_API_KEY
"ANTHROPIC_MODEL": "claude-3-haiku-20240307",
# ── Google Gemini ─────────────────────────────────────────────
"GEMINI_API_KEY": "", # or set env var GEMINI_API_KEY
"GEMINI_MODEL": "gemini-1.5-flash",
# ── PII Engine ────────────────────────────────────────────────
"PII_ENGINE": "regex", # "regex" (default) | "presidio" (install [presidio] extra)
"PII_ENTITIES": None, # None = all; or ["EMAIL_ADDRESS", "PHONE_NUMBER"]
# ── Governance ────────────────────────────────────────────────
"TOKEN_BUDGET_ENABLED": False,
"AUDIT_LOG_RETENTION_DAYS": 90,
}
LLM-Agnostic Proxy
Switch providers without changing a line of your application code:
from agent_governance.proxy import LLMProxy
proxy = LLMProxy()
# Talks to Ollama by default
response = proxy.chat("Summarize this document.")
# Override per-call
response = proxy.chat("Summarize this.", backend="anthropic", model="claude-3-opus-20240229")
print(response.content) # The completion text
print(response.input_tokens) # Token counts
print(response.latency_ms) # Latency
Supported PII Entities (Regex Engine)
| Entity | Example |
|---|---|
EMAIL_ADDRESS |
user@example.com |
PHONE_NUMBER |
(555) 867-5309 |
CREDIT_CARD |
4111111111111111 |
US_SSN |
123-45-6789 |
IP_ADDRESS |
192.168.1.100 |
URL |
https://internal-server/api |
UK_NIN |
AB123456C |
Upgrade to [presidio] for: PERSON, ORG, LOCATION, DATE_TIME, NRP, and 50+ more.
Contributing
See CONTRIBUTING.md. PRs welcome!
License
Project details
Release history Release notifications | RSS feed
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_agent_governance-0.1.0.tar.gz.
File metadata
- Download URL: django_agent_governance-0.1.0.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79c4a0049eaf69ffddbf84ebfb9628f536b433006f25dfa4c7290561ec31a251
|
|
| MD5 |
4d31c5f11942a70fe7c0162d237b045b
|
|
| BLAKE2b-256 |
02daa600837ae700831e5aea829e5d4d2c243f3b25d2e49d1754868d992423ee
|
File details
Details for the file django_agent_governance-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_agent_governance-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72c567bf895411e69233646d60b934beeb4d1c0c08e59941174fd6c556692356
|
|
| MD5 |
4e9cb588f4b33a7b6311e546a1c2e2b5
|
|
| BLAKE2b-256 |
844c03b455587e8f036456e70e82ac77dd6f3d9acd901b6604d9795a3f489639
|