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
Fires before every
authorize() call. Return { allow: false, reason } to block. Return void or { allow: true } to proceed.Fires after authorize() completes with the final result. Useful for logging and alerting.
Fires before an agent is created. Return
{ allow: false } to reject the creation.Fires after an agent is successfully created.
Fires when an agent is revoked.
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.
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.