Skip to main content

LLM Firewall: a safety layer between your application and any LLM API — input/output firewalling, PII redaction, jailbreak detection, and domain-specific guardrails.

Project description

LLM Firewall (llmfw)

A safety layer between your application and any LLM API. It inspects prompts (input) and responses (output), applies a set of default checks, and optionally applies extra domain-specific checks (finance, medical, legal, ...). Ships with a local dashboard to visualize what got flagged or blocked over time.

Install

pip install llmfw            # core
pip install llmfw[dashboard] # + local dashboard (FastAPI/uvicorn)
pip install llmfw[all]       # everything

What to do after installing

You already have a chatbot or an app that calls an LLM. There are exactly 3 things to add around your existing code — nothing else changes.

from llmfw import Firewall

fw = Firewall(domain="finance")  # 1. create once (pick your domain, or omit it)

def handle_message(user_message):
    check = fw.check_input(user_message)          # 2. check what the user sent
    if not check.allowed:
        return f"Sorry, I can't help with that ({check.blocked_by})."

    response = call_your_llm(check.redacted_text)  # your existing LLM call, unchanged

    out_check = fw.check_output(response)          # 3. check what the LLM sent back
    if not out_check.allowed:
        return "Sorry, I can't share that response."

    return out_check.redacted_text

Replace call_your_llm(...) with whatever you already use (OpenAI, Claude, Gemini, ...) — that part doesn't change. fw should be created once per user session, not once globally for your whole app (see docs/architecture.md for why).

Prefer one line instead of wrapping input/output separately?

result = fw.protect(user_message, llm_call_fn=call_your_llm)
if not result.allowed:
    return f"Blocked: {result.blocked_by}"
return result.redacted_text

See examples/quickstart.py for a runnable version, or the full docs for configuration, the CLI, and the dashboard.

What it checks

Default detectors (always run):

  • Injection — prompt injection / jailbreak detection (pattern, structural, and paraphrase matching)
  • PII — emails, phone numbers, SSNs, card numbers (Luhn-validated), IPs — redacted in place
  • Output validator — schema/length checks, basic hallucination markers
  • Leak guard — detects system-prompt-like content leaking into output
  • Multi-turn tracker — flags cumulative suspicious patterns across a session
  • Toxicity — hate speech / self-harm / abuse filter
  • Obfuscation — decodes base64/unicode/leetspeak tricks before re-running the other checks

Domain packs (opt-in via domain=):

  • finance — guaranteed-return scam phrasing, unlicensed investment advice, pump-and-dump/fake-ticker hype
  • medical — unlicensed diagnosis/prescription language, unsafe dosage mentions, patient-identifiable health info (PHI)
  • legal — unauthorized legal advice/guaranteed-outcome phrasing, unverified case citations flagged for human review

CLI

llmfw check "some prompt" --domain finance
llmfw dashboard              # launch the local dashboard at localhost:8420
llmfw test --domain finance  # run the curated attack-prompt suite

Dashboard

llmfw dashboard starts a local FastAPI server (requires the dashboard extra) that serves a small UI showing recent blocked/flagged events, a breakdown by rule and domain, and a blocks-per-day trend. Events are logged to a local SQLite database (llmfw_events.db by default).

Configuration

# llmfw.config.yaml
domain: finance
enabled_detectors:
  - injection
  - pii
  - output_validator
  - leak_guard
  - multiturn_tracker
  - toxicity
  - obfuscation
thresholds:
  injection: 0.6
log_events: true
db_path: llmfw_events.db
fw = Firewall(config="llmfw.config.yaml")

Testing

pip install -e ".[dev,dashboard]"
pytest tests/
python -m tests.benchmark   # false-positive / false-negative rate per domain

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llmfw-0.1.1.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmfw-0.1.1-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file llmfw-0.1.1.tar.gz.

File metadata

  • Download URL: llmfw-0.1.1.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for llmfw-0.1.1.tar.gz
Algorithm Hash digest
SHA256 10d1341ffa8cc9b44091c1f97e3cf935e0ca045fb945a681f0b9d2a63ff570f4
MD5 af4ff196dbb84923d124f49c93239b6d
BLAKE2b-256 2bba0c65be22d4d8cde2b6581392d1d60e8576c7cb734a335c19824df5a4e088

See more details on using hashes here.

File details

Details for the file llmfw-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: llmfw-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 36.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for llmfw-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 498902577698e5961630f058b410dd271c66e8d7b862d82739383973dffa9245
MD5 84476968f3297bbc70385bbc18e1df0b
BLAKE2b-256 b437560493c8fda36cab0d797d95db7bfa9fe35397d55f23c8d2853bc842468a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page