Skip to main content

Local-first runtime governance layer for AI systems

Project description

๐Ÿ›ก๏ธ Guardian Runtime

The local-first AI firewall for developers.

Cut LLM token costs ยท Block data leaks ยท Stop jailbreaks โ€” all on your machine.

PyPI Python Tests License


Guardian Runtime is a Python SDK that acts as a transparent security layer between your AI application and any LLM provider. It automatically compresses prompts to reduce token costs, blocks PII and API key leaks before they reach the model, and catches jailbreak attempts โ€” all running locally on your machine. Your prompts never leave your infrastructure.

  Your Prompt
      โ”‚
      โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           GUARDIAN RUNTIME              โ”‚
โ”‚                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚ Optimizer โ”‚   โ”‚   Input Guard    โ”‚   โ”‚
โ”‚  โ”‚           โ”‚   โ”‚                  โ”‚   โ”‚
โ”‚  โ”‚ -30~70%   โ”‚โ”€โ”€โ–ถโ”‚ PII ยท Secrets   โ”‚   โ”‚
โ”‚  โ”‚  tokens   โ”‚   โ”‚ Jailbreaks      โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                           โ”‚             โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚         โ”‚       Output Guard         โ”‚  โ”‚
โ”‚         โ”‚  Scans AI response for PII โ”‚  โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚
      โ–ผ
  Safe Response + Cost Report

๐Ÿš€ Quick Start

1. Install

pip install guardian-runtime

Requires Python 3.10+. Includes the document converter (PDF/DOCX โ†’ Markdown) out of the box.

2. Set your API key

export GEMINI_API_KEY="your-google-ai-studio-key"   # free tier works!

3. Create a policy file (policy.yaml)

version: "1.0"
agents:
  default:
    llm:
      provider: gemini
      default_model: gemini-2.5-flash
    input_guard:
      pii_detection: true
      jailbreak_detection: true
      pii_action: block
    output_guard:
      pii_detection: true
    optimizer:
      enabled: true
      whitespace_normalization: true
      max_history_messages: 10
    cost:
      max_input_tokens: 8000

4. Use it

from guardian import Guardian

guardian = Guardian.from_policy("policy.yaml")

response = guardian.complete(
    messages=[{"role": "user", "content": "Summarize this document for me."}]
)

print(response.content)              # safe, validated response
print(response.input_tokens)         # tokens consumed
print(response.estimated_cost_usd)   # cost in USD
print(response.optimization)         # token savings breakdown

โœจ Why Guardian?

Observability tools (Langfuse, LangSmith, Helicone) log what went wrong โ€” after it happened.
Guardian stops it before it happens, on your machine.

Observability Tools Guardian Runtime
When it acts After the LLM call Before and after
Your data Sent to their cloud Stays on your machine
PII in prompt Logged Blocked
Exposed API keys Not detected Blocked
Token costs Tracked Actively reduced
Jailbreak attempts Logged Blocked

๐Ÿ”Œ Supported Providers

Works out-of-the-box with the Big 3 LLM providers:

Provider Environment Variable Default Model
Google Gemini GEMINI_API_KEY gemini-2.5-flash
Anthropic Claude ANTHROPIC_API_KEY claude-3-5-haiku-latest
OpenAI OPENAI_API_KEY gpt-4o-mini

Override provider at runtime:

response = guardian.complete(
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    messages=[...]
)

๐Ÿ›ก๏ธ Security & Privacy Features

PII Detection

Detects and blocks sensitive personal data with specialized patterns:

Category Detected Entities
India (DPDP Act) Aadhaar (xxxx xxxx xxxx), PAN (XXXXX0000X), UPI (name@bank)
Global SSN, Credit Cards, Email, Phone, Passport
Action modes block, redact, or flag per entity type

Secret Leak Prevention

Stops developers from accidentally sending credentials to an LLM:

  • OpenAI keys (sk-...) ยท AWS access keys (AKIA...) ยท GitHub tokens (ghp_...)
  • Stripe keys (sk_live_...) ยท Razorpay keys (rzp_live_...) ยท Groq keys (gsk_...)
  • Generic .env variable patterns

Jailbreak Detection

40+ regex patterns covering:

  • DAN variants and instruction overrides
  • Role-play injections and encoding tricks
  • System prompt extraction attempts

Output Guard

Scans LLM responses for PII and secrets before they reach your users.


๐Ÿ“‰ Cost Optimization

Feature How it saves
Prompt Compression Strips redundant whitespace, deduplicates system prompts, removes empty messages
History Trimming Keeps last N turns, always preserves system prompt
Document Conversion PDF, DOCX, XLSX, PPTX โ†’ clean Markdown (30โ€“70% token savings)
Token Budget Enforcement Warn or block when input exceeds your defined limit
Cost Estimation Every response includes token count and USD cost estimate

๐Ÿ“Š Analysis Report

Every guardian.complete() call returns a full analysis:

response = guardian.complete(messages=[...])

print(response.content)              # safe, validated response
print(response.blocked)              # True if blocked
print(response.violations)           # list of what was caught
print(response.input_tokens)         # tokens used
print(response.estimated_cost_usd)   # cost in USD
print(response.optimization)         # tokens saved, savings %

Example optimization result:

{
  "original_tokens": 1840,
  "optimized_tokens": 620,
  "savings_pct": 0.66,
  "actions_taken": ["whitespace_normalization", "history_trimming"]
}

โš–๏ธ Compliance Coverage

Guardian's detection covers major data protection regulations:

Regulation Coverage
India DPDP Act 2023 Aadhaar, PAN, UPI โ€” native patterns
GDPR (EU) Email, phone, passport, general PII
HIPAA (US Health) Sensitive personal data blocking
CCPA (California) Consumer data protection

Note: Guardian is an assistive compliance tool, not legal advice. Always consult qualified counsel for regulatory requirements.


๐Ÿ–ฅ๏ธ CLI

Guardian comes with a built-in command line interface:

guardian init --key gdn_free_xxxxx       # optional license setup
guardian validate policies/gemini.yaml   # check policy syntax
guardian status                          # view usage, tokens, and costs
guardian logs --tail 20                  # view recent violations

Example guardian status output:

License: not configured (offline free tier)
Plan: free
Checks this month: 1 / 10000
Status: ACTIVE

--- Usage Analytics ---
Original Input Tokens:  1,840
Optimized Input Tokens: 620 (-1,220 saved)
Total Output Tokens:    19
Estimated Cost:         $0.000420 USD

๐Ÿ—๏ธ Local-First Architecture

Everything runs on your machine. Nothing is uploaded.

Your Machine
โ”œโ”€โ”€ guardian SDK         โ† all processing happens here
โ”œโ”€โ”€ ~/.guardian/
โ”‚   โ”œโ”€โ”€ config.json     โ† license key (if using paid plan)
โ”‚   โ”œโ”€โ”€ usage.json      โ† monthly check count
โ”‚   โ””โ”€โ”€ logs/           โ† violation logs (never uploaded)
โ”‚       โ””โ”€โ”€ YYYY-MM-DD.jsonl

What Guardian's servers never receive:

  • โŒ Your prompts
  • โŒ Your LLM responses
  • โŒ Your violation details
  • โŒ Your API keys (OpenAI, Anthropic, etc.)

๐Ÿ“ Project Structure

guardian/
โ”œโ”€โ”€ core/           engine, policy, models, storage
โ”œโ”€โ”€ guards/         input guard, output guard, validators
โ”‚   โ””โ”€โ”€ validators/ pii, secrets, jailbreak, hallucination
โ”œโ”€โ”€ optimizer/      prompt compression, document converter
โ”œโ”€โ”€ providers/      openai, gemini, anthropic
โ”œโ”€โ”€ finops/         token counter, cost calculator
โ”œโ”€โ”€ proxy/          localhost reverse proxy server
โ”œโ”€โ”€ logging/        local JSONL logger
โ”œโ”€โ”€ dashboard/      usage dashboard
โ””โ”€โ”€ cli/            init, validate, status, logs

๐Ÿงช Development

git clone https://github.com/ashp15205/guardian-runtime.git
cd guardian-runtime
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest tests/ -q    # 111 tests

See ARCHITECTURE.md for the full technical specification.


๐Ÿ”’ Privacy

  • All scanning runs on your infrastructure
  • Logs stored locally at ~/.guardian/logs/
  • Optional license sync sends only a hashed key + check count โ€” never prompts or responses

Built with โค๏ธ for developers who care about AI safety and cost.
PyPI ยท GitHub

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

guardian_runtime-0.2.4.tar.gz (89.2 kB view details)

Uploaded Source

Built Distribution

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

guardian_runtime-0.2.4-py3-none-any.whl (50.9 kB view details)

Uploaded Python 3

File details

Details for the file guardian_runtime-0.2.4.tar.gz.

File metadata

  • Download URL: guardian_runtime-0.2.4.tar.gz
  • Upload date:
  • Size: 89.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for guardian_runtime-0.2.4.tar.gz
Algorithm Hash digest
SHA256 06310e8f9c39c8b9f37249eae0de4ac32afbb7984a6ef5b19dfbb6aaa3f8c706
MD5 89b7c0c1a7e04a9610a7b2b69c35f5e2
BLAKE2b-256 830e2702caa43163f48549e78c4f43e08186bca07c5969a22cd4cf5bdeae54fb

See more details on using hashes here.

File details

Details for the file guardian_runtime-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for guardian_runtime-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 25152561b1f3c1259df0fccb8a00c81b291120792a8cd15f3345e31053a35513
MD5 0b7e7ea04b2118ad3a7ed77d51f9b53b
BLAKE2b-256 f2fdadb61b9a648d8f73e4c58d01e86327a5f91b33fe4af82c309542ab359b91

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