Skip to main content

django-agent-governance

AI governance and PII protection for Django — plug in, don't rip out.
Like django-allauth for 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

MIT

Metadata

Release files for django-agent-governance 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-agent-governance 0.1.0
File Size Uploaded
django_agent_governance-0.1.0.tar.gz 21.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-agent-governance 0.1.0
File Interpreter ABI Platform
django_agent_governance-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.9 kB

Release files / django_agent_governance-0.1.0.tar.gz

Download URL django_agent_governance-0.1.0.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
79c4a0049eaf69ffddbf84ebfb9628f536b433006f25dfa4c7290561ec31a251
BLAKE2b-256 checksum
How to use checksums
02daa600837ae700831e5aea829e5d4d2c243f3b25d2e49d1754868d992423ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.12

Release files / django_agent_governance-0.1.0-py3-none-any.whl

Download URL django_agent_governance-0.1.0-py3-none-any.whl
Size 27.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72c567bf895411e69233646d60b934beeb4d1c0c08e59941174fd6c556692356
BLAKE2b-256 checksum
How to use checksums
844c03b455587e8f036456e70e82ac77dd6f3d9acd901b6604d9795a3f489639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release 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