Skip to main content

Lightweight policy-driven API protection and guardrails library

Project description

Hexarch Guardrails Python SDK

Lightweight policy-driven API protection for students, solo developers, and hackathons.

Installation

pip install hexarch-guardrails

Quick Start

1. Create a policy file (hexarch.yaml)

policies:
  - id: "api_budget"
    description: "Protect against overspending"
    rules:
      - resource: "openai"
        monthly_budget: 10
        action: "warn_at_80%"

  - id: "rate_limit"
    description: "Prevent API abuse"
    rules:
      - resource: "*"
        requests_per_minute: 100
        action: "block"

  - id: "safe_delete"
    description: "Require confirmation for destructive ops"
    rules:
      - operation: "delete"
        action: "require_confirmation"

2. Use in your code

from hexarch_guardrails import Guardian

# Initialize (auto-discovers hexarch.yaml)
guardian = Guardian()

# Protect API calls
@guardian.check("api_budget")
def call_openai(prompt):
    import openai
    return openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )

# Use it
response = call_openai("Hello AI!")

Features

  • Zero-config - Auto-discovers hexarch.yaml
  • Decorator-based - Drop in @guardian.check(policy_id)
  • Policy-driven - YAML-based rules, no code changes
  • Local-first - Works offline or with local OPA
  • Pluggable - Works with any API/SDK

Examples

Budget Protection (OpenAI)

@guardian.check("api_budget")
def expensive_operation():
    # This call is protected by budget limits
    return openai.ChatCompletion.create(model="gpt-4", ...)

Rate Limiting

@guardian.check("rate_limit")
def send_discord_message(msg):
    return client.send(msg)

Safe File Operations

@guardian.check("safe_delete")
def delete_file(path):
    os.remove(path)

Documentation

Admin CLI (v0.3.0+)

Hexarch includes a command-line interface for managing policies and monitoring decisions:

Installation

# Install with CLI extras
pip install hexarch-guardrails[cli]

Quick Start

# List all policies
hexarch-ctl policy list

# Export a policy
hexarch-ctl policy export ai_governance --format rego

# Validate policy syntax
hexarch-ctl policy validate ./policy.rego

# Compare policy versions
hexarch-ctl policy diff ai_governance

Available Commands

Policy Management:

  • hexarch-ctl policy list - List all OPA policies
  • hexarch-ctl policy export - Export policy to file or stdout
  • hexarch-ctl policy validate - Validate OPA policy syntax
  • hexarch-ctl policy diff - Compare policy versions

Upcoming (Phase 3-4):

  • Decision querying and analysis
  • Metrics and performance monitoring
  • Configuration management

For detailed CLI documentation, see POLICY_COMMANDS_GUIDE.md

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

hexarch_guardrails-0.3.0.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

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

hexarch_guardrails-0.3.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file hexarch_guardrails-0.3.0.tar.gz.

File metadata

  • Download URL: hexarch_guardrails-0.3.0.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for hexarch_guardrails-0.3.0.tar.gz
Algorithm Hash digest
SHA256 63d649d9a08372b1a148e99350ab0abaefa6e0b801324a61c1e8ee322000cd2b
MD5 8313304b41bdcb6296f2235eba8ed980
BLAKE2b-256 7acc21c0211635bfded476b7613427c2a6e80c414c1d1e8ccb8f880627a119af

See more details on using hashes here.

File details

Details for the file hexarch_guardrails-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hexarch_guardrails-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cda7cf65378b843332fa719188fe172d9778ee53c66b9dfe079889aa4afef1a1
MD5 e095ab20b4559ae2d63f6bdf1f255708
BLAKE2b-256 3da7d76941277e8b69cc26954f785a4dcd193e5576804bdd0ea709905651e1f6

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