Documentation Index
Fetch the complete documentation index at: https://mintlify.com/microsoft/agent-governance-toolkit/llms.txt
Use this file to discover all available pages before exploring further.
govern() is the fastest path to governed agents. It wraps any Python callable — a tool function, an agent’s run() method, or a LangChain tool class — so that every invocation is checked against a policy, logged to the audit trail, and either permitted or blocked before a single line of the wrapped function executes.
safe_tool behaves identically to my_tool — same signature, same return type — except that it raises GovernanceDenied if the policy denies the call.
Import
Signature
Parameters
The function, tool, or agent callable to govern. The returned
GovernedCallable preserves the original function’s __name__, __doc__, and signature via functools.update_wrapper.The policy to enforce. Accepts:
- A file path to a YAML policy file (supports
extendsfor inheritance) - An inline YAML string
- A
Policyobject constructed programmatically
Agent identifier passed to the policy engine during evaluation. Use a specific agent ID when policies contain agent-scoped rules. Defaults to
"*" (wildcard, matches all agent patterns).When
True, every call to the governed function is logged to an in-memory AuditLog. The log is accessible via safe_tool.audit_log. Set to False in unit tests where you don’t need audit overhead.Optional callback invoked when the policy denies a call. Receives the
PolicyDecision. When None (default), a GovernanceDenied exception is raised. Use a callback to return a graceful fallback instead:Optional human-approval handler for
require_approval policy actions. Built-in handlers include ConsoleApproval (stdin prompt), WebhookApproval (HTTP callback), and CallbackApproval (custom function). Defaults to AutoRejectApproval (auto-rejects unapproved calls fail-closed).Optional advisory layer (e.g., a semantic classifier or LLM-based check). Advisory checks run only after the deterministic policy permits a call. They can escalate to a block, but never override a deny. Fails open — an advisory error does not block the call.
Policy conflict resolution strategy. Options:
"deny_overrides" (any deny wins — recommended for enterprise), "allow_overrides", "priority_first_match", "most_specific_wins". See Conflict Resolution for details.Optional privilege ring assignment for ring-level resource constraint enforcement. When set, ring enforcement runs before policy evaluation. Shared ring breach detectors accumulate violations across all governed callables for the same
(agent_id, session_id) pair.Session identifier used by the ring breach detector to count violations per session. Defaults to an empty string (treated as
"default").When combined with
approval_chain_id, routes require_approval decisions through the action-bound approval protocol (ADR-0030). Provides cryptographic binding, audit linkage, and fail-closed timeout semantics.Approval chain ID for the action-bound protocol. Required when
approval_coordinator is set.Time-to-live for approval requests in seconds. Requests that expire before approval is received are automatically rejected (fail-closed).
Returns
A callable wrapper that behaves identically to
fn but with governance enforcement on every call. Exposes two properties:GovernanceDenied Exception
GovernanceDenied is raised when the policy engine denies an action and no on_deny callback is registered.
The full
PolicyDecision that triggered the denial. Contains matched_rule, reason, action, allowed, and audit_entry."Action denied by policy rule '{rule_name}': {reason}".
How Context Is Built
govern() builds the policy evaluation context automatically from the wrapped function’s keyword arguments:
- If a kwarg named
actionis passed, it becomescontext["action"]. If it’s a dict, it’s used as-is; otherwise it’s wrapped as{"type": str(value)}. - All other kwargs are added directly to the context dict. Dicts are passed through; scalars are wrapped as
{"value": scalar}. - Positional arguments are mapped to
context["action"]["type"]when noactionkwarg is present.
Complete Code Examples
Quickstart — two lines
Graceful degradation with on_deny
Inspect the audit log
Human approval for sensitive operations
Async Tools
govern() wraps synchronous callables. For async tools, use asyncio.run inside the wrapped function or use the async-compatible approach:
LangChain Tool Integration
Wrap a LangChain tool by governing its_run method:
invoke:
GovernanceConfig Dataclass
For multi-step configuration, build aGovernanceConfig and pass it directly to GovernedCallable:
Policy file path, inline YAML string, or
Policy object.Agent identifier for policy evaluation.
Enable audit logging.
Path for a file-based audit log.
None = in-memory only.Callback on denial; default raises
GovernanceDenied.Optional human-approval handler for
require_approval policy actions.Optional advisory layer. Runs only after the deterministic policy permits a call. Fails open — an advisory error does not block the call.
Policy conflict resolution strategy.
Time-to-live for approval requests.
See Also
- PolicyEvaluator — full programmatic policy API
- AuditLogger — persistent audit trail
- AgentIdentity — cryptographic agent identity