Skip to main content

Production-grade action middleware for agentic AI โ€” by Plyra

Project description

๐Ÿ›ก๏ธ plyra-guard

Part of the Plyra agentic infrastructure suite.

Production-grade middleware for securing, observing, and controlling actions taken by AI agents.

PyPI Python Version License Tests

pip install plyra-guard

Built by Plyra โ€” Infrastructure for Agentic AI.


Why plyra-guard?

  • ๐Ÿ”’ Every action passes through a security pipeline โ€” Intercept โ†’ Evaluate โ†’ Execute โ†’ Observe โ†’ Rollback. No unguarded tool calls.
  • ๐Ÿค– Works with any AI framework โ€” LangChain, LlamaIndex, CrewAI, AutoGen, OpenAI, Anthropic, or plain Python callables.
  • ๐ŸŒ Multi-agent native โ€” Trust ledgers, delegation tracking, cascade control, and cross-agent rollback built in from day one.

Quick Install

pip install plyra-guard

With optional features:

pip install plyra-guard[sidecar]    # HTTP sidecar server
pip install plyra-guard[otel]       # OpenTelemetry export
pip install plyra-guard[all]        # Everything

Quickstart

1. Basic Decorator

from plyra_guard import ActionGuard, RiskLevel

guard = ActionGuard.default()

@guard.protect("file.delete", risk_level=RiskLevel.HIGH)
def delete_file(path: str) -> bool:
    import os
    os.remove(path)
    return True

# Every call is intercepted, evaluated, and audited
delete_file("/tmp/test.txt")

2. Namespace Import

# Both import styles work:
from plyra_guard import ActionGuard      # direct
from plyra.guard import ActionGuard      # namespace

3. Multi-Agent Orchestration

from plyra_guard import ActionGuard, TrustLevel

guard = ActionGuard.default()

# Register agents with trust levels
guard.register_agent("orchestrator", TrustLevel.ORCHESTRATOR)
guard.register_agent("email-agent", TrustLevel.SUB_AGENT)
guard.register_agent("code-agent", TrustLevel.PEER)

# Context manager sets active agent
with guard.set_task_context("task-001", "email-agent"):
    send_email("boss@company.com", "Report", "...")

# Roll back all actions in a task across all agents
guard.rollback_task("task-001")

4. Policy Configuration (YAML)

# guard_config.yaml
version: "1.0"

policies:
  - name: "block_system_paths"
    action_types: ["file.delete", "file.write"]
    condition: "parameters.path.startswith('/etc')"
    verdict: BLOCK
    message: "System path access is forbidden"

  - name: "escalate_high_cost"
    action_types: ["*"]
    condition: "estimated_cost > 0.50"
    verdict: ESCALATE
    message: "Requires human approval"

agents:
  - id: "orchestrator"
    trust_level: 0.8
    can_delegate_to: ["worker-1", "worker-2"]
guard = ActionGuard.from_config("guard_config.yaml")

CLI

plyra-guard serve --config guard.yaml        # HTTP sidecar
plyra-guard inspect --config guard.yaml      # Pipeline visualization
plyra-guard explain --action file.delete     # Dry-run explanation
plyra-guard test-policy --condition "..."    # Interactive policy testing
plyra-guard version                          # Version info

Supported Frameworks

plyra-guard natively configures transparent adapters handling tool executions across the most popular multi-agent frameworks. Depending on your framework's internal architecture, the recommended integration pattern differs:

Framework Recommended approach
LangChain guard.wrap(tools)
LangGraph Custom GuardedToolNode (see examples/langgraph_integration.py)
AutoGen guard.wrap([func]) + register_function
CrewAI guard.wrap(tools)
OpenAI / Anthropic guard.wrap(tool_defs)
Generic Python @guard.protect() decorator

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        AI Agent / Framework                       โ”‚
โ”‚  (LangChain, LlamaIndex, CrewAI, AutoGen, OpenAI, Anthropic)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚ tool call
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      ๐Ÿ›ก๏ธ  plyra-guard                               โ”‚
โ”‚                                                                   โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ Adapter  โ”‚โ†’โ”‚   Evaluation Pipeline     โ”‚โ†’โ”‚ Execution Gate  โ”‚ โ”‚
โ”‚  โ”‚ Registry โ”‚  โ”‚                          โ”‚  โ”‚                 โ”‚ โ”‚
โ”‚  โ”‚          โ”‚  โ”‚ 1. Schema Validator      โ”‚  โ”‚ โ€ข Pre/post hooksโ”‚ โ”‚
โ”‚  โ”‚ โ€ข Lang-  โ”‚  โ”‚ 2. Policy Engine (YAML)  โ”‚  โ”‚ โ€ข Timeout mgmt  โ”‚ โ”‚
โ”‚  โ”‚   Chain  โ”‚  โ”‚ 3. Risk Scorer (0.0-1.0) โ”‚  โ”‚ โ€ข Error capture โ”‚ โ”‚
โ”‚  โ”‚ โ€ข OpenAI โ”‚  โ”‚ 4. Rate Limiter          โ”‚  โ”‚                 โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Custom โ”‚  โ”‚ 5. Cost Estimator        โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚ 6. Human Gate (optional)  โ”‚           โ”‚          โ”‚
โ”‚               โ”‚ 7. Custom evaluators...   โ”‚           โ”‚          โ”‚
โ”‚               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚          โ”‚
โ”‚                                                       โ”‚          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  Multi-Agent     โ”‚  โ”‚   Rollback    โ”‚  โ”‚  Observability    โ”‚ โ”‚
โ”‚  โ”‚                  โ”‚  โ”‚               โ”‚  โ”‚                   โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Trust Ledger   โ”‚  โ”‚ โ€ข Snapshots   โ”‚  โ”‚ โ€ข Audit Log       โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Instr. Chain   โ”‚  โ”‚ โ€ข File handlerโ”‚  โ”‚ โ€ข OpenTelemetry   โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Cascade Ctrl   โ”‚  โ”‚ โ€ข DB handler  โ”‚  โ”‚ โ€ข Datadog         โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Global Budget  โ”‚  โ”‚ โ€ข HTTP comp.  โ”‚  โ”‚ โ€ข Webhooks        โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Features

Evaluation Pipeline

Six built-in evaluators, fully pluggable:

Evaluator Purpose
SchemaValidator Validates ActionIntent structure
PolicyEngine YAML policies with AST-compiled conditions
RiskScorer Dynamic risk score (0.0-1.0) from 5 signals
RateLimiter Per-agent, per-tool sliding window limits
CostEstimator Token + API cost budget enforcement
HumanGate Human-in-the-loop approval gate

Add your own:

from plyra_guard import BaseEvaluator, ActionIntent, EvaluatorResult, Verdict

class MyEvaluator(BaseEvaluator):
    @property
    def name(self) -> str:
        return "my_evaluator"

    def evaluate(self, intent: ActionIntent) -> EvaluatorResult:
        if "dangerous" in intent.parameters:
            return EvaluatorResult(verdict=Verdict.BLOCK, reason="Dangerous parameter")
        return EvaluatorResult(verdict=Verdict.ALLOW, reason="OK")

guard.pipeline.add(MyEvaluator(), position="after_risk_scorer")

Multi-Agent Support

  • Trust Ledger โ€” Register agents with trust levels (HUMAN, ORCHESTRATOR, PEER, SUB_AGENT)
  • Instruction Chain โ€” Immutable provenance tracking across delegation hops
  • Cascade Controller โ€” Loop detection, depth limits, concurrent delegation caps
  • Global Budget โ€” Cross-agent cost aggregation with gaming detection

Rollback System

  • Automatic pre-execution state snapshots
  • Built-in handlers for files, databases, and HTTP (compensation endpoints)
  • Cross-agent rollback_task() undoes actions in reverse order

HTTP Sidecar

Language-agnostic access via HTTP:

plyra-guard serve --config guard_config.yaml --port 8080

curl -X POST http://localhost:8080/evaluate \
  -H "Content-Type: application/json" \
  -d '{"action_type": "file.read", "parameters": {"path": "/tmp/test"}, "agent_id": "my-agent"}'

Important Defaults for Production

When moving from local development to production, please note the following default behaviors:

  • StdoutExporter is enabled by default in DEFAULT_CONFIG. This means every action evaluated will print a JSON audit line to stdout. If this is too noisy for your production logs, override it in your YAML config by setting observability.exporters: [] or switching to a webhook or otel exporter.
  • The SnapshotManager persists state to ~/.plyra/snapshots.db (or your platform's default app data directory) via SQLite. This is designed to preserve rollback states dynamically, but ensures you are aware of local disk writes occurring automatically upon first initialization.
  • The Sidecar HTTP CORSMiddleware uses allow_origins=["*"]. This caters natively to seamless local dashboard development. In production deployments exposing the sidecar externally, ensure you mount it behind a strict API gateway or manually configure CORS constraints.

Documentation

Guide Description
Quickstart Get started in 5 minutes
Architecture How plyra-guard works internally
Policy Reference YAML policy syntax and built-in functions
Multi-Agent Guide Trust, delegation, and cascading
Rollback Guide Snapshot and rollback system
Adapters Framework integration details
API Reference Full public API documentation

Contributing

We welcome contributions! See CONTRIBUTING.md for detailed guidelines.

git clone https://github.com/plyra/plyra-guard.git
cd plyra-guard
pip install -e ".[dev,sidecar]"
pytest tests/ -v

License

Apache-2.0 License โ€” see LICENSE for details.


plyra-guard is part of the Plyra suite. Explore the full stack at plyra.dev.

Library Purpose Status
plyra-guard Action safety middleware โœ… stable
plyra-memory Tiered agent memory ๐Ÿ”œ soon
plyra-trace Observability & debugging ๐Ÿ”œ soon
plyra-budget Cost optimization ๐Ÿ”œ soon
plyra-mesh Multi-agent communication ๐Ÿ”œ soon

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

plyra_guard-0.1.8.tar.gz (99.9 kB view details)

Uploaded Source

Built Distribution

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

plyra_guard-0.1.8-py3-none-any.whl (105.3 kB view details)

Uploaded Python 3

File details

Details for the file plyra_guard-0.1.8.tar.gz.

File metadata

  • Download URL: plyra_guard-0.1.8.tar.gz
  • Upload date:
  • Size: 99.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plyra_guard-0.1.8.tar.gz
Algorithm Hash digest
SHA256 272a3bb36b448eb5705e0104f5ae0390f5ace20f1c82c20301513f6001c5d397
MD5 064c8a4f4fe9a0af2d1ac5a087e3f111
BLAKE2b-256 e79d61e8dc852aca073dd24967f31327aa498cf2023732a1f019298a45b74ebf

See more details on using hashes here.

Provenance

The following attestation bundles were made for plyra_guard-0.1.8.tar.gz:

Publisher: publish.yml on plyraAI/plyra-guard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plyra_guard-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: plyra_guard-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 105.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plyra_guard-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b0077dcb6fc501c6c9b0f6a8c193db996cd3daa5d7ab5a507196280c3307b55b
MD5 32df4255d0748933350fdf0dc91bf198
BLAKE2b-256 cf16a5baf528c1611edf8a045e1b27db39695b196b5bc4463c2f55f91f636444

See more details on using hashes here.

Provenance

The following attestation bundles were made for plyra_guard-0.1.8-py3-none-any.whl:

Publisher: publish.yml on plyraAI/plyra-guard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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