Policy Module
The policy module provides policy-based execution control for agents, allowing you to define rules for what agents can and cannot do.Installation
Features
- Define rules for what agents can/cannot do
- Tool execution policies
- Resource access control
- Rate limiting and quotas
Quick Start
Classes
PolicyEngine
Main engine for evaluating policies.Methods
| Method | Description |
|---|---|
add_policy(policy) | Add a policy to the engine |
remove_policy(name) | Remove a policy by name |
check(resource, context) | Check if action is allowed |
list_policies() | List all policies |
Policy
A collection of rules.Attributes
| Attribute | Type | Description |
|---|---|---|
name | str | Policy name |
description | str | Policy description |
rules | list[PolicyRule] | List of rules |
priority | int | Evaluation priority |
enabled | bool | Whether policy is active |
PolicyRule
A single rule in a policy.Attributes
| Attribute | Type | Description |
|---|---|---|
action | str | ”allow” or “deny” |
resource | str | Resource pattern (supports wildcards) |
reason | str | Reason for the rule |
conditions | dict | Additional conditions |
PolicyResult
Result of a policy check.Attributes
| Attribute | Type | Description |
|---|---|---|
allowed | bool | Whether action is allowed |
reason | str | Reason for decision |
policy_name | str | Policy that made decision |
rule | PolicyRule | Rule that matched |
PolicyAction
Enumeration of policy actions.Convenience Functions
create_deny_tools_policy
Create a policy that denies specific tools.create_allow_tools_policy
Create a policy that only allows specific tools.create_read_only_policy
Create a read-only policy.Usage Examples
Basic Policy
With Agent
Conditional Rules
Multiple Policies
Best Practices
- Use wildcards - Pattern matching for flexible rules
- Set priorities - Higher priority policies are evaluated first
- Provide reasons - Clear reasons help debugging
- Use convenience functions - Pre-built policies for common cases
- Test policies - Verify rules work as expected
Related
- Agent - Using policies with agents
- Hooks - Event hooks
- Guardrails - Output validation

