LLM Security Firewall
A production-ready security layer for AI applications that prevents prompt injection, data exfiltration, and policy violations.
The Problem
Companies deploying LLMs face critical security risks:
- ❌ Prompt Injection - Users manipulate AI behavior with crafted inputs
- ❌ Data Exfiltration - Sensitive data leaks through AI responses
- ❌ Jailbreaks - Users bypass content policies and safety guardrails
- ❌ PII Exposure - Personal information revealed in outputs
- ❌ No Audit Trail - Can't track what users ask or what AI responds
- ❌ Cost Overruns - Uncontrolled API usage drains budgets
The Solution
LLM Security Firewall sits between your application and AI providers, providing:
✅ Real-time Threat Detection - Block attacks before they reach the AI ✅ Content Filtering - Redact sensitive data in requests and responses ✅ Policy Enforcement - Define and enforce usage policies ✅ Complete Audit Logs - Track every interaction for compliance ✅ Cost Control - Rate limiting and budget management ✅ Multi-Provider Support - Works with OpenAI, Anthropic, Azure, local models
Quick Start
Installation
pip install llm-security-firewall
Basic Usage
from llm_firewall import LLMFirewall, FirewallConfig
# Initialize firewall
config = FirewallConfig(
providers=["openai", "anthropic"],
enable_prompt_injection_detection=True,
enable_pii_redaction=True,
enable_audit_logging=True,
)
firewall = LLMFirewall(config)
# Protect your LLM calls
async with firewall.protect() as session:
response = await session.chat(
provider="openai",
model="gpt-4",
messages=[{"role": "user", "content": user_input}]
)
# Firewall automatically:
# - Scans for prompt injection
# - Redacts PII
# - Logs the interaction
# - Enforces rate limits
# - Returns safe response
As Middleware (FastAPI)
from fastapi import FastAPI
from llm_firewall.middleware import LLMFirewallMiddleware
app = FastAPI()
# Add firewall middleware
app.add_middleware(
LLMFirewallMiddleware,
config=config,
api_key_header="X-API-Key",
)
@app.post("/api/chat")
async def chat(request: ChatRequest):
# Requests automatically protected by firewall
return await llm_client.chat(request)
As Proxy Server
# Start proxy server
llm-firewall-proxy --config config.yaml --port 8080
# Point your LLM SDK at the proxy
export OPENAI_BASE_URL=http://localhost:8080/openai
export ANTHROPIC_BASE_URL=http://localhost:8080/anthropic
Features
🛡️ Threat Detection
Prompt Injection Detection
- Pattern-based detection (known attack vectors)
- ML-based classification (custom trained model)
- Confidence scoring (0-100%)
- Automatic blocking or warning mode
Jailbreak Prevention
- Detects attempts to bypass content policies
- Recognizes role-play attacks ("pretend you're...")
- Identifies multi-turn manipulation attempts
Data Exfiltration Protection
- Scans for requests asking for system prompts
- Detects attempts to extract training data
- Prevents credential/key harvesting
🔒 Content Security
PII Redaction
- Automatic detection and redaction:
- Email addresses
- Phone numbers
- Credit card numbers
- SSN/Tax IDs
- IP addresses
- API keys/tokens
- Reversible redaction (restore in logs)
- Custom redaction patterns
Response Filtering
- Scan AI responses for sensitive data
- Block or redact policy violations
- Custom content rules
Data Loss Prevention (DLP)
- Prevent specific data types from being sent/received
- Configurable allow/deny lists
- Regex-based pattern matching
📊 Policy Engine
Access Control
- User-based quotas (requests per hour/day)
- Model access restrictions
- Feature gating (by tier, role, etc.)
- IP allowlist/denylist
Cost Management
- Budget limits per user/team
- Token usage tracking
- Cost alerts and auto-cutoff
- Provider cost comparison
Compliance
- GDPR mode (data minimization)
- HIPAA compliance features
- SOC 2 audit logging
- Data residency controls
📈 Observability
Audit Logging
- Complete request/response logging
- Threat detection events
- Policy violations
- Performance metrics
- Export to SIEM (Splunk, Elastic, Datadog)
Real-time Monitoring
- Live dashboard
- Threat alerts (Slack, PagerDuty, webhook)
- Usage analytics
- Performance metrics
Analytics
- User behavior analysis
- Attack pattern detection
- Model performance comparison
- Cost optimization insights
Architecture
┌─────────────────┐
│ Application │
└────────┬────────┘
│
┌────▼────────────────────────────────────────┐
│ LLM Security Firewall │
│ │
│ ┌──────────────────────────────────────┐ │
│ │ Input Protection Layer │ │
│ │ • Prompt Injection Detection │ │
│ │ • PII Redaction │ │
│ │ • Request Validation │ │
│ └──────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────┐ │
│ │ Policy Engine │ │
│ │ • Access Control │ │
│ │ • Rate Limiting │ │
│ │ • Cost Management │ │
│ └──────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────┐ │
│ │ Output Protection Layer │ │
│ │ • Response Scanning │ │
│ │ • Content Filtering │ │
│ │ • Data Redaction │ │
│ └──────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────┐ │
│ │ Audit & Monitoring │ │
│ │ • Event Logging │ │
│ │ • Metrics Collection │ │
│ │ • Alert Management │ │
│ └──────────────────────────────────────┘ │
└────────┬────────────────────────────────────┘
│
┌────────▼────────┐
│ AI Providers │
│ • OpenAI │
│ • Anthropic │
│ • Azure │
│ • Local LLMs │
└─────────────────┘
Configuration
YAML Configuration
# config.yaml
firewall:
# Detection settings
detection:
prompt_injection:
enabled: true
threshold: 0.7 # 70% confidence
action: block # or 'warn'
jailbreak:
enabled: true
patterns:
- "ignore previous instructions"
- "pretend you are"
- "act as if"
data_exfiltration:
enabled: true
sensitive_keywords:
- "api key"
- "password"
- "secret"
# Content filtering
content:
pii_redaction:
enabled: true
types:
- email
- phone
- ssn
- credit_card
response_filtering:
enabled: true
block_patterns:
- r"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b"
# Policy engine
policy:
rate_limiting:
enabled: true
limits:
default: 100/hour
premium: 1000/hour
cost_management:
enabled: true
budget_limits:
daily: 100.00
monthly: 2000.00
access_control:
allowed_models:
- gpt-4
- gpt-4-turbo
- claude-3-sonnet
# Logging & monitoring
logging:
audit_log:
enabled: true
destination: postgresql
retention_days: 90
alerts:
webhook_url: https://hooks.slack.com/services/YOUR/WEBHOOK
alert_on:
- high_risk_threat
- policy_violation
- budget_exceeded
Environment Variables
# Provider API keys
export OPENAI_API_KEY=sk-...
export ANTHROPIC_API_KEY=sk-ant-...
# Firewall settings
export LLM_FIREWALL_CONFIG=config.yaml
export LLM_FIREWALL_LOG_LEVEL=INFO
export LLM_FIREWALL_DATABASE_URL=postgresql://localhost/firewall
# Security
export LLM_FIREWALL_SECRET_KEY=your-secret-key
export LLM_FIREWALL_ENCRYPTION_KEY=your-encryption-key
Use Cases
Enterprise SaaS Application
# Protect customer-facing chatbot
firewall = LLMFirewall(config)
@app.post("/api/chat")
async def chat(message: str, user_id: str):
# Enforce per-user rate limits
# Block prompt injection
# Redact PII from user messages
# Log all interactions for compliance
async with firewall.protect(user_id=user_id) as session:
return await session.chat(
provider="anthropic",
model="claude-3-sonnet",
messages=[{"role": "user", "content": message}]
)
Internal Developer Tools
# Protect code generation assistant
config = FirewallConfig(
enable_prompt_injection_detection=True,
enable_code_safety_analysis=True, # Scan generated code
enable_cost_tracking=True,
cost_alerts={
"daily": 50.00,
"per_request": 0.50,
}
)
firewall = LLMFirewall(config)
RAG Application
# Protect retrieval-augmented generation
async with firewall.protect() as session:
# Prevent prompt injection in retrieved documents
# Block attempts to extract training data
# Redact sensitive data from vector DB results
results = vector_db.search(query)
response = await session.chat(
provider="openai",
model="gpt-4",
messages=[
{"role": "system", "content": "Use these docs: " + results},
{"role": "user", "content": user_query}
]
)
Deployment
Docker
# Pull image
docker pull llmsecurity/firewall:latest
# Run as proxy
docker run -p 8080:8080 \
-e OPENAI_API_KEY=$OPENAI_API_KEY \
-v $(pwd)/config.yaml:/app/config.yaml \
llmsecurity/firewall:latest
Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: llm-firewall
spec:
replicas: 3
template:
spec:
containers:
- name: firewall
image: llmsecurity/firewall:latest
env:
- name: OPENAI_API_KEY
valueFrom:
secretKeyRef:
name: llm-api-keys
key: openai
ports:
- containerPort: 8080
AWS Lambda (Serverless)
# lambda_handler.py
from llm_firewall import LLMFirewall, FirewallConfig
config = FirewallConfig.from_env()
firewall = LLMFirewall(config)
def lambda_handler(event, context):
user_input = event['body']['message']
async with firewall.protect() as session:
response = await session.chat(
provider="openai",
model="gpt-4",
messages=[{"role": "user", "content": user_input}]
)
return {"statusCode": 200, "body": response}
Performance
Latency Overhead:
- Pattern-based detection: ~5-10ms
- ML-based detection: ~20-50ms
- PII redaction: ~10-15ms
- Total average: ~35-75ms added latency
Throughput:
- 10,000+ requests/sec (pattern-based only)
- 2,000+ requests/sec (full protection)
- Scales horizontally with Redis cache
Resource Usage:
- Memory: ~200MB base + ~50MB per 1000 req/sec
- CPU: ~0.5 cores at 1000 req/sec
Pricing
Open Source (MIT License)
- ✅ Core security features
- ✅ Self-hosted deployment
- ✅ Community support
Cloud (SaaS)
-
Starter: $99/month
- 100K requests/month
- Basic threat detection
- 30-day audit logs
-
Professional: $499/month
- 1M requests/month
- Advanced ML detection
- 90-day audit logs
- Slack/PagerDuty alerts
-
Enterprise: Custom pricing
- Unlimited requests
- Custom ML models
- Unlimited log retention
- SLA & dedicated support
- On-premise deployment
Roadmap
Q1 2026
- Core firewall MVP
- Prompt injection detection
- PII redaction
- Web dashboard
- SIEM integrations
Q2 2026
- Advanced ML models
- Multi-language support
- Custom policy DSL
- Terraform provider
Q3 2026
- Real-time collaboration features
- Advanced analytics
- Compliance certifications
- Enterprise features
Contributing
We welcome contributions! See CONTRIBUTING.md for guidelines.
License
MIT License - see LICENSE for details.
Support
Built with ❤️ for secure AI deployments
Proudly part of the AfterDark Security ecosystem
Release files for afterdark-llm-firewall 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| afterdark_llm_firewall-0.1.0.tar.gz | 18.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| afterdark_llm_firewall-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.4 kB
Release files / afterdark_llm_firewall-0.1.0.tar.gz
| Download URL | afterdark_llm_firewall-0.1.0.tar.gz |
|---|---|
| Size | 18.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1537e8647aec8024161b827857587a18932dca3564b167e02ed8bfcf85dadd57
|
|
BLAKE2b-256 checksum How to use checksums |
c56018fa5810ff205b5164221a92988fb08fe41def4a92f977740fce29825a65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / afterdark_llm_firewall-0.1.0-py3-none-any.whl
| Download URL | afterdark_llm_firewall-0.1.0-py3-none-any.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b2c2ef41d41dcc814e77868f5f4834f461db29cb486ee8d1391292e4d8bfd0c
|
|
BLAKE2b-256 checksum How to use checksums |
2687794b822a25a26a86ba8ab9fa971ed585fbfdd2d7bd6e87afef33343fb99c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|