SafetyGuard is a security-first FastAPI gateway for LLM, RAG, and agent workflows. It sits in front of downstream models, retrieval systems, and tools to enforce policy, block unsafe prompts, reduce data leakage risk, and produce auditable decisions.
Modern AI applications need more than a simple proxy. SafetyGuard adds a defense layer that helps teams:
- enforce identity-aware access policies
- detect prompt injection and jailbreak attempts
- scan prompts and attachments for secrets, credentials, and PII
- filter retrieved context before it reaches the model
- control tool usage and tool arguments
- redact or block unsafe model outputs
- capture audit and observability data for security review
- Policy enforcement: ordered rules with allow, deny, and restricted modes
- Input protection: deterministic scanning for prompt injection, obfuscation, secret exfiltration, and exploit intent
- DLP scanning: detection for secrets, API keys, private keys, PII, credentials, and financial patterns
- Session risk scoring: per-session risk tracking based on repeated violations and suspicious behavior
- Retrieval guardrails: tenant isolation, sensitivity filtering, and indirect prompt injection filtering for RAG content
- Tool gateway: permission checks, sensitivity checks, and argument injection detection
- Prompt building: trust-segmented prompt assembly with token budget awareness
- Output guard: leakage detection, redaction, and model-assisted output blocking
- OpenAI compatibility: endpoints for
/v1/modelsand/v1/chat/completions - Operations support: health probes, policy reload, audit event access, and structured decision logging
Client
-> Identity + request context
-> Rate limiting + normalization
-> Session state + risk scoring
-> Input scanning + DLP + optional LLM guard
-> Policy decision
-> Retrieval filtering + tool authorization
-> Prompt builder
-> Downstream LLM / agent runtime
-> Output guard + audit logging
-> Response
POST /v1/chat- primary chat endpointGET /v1/models- OpenAI-compatible model listPOST /v1/chat/completions- OpenAI-compatible chat completions endpointGET /health- liveness probeGET /ready- readiness probe
These require the X-Admin-Key header.
GET /admin/audit/eventsPOST /admin/policies/reload
app/ Core gateway implementation
main.py FastAPI entrypoint
chat_orchestrator.py End-to-end request pipeline
input_scanner.py Injection and jailbreak detection
dlp_scanner.py Secret, PII, and credential scanning
policy_engine.py Policy evaluation
retrieval_gateway.py RAG filtering and retrieval controls
tool_gateway.py Tool authorization and validation
prompt_builder.py Trust-segmented prompt creation
output_guard.py Response redaction and blocking
docs/ Architecture, security, operations, and development guides
tests/ Unit and API regression tests
- Python 3.11+
- uv
uv sync --all-groups
uv run uvicorn app.main:app --reload --port 8080The service starts on http://localhost:8080.
Copy the example file and adjust values for your environment:
cp .env.example .envKey settings include:
APP_DEV_MODEAPP_JWT_VERIFY_SIGNATUREAPP_ADMIN_API_KEYAPP_GUARD_LLM_ENABLEDAPP_GUARD_LLM_BASE_URLAPP_GUARD_LLM_MODELAPP_SECURITY_GATEWAY_URLAPP_RETRIEVAL_BACKEND_URLAPP_RATE_LIMIT_REQUESTSAPP_MAX_PROMPT_CHARS
SafetyGuard can call a local OpenAI-compatible guard model, such as Ollama:
ollama serve
ollama pull llama3.1
export APP_GUARD_LLM_ENABLED=true
export APP_GUARD_LLM_BASE_URL=http://localhost:11434/v1
export APP_GUARD_LLM_API_KEY=dummy
export APP_GUARD_LLM_MODEL=llama3.1To disable the model-assisted guard for faster local iteration:
export APP_GUARD_LLM_ENABLED=falseBuild and run the API container:
docker build -t safetyguard .
docker run --rm -p 8080:8080 safetyguardA sample Open WebUI integration is included:
docker compose -f docker-compose.openwebui.yml up --buildThis starts:
- SafetyGuard on
http://localhost:8080 - Open WebUI on
http://localhost:3000
Run the existing test suite with:
uv run pytestCoverage output is also supported:
uv run pytest --cov=app --cov-report=term-missing --cov-report=htmlDetailed documentation lives under docs/, including:
- architecture overview and component guides
- security controls and assumptions
- deployment and monitoring guidance
- local development and contribution notes
- operational runbooks
SafetyGuard is a good fit for teams building:
- internal chat assistants
- RAG applications handling sensitive data
- agent systems with tool execution
- OpenAI-compatible gateways with added guardrails
- security review layers in front of existing LLM services
The repository currently includes an automated test suite covering API and core security behaviors.