What hooks are
Hooks are async callbacks you register at startup. KavachOS calls them at specific points in the authorization and agent lifecycle. They let you add custom logic without patching the SDK: log denials to Slack, block agents from running in unsandboxed environments, fire webhooks, or update your own database.Available hooks
beforeAuthorize
Fires before every
authorize() call. Return { allow: false, reason } to block. Return void or { allow: true } to proceed.afterAuthorize
Fires after authorize() completes with the final result. Useful for logging and alerting.
beforeAgentCreate
Fires before an agent is created. Return
{ allow: false } to reject the creation.afterAgentCreate
Fires after an agent is successfully created.
onAgentRevoke
Fires when an agent is revoked.
onViolation
Fires when a permission denial, rate limit, or policy violation is detected.
Violation types
TheonViolation hook receives a typed type field so you can route each category to a different handler.
| Type | When it fires |
|---|---|
permission_denied | The agent lacks the required permission |
rate_limited | The agent hit a rate limit |
ip_blocked | The request IP is on a blocklist |
time_restricted | The request is outside the allowed time window |
approval_required | The action needs human approval before proceeding |
Registering hooks
Pass ahooks object to createKavach:
Logging every denial
result.auditId links this log line to the immutable audit entry. You can use it to correlate your own logs with the KavachOS audit trail.
Enforcing a sandbox check
{ allow: false } from beforeAgentCreate causes the kavach.agent.create() call to throw a KavachError with the reason you provided.
Reacting to violations
TheonViolation hook fires for any denial that fits a known violation category. Use it to send alerts or update your observability platform.
Cleaning up after revocation
Next steps
Budget policies
Block agents when they exceed token or call limits.
Event streaming
Stream authorization events to Kafka, NATS, or Webhooks.
Audit log
Query the immutable record of every authorization decision.