Policy-as-Code
Define guard rules in TOML/YAML configuration files. Version-controlled safety policies.
Overview
Define guard rules in TOML or YAML configuration files instead of code. Version-control your safety policies alongside your application.
Configuration File
Create an agentguard.toml file in your project root:
toml
# agentguard.toml
[defaults]
validate_input = true
validate_output = true
max_retries = 2
timeout = 30.0
record = true
trace_dir = "./traces"
trace_backend = "sqlite"
[defaults.rate_limit]
calls_per_minute = 60
burst = 10
[tools.search_web]
detect_hallucination = true
timeout = 15.0
[tools.search_web.rate_limit]
calls_per_minute = 30
[tools.search_web.hallucination]
expected_latency_ms = [100, 5000]
required_fields = ["results", "total"]
[tools.send_email]
max_retries = 0
timeout = 60.0
[tools.send_email.budget]
max_calls_per_session = 10
mode = "block"
[tools.database_query]
validate_input = true
validate_output = true
[tools.database_query.circuit_breaker]
failure_threshold = 3
timeout = 120.0
Loading Policies
python
from agentguard import guard, load_policy
# Load from file (auto-detects TOML or YAML)
policy = load_policy("agentguard.toml")
# Apply policy to tools — decorator reads config by function name
@guard(policy=policy)
def search_web(query: str) -> dict:
return api.search(query)
@guard(policy=policy)
def send_email(to: str, subject: str, body: str) -> bool:
return email_api.send(to, subject, body)
# search_web gets hallucination detection + 30 calls/min
# send_email gets 10 calls max + no retries
# Both inherit defaults: input validation, 2 retries, tracing
CLI Validation
bash
# Validate your policy file
agentguard policy validate agentguard.toml
# Show resolved config for a specific tool
agentguard policy show search_web --config agentguard.toml
# Diff two policy files
agentguard policy diff staging.toml production.toml
YAML Support
If you prefer YAML:
python
# agentguard.yaml
defaults:
validate_input: true
max_retries: 2
rate_limit:
calls_per_minute: 60
tools:
search_web:
detect_hallucination: true
timeout: 15.0
rate_limit:
calls_per_minute: 30
Environment-Specific Policies
python
from agentguard import load_policy
# Load different policies per environment
import os
env = os.getenv("ENVIRONMENT", "development")
policy = load_policy(f"policies/{{env}}.toml")
# development.toml — loose limits, verbose tracing
# staging.toml — production-like limits, full tracing
# production.toml — strict limits, minimal tracing
✅ Version control your policies
Check your agentguard.toml into git. Use PR reviews for policy changes. This gives you an audit trail of who changed safety rules and when.
TOML Policy Format
All supported fields in agentguard.toml:
toml
version = "1"
[defaults]
validate_input = true
max_retries = 2
record = true
trace_dir = "./traces"
trace_backend = "sqlite"
[tools.search_web]
timeout = 10.0
detect_hallucination = true
[tools.search_web.rate_limit]
calls_per_minute = 60
burst = 10
[tools.search_web.hallucination]
expected_latency_ms = [100, 5000]
required_fields = ["results", "total"]
[tools.query_database]
timeout = 30.0
[tools.query_database.budget]
max_cost_per_session = 1.00
cost_per_call = 0.05
on_exceed = "warn"
[tools.query_database.circuit_breaker]
failure_threshold = 5
recovery_timeout = 60
[tools.query_database.retry]
max_retries = 3
initial_delay = 0.5
backoff_factor = 2.0
max_delay = 30.0
jitter = true
API Functions
python
from agentguard.policy import load_policy, apply_policy, validate_policy
# Load and parse a policy file
policy = load_policy("agentguard.toml")
# Returns: dict[str, GuardConfig]
# Validate without loading
errors = validate_policy("agentguard.toml")
if errors:
print("Validation failed:", errors)
# Apply policy to a list of tool functions
guarded = apply_policy(policy, [search_web, query_db])
# Returns: dict[str, GuardedTool]
# missing_ok=False raises PolicyError for unmatched tools
guarded = apply_policy(policy, [search_web], missing_ok=False)
CLI Commands
bash
# Validate a policy file
agentguard policy validate agentguard.toml
# Show human-readable summary
agentguard policy apply agentguard.toml
Supported Configuration Keys
| Section | Valid Keys |
|---|---|
| Top-level | version, defaults, tools |
| defaults / tools.* | validate_input, validate_output, detect_hallucination, max_retries, timeout, record, trace_dir, trace_backend, trace_db_path, session_id, budget, rate_limit, circuit_breaker, retry, hallucination |
| budget | max_cost_per_call, max_cost_per_session, max_calls_per_session, alert_threshold, on_exceed, cost_per_call |
| rate_limit | calls_per_second, calls_per_minute, calls_per_hour, burst, on_limit |
| circuit_breaker | failure_threshold, recovery_timeout, success_threshold, on_open |
| retry | max_retries, initial_delay, max_delay, backoff_factor, jitter |