Skip to main content

Official Python SDK for AgentGuard — the AI action firewall

Project description

agentguard

Official Python SDK for AgentGuard — the AI action firewall that keeps your agents safe.

Install

pip install agentguard

Requirements: Python 3.8 or later.

Quick Start

from agentguard import AgentGuard

guard = AgentGuard(api_key="ag_live_YOUR_KEY_HERE")

Then, before your agent executes any action:

result = guard.check(
    action="send_email",
    payload={"to": "user@example.com", "body": "Hello!"},
)

if result.decision == "block":
    raise Exception(f"Action blocked: {result.reason}")

# proceed with the action

Constructor Options

AgentGuard(
    api_key: str,          # required — from your AgentGuard dashboard
    base_url: str = None,  # default: https://agentguard.dev/api
                           # also read from AGENTGUARD_BASE_URL env var
    timeout: float = 5.0,  # request timeout in seconds
)

The base_url is resolved in this order:

  1. base_url argument passed to the constructor
  2. AGENTGUARD_BASE_URL environment variable
  3. https://agentguard.dev/api (production default)

guard.check() Reference

result = guard.check(
    action: str,                        # required — name of the action
    payload: dict,                      # required — data the agent is acting on
    agent_id: str | None = None,        # which agent is acting (improves logging)
    app_user_id: str | None = None,     # end user ID (enables cross-user detection)
    app_name: str | None = None,        # identifies your app in the dashboard
    risk_context: dict | None = None,   # extra metadata for risk scoring
)

Return value — CheckResult

@dataclass
class CheckResult:
    decision: Literal["allow", "block", "review"]
    risk_level: Literal["low", "medium", "high", "critical"]
    reason: str
    log_id: int | None
    prompt_injection_detected: bool

Handling Each Decision

result = guard.check(action=action, payload=payload)

if result.decision == "allow":
    # Safe to proceed. Execute the action.
    execute_action(action, payload)

elif result.decision == "review":
    # High-risk but not definitively malicious.
    # Queue for human review or notify your security team.
    queue_for_review(action, payload, result.reason)

elif result.decision == "block":
    # Policy violation or detected threat. Do NOT proceed.
    raise Exception(f"Blocked ({result.risk_level}): {result.reason}")

Error Handling

from agentguard import (
    AgentGuard,
    AgentGuardAuthError,
    AgentGuardNetworkError,
    AgentGuardServerError,
)

try:
    result = guard.check(action=action, payload=payload)
except AgentGuardAuthError as e:
    # Invalid or expired API key — check your dashboard
    print(f"Auth error: {e}")
except AgentGuardNetworkError as e:
    # Connectivity issue — the SDK already retried once
    # Fall back gracefully (allow or queue for later)
    print(f"Network error: {e}")
except AgentGuardServerError as e:
    # Unexpected server error (5xx)
    print(f"Server error {e.status_code}: {e}")

The SDK automatically retries once on network errors. Auth errors and server errors are never retried.

Example — Flask Route

from flask import Flask, request, jsonify
from agentguard import AgentGuard
import os

guard = AgentGuard(api_key=os.environ["AGENTGUARD_KEY"])
app = Flask(__name__)

@app.post("/agent/send-email")
def send_email():
    data = request.get_json()
    result = guard.check(
        action="send_email",
        payload={"to": data["to"], "body": data["body"]},
        app_user_id=data.get("userId"),
        agent_id=data.get("agentId"),
    )

    if result.decision == "block":
        return jsonify({"error": result.reason}), 403

    # send the email...
    return jsonify({"ok": True})

Publishing to PyPI

When you're ready to publish:

cd sdks/python
pip install build twine
python -m build
twine upload dist/*

Make sure your pyproject.toml has the correct name, version, and license fields before publishing.

Links

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

aguard-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

aguard-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file aguard-0.1.0.tar.gz.

File metadata

  • Download URL: aguard-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for aguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2de5b9a3b928c0f33669f47df8e7110334e4434b17ce9fff1fe739ee31223ff7
MD5 62732a99a51abc90491e9749eafbf892
BLAKE2b-256 79126e069082e467f44558ba87234ab1d54e76b7499c344f40358fb63c419fdf

See more details on using hashes here.

File details

Details for the file aguard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aguard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for aguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bfad43597f79cc9b980e7ec66fbf77e18d59b70def03729c742abca5ad3739c
MD5 2c853fe17233e32a990ccd8c7af3a5c4
BLAKE2b-256 998cf37d9bd839c61333c96fd556040b40ca5ec5f509cadd8a58aaccb7b8b0f7

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