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

License

MIT © Noir Stack LLC

See LICENSE for full details.

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.1.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.1-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hexarch_guardrails-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 355c7b311992ad9cff5740905f6503345d5b8ff6a621c8980b07b8e631560ee2
MD5 4b1d221e543374ae91a4a4ac11296472
BLAKE2b-256 5ca3daac522d6c7f00cb8be0c81355e160e5dc6083e3adac7d2277950735ce46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hexarch_guardrails-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5ba2f65be31bb51ba6be2c6cd174426be542a7ae882d5b0fbb57353dcae1d36
MD5 666b3082c72b284e4966919cfc962752
BLAKE2b-256 d0869314bc4b4dbdcfb943e8244baf8a9b6a89f9eb3fabcd89324f4aa5734944

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